user3605574
user3605574

Reputation: 11

To print the given rows of string into columns

I have *.txt file with first row as name,address,mail id and second line with the values. I have to print this into two columns,the first one with the headings and second with the value using Java. how do I do this?

public class ReadFile1 {

    public static void main(String[] args) {
        BufferedReader br=null;
        String sCurrentLine = null;
        String delimiter = ",";
        String[] filetags;
        try {
            br = new BufferedReader(new FileReader("path\\Read.txt"));
            sCurrentLine = br.readLine();
            StringBuffer result = new StringBuffer();           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        filetags = line.split(delimiter);
        for(int i = 0;i < line.length(); i++)
        {
            System.out.println("****" +sCurrentLine);
            String[] s = line.split(",");
            for(int j = i-1; j<line.length();j++)
            {
                System.out.println("##############"+Arrays.toString(s));
            }
        }
    }
}

This is what I tried. Ex: I have a file say,

line1) name,email,mobile and second 
line2) john,[email protected],9876 
line3) max,[email protected],1234  

Now, I need to print:

name john
email [email protected]                                                  
moblie 9876                                                  
name max                                                                
email [email protected]                                                  
mobile 1234 

Upvotes: 0

Views: 1076

Answers (2)

ConMan
ConMan

Reputation: 1652

Something like this should do the trick.

  1. Read the file and store each line in a list.
  2. Iterate through the list of lines
  3. If it is safe to assume the first line will always be the title line, take the input and store it in a collection.
  4. For the rest of the lines, split on the comma and use the index of the splitter array to refer to the title column.
List <String> lines = new ArrayList<String>();
Scanner scanner = new Scanner(new File("FileName.txt"));
while(scanner.hasNextLine()){
    String line = scanner.nextLine();
    lines.add(line);
}
scanner.close();

int lineNo = 0;
List <String> title = new ArrayList<String>();
for(String line : lines){
    if(lineNo == 0){
        String [] titles = line.split(",");
        for(String t : titles){
            title.add(t);
        }
        lineNo++;
    }
    else{
        String input = line.split(",");
        for(int i = 0; i<input.length; i++){
            System.out.println(title.get(i) + ": " + input[i]);
        }
        lineNo++;
    }
}

Upvotes: 0

Levenal
Levenal

Reputation: 3806

Below is one way you may be able to get what you want, It is similar to how you have attempted but slightly more refined.

The File:

 name,email,mobile and second
 john,[email protected],9876
 max,[email protected],1234

The code:

    //File is on my Desktop
    Path myFile = Paths.get(System.getProperty("user.home")).resolve("Desktop").resolve("tester.txt");
    //Try-With-Resources so we autoclose the reader after try block
    try(BufferedReader reader = new BufferedReader(new FileReader(myFile.toFile()))){
        String[] headings = reader.readLine().split(",");//Reads First line and gets headings
        String line;
        while((line = reader.readLine()) != null){//While there are more lines
            String[] values = line.split(","); //Get the values
            for(int i = 0; i < values.length; i++){//For each value
                System.out.println(headings[i] + ":  " + values[i]);//Print with a heading
            }
        }
    } catch (IOException io) {
        io.printStackTrace();
    } 

Good Luck!

Upvotes: 1

Related Questions