Ronald Segan
Ronald Segan

Reputation: 215

Read File in Java, output the first comma delimited String

I want to extract the first String in a file using the delimiter ",". Why does this code generate a number of lines greater than one?

public static void main(String[] args) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("irisAfter.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            read = in.readLine();
            String[] splited = read.split(",");
            for (int i =0; i<splited.length;i++) {
                System.out.println(splited[0]);
            } 
        }
    } catch (IOException e) {
            System.out.println("There was a problem: " + e);
            e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) { e.printStackTrace(); }
    }
}

Upvotes: 1

Views: 1667

Answers (5)

Blake Yarbrough
Blake Yarbrough

Reputation: 2316

if you modify your code like this, you should get the result you expect.

 public static void main(String[] args) {

        BufferedReader in = null;
        try {
            String[] splited;
            in = new BufferedReader(new FileReader("irisAfter.txt"));
            String read = null;
            while ((read = in.readLine()) != null) {
                read = in.readLine();
                splited = read.split(",");
            }

            System.out.println(splited[0]);


        } catch (IOException e) {
            System.out.println("There was a problem: " + e);
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
}

Upvotes: 1

Buurman
Buurman

Reputation: 1984

You read in every line from "irisAfter.txt", then split each line on "," into multiple elements, then print out the first element of that line on its own line as many times as there are elements in the line. Multiple lines*multiple elements per line = more lines in output than in input.

Change

for (int i =0; i<splited.length;i++) {
      System.out.println(splited[0]);

}

to

if (splited.length > 0)
{
     System.out.println(splited[0]);
}

That way you print out the first element of every line on its own line only one time and only if there actually is a first element.

You are also skipping every other line. If you don't want to do that, remove the line read = in.readLine(); just below while ((read = in.readLine()) != null) {.

(You are now reading in a line and then reading in the next line, discarding the first read in line. Then you process that second line, after which the loop starts again, you read in the third line, then read in the fourth line, discarding the third, etc. etc.)

Upvotes: 2

integer-j
integer-j

Reputation: 344

Not sure why your code works that way but you might try Scanner with a delimeter. Try:

Scanner sc = new Scanner( new File("myNumbers")).useDelimiter(",");
String firstString = sc.next(); 
/// check for null..

Upvotes: 3

Rey Libutan
Rey Libutan

Reputation: 5314

You are printing inside a loop. That's why it is printing multiple times (if that's what you're asking).

String[] splited = read.split(",");    
System.out.println(splited[0]);

will just do

EDIT: As Abishek also mentioned, don't read = in.readLine(); again inside your while loop since by doing so you are skipping a line.

while ((read = in.readLine()) != null) {
   String[] splited = read.split(",");
   System.out.println(splited[0]);
}

Upvotes: 5

Raman Shrivastava
Raman Shrivastava

Reputation: 2953

What do you mean by number of lines superior to the original ones

If you are using splited[0], why are you keeping inside a loop. It will always get you same string

Upvotes: 4

Related Questions