Home > OS >  print the column values with a separator and by using for loop
print the column values with a separator and by using for loop

Time:04-29

I want to read each column values of the row and print the column list values with a separator and by using for loop in java ,the data we get from DB as list object each row and each column , the column headers are username , last name , first name , email id, address. print the column values in console. could some one help me in this with the logic. Thanks in advance .In this way the o/p should be with the list of 5 parameters column values

CodePudding user response:

  1. Open file
  2. Read the line with readLine() method
//open the file
String line;
while ((line = file.readLine()) != null) {
  String[] result = line.split(";");
}

The split() method allows you to split the string using the separator into a String array. So, each row will be an array with the columns of the row.

CodePudding user response:

If you really want to use a for-loop:

for (String line = file.readLine(); line != null; line = file.readLine()) {
  String[] split = line.split("");
  System.out.println(String.join(", ", split));
}

For a file like this:

User
Example
Person
[email protected]

It would print:

U, s, e, r
E, x, a, m, p, l, e
P, e, r, s, o, n
e, x, a, m, p, l, e, u, s, e, r, @, g, m, a, i, l, ., c, o, m

CodePudding user response:

Read from what?? A text or CSV file? A database? Regardless of where the data is coming, I'm going to assume it's being read from a text file. It looks to me (at least as I see it) that you want to display the file data in the Console Window in a table style format...something like this:

User Name    | Last Name    | First Name   | E-Mail ID        | E-Mail Domain       
=============================================================================
Creeper001   | Jones        | Tom          | tjones           | @yahoo.com           
BigSpender   | Cash         | Johnny       | backtofolsom     | @yahoo.com           
TripWire     | Whire        | Trip         | nevercaught      | @hotmail.com         
BigSplit     | Ciccone      | Madonna      | ilikeitcreamy    | @muchmusic.com       
WhatThe...   | Newhart      | Bob          | usetobegood      | @gmail.com           
=============================================================================

This of course comes from file data that looks like this:

MyDataFile.txt:

reeper001, Jones, Tom, tjones, yahoo.com
BigSpender, Cash, Johnny, backtofolsom, yahoo.com
TripWire, Whire, Trip, nevercaught, hotmail.com
BigSplit, Ciccone, Madonna, ilikeitcreamy, muchmusic.com
WhatThe..., Newhart, Bob, usetobegood, gmail.com

With that being said, you can display the above table with something like this:

String filePath = "MyDataFile.txt";
String header = String.format("%-12s | %-12s | %-12s | %-16s | %-20s", 
        "User Name", "Last Name", "First Name", "E-Mail ID", "E-Mail Domain");
String underline = String.join("", java.util.Collections.nCopies(header.trim().length(), "="));
System.out.println(header   System.lineSeparator()   underline);
File file = new File(filePath);
try (Scanner reader = new Scanner(file)) {
    String line;
    while (reader.hasNextLine()) {
        line = reader.nextLine().trim();
        if (line.isEmpty()) { continue; }
        String[] lineParts = line.split("\\s*,\\s*"); // Comma is the most typical delimiter for CSV data.
        System.out.printf("%-12s | %-12s | %-12s | %-16s | @%-20s%n", 
                lineParts[0], lineParts[1], lineParts[2], lineParts[3], lineParts[4]);
    }
    System.out.println(underline);
    System.out.println();
}
catch (FileNotFoundException ex) {
    System.out.println("Can Not Locate The File Specified: -> "   file.getAbsolutePath());
}

Note that the Header is formatted using the String#format() method and the file data is formatted using the Scanner#printf() method. Both work in a similar fashion.

  • Related