1

My problem is the next: 1. I should get back 40 lines, but i get only 20. 2. The biggest problem is that i get back NULL for all lines. Why is that? What is the problem with the file reading?

Thanks in advance!

public class Program{
 public static void main(String[] args) throws FileNotFoundException, IOException {

    String[] egysor = new String[5];
        String line;
    FileReader fr = new FileReader("szavazatok.txt");
    BufferedReader br = new BufferedReader(fr);
    int n=0;
    Sor[] sorok = new Sor[100];

    while(br.readLine()!=null){
        sorok[n] = new Sor();
        egysor = br.readLine().split(" ");

        sorok[n].setKorzet(egysor[0]);
        sorok[n].setSzavazat(Integer.parseInt(egysor[1]));
        sorok[n].setVezNev(egysor[2]);
        sorok[n].setUtoNev(egysor[3]);
        sorok[n].setPart(egysor[4]);
        n++;
        System.out.println(sorok[n]);
    }
    System.out.println(n);

and my class:

public class Sor {
private String korzet, vezNev, utoNev, part;
private int szavazat;`

public String getKorzet() {
    return korzet;
}

public void setKorzet(String korzet) {
    this.korzet = korzet;
}

public String getVezNev() {
    return vezNev;
}

public void setVezNev(String vezNev) {
    this.vezNev = vezNev;
}

public String getUtoNev() {
    return utoNev;
}

public void setUtoNev(String utoNev) {
    this.utoNev = utoNev;
}

public String getPart() {
    return part;
}

public void setPart(String part) {
    this.part = part;
}

public int getSzavazat() {
    return szavazat;
}

public void setSzavazat(int szavazat) {
    this.szavazat = szavazat;
}


public Sor(){
    this.korzet = korzet;
    this.vezNev = vezNev;
    this.utoNev = utoNev;
    this.part = part;
    this.szavazat = szavazat;
}

public String toString(){
    return "korzet"+korzet;
}

2 Answers 2

1

Your while loop is eating up half the lines.

while(br.readLine()!=null)

the loop test condition will read a line. And you basically throw this data away. Then in the loop you read the next line and actually process it. What you need to do is this.

String inputLine = br.readLine();
while (inputLine != null){
     //...your logic.
     inputLine=br.readLine();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Calling BufferedReader.readline() will consume and return the line that was just read.

BufferedReader.readLine():

Reads a line of text. A line is considered to be terminated by any one of a >line feed ('\n'), a carriage return ('\r'), or a carriage return followed >immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line->termination characters, or null if the end of the stream has been reached

Try this:

String line;

while( (line = br.readLine() ) != null) {
    sorok[n] = new Sor();
    egysor = line.split(" ");

    sorok[n].setKorzet(egysor[0]);
    sorok[n].setSzavazat(Integer.parseInt(egysor[1]));
    sorok[n].setVezNev(egysor[2]);
    sorok[n].setUtoNev(egysor[3]);
    sorok[n].setPart(egysor[4]);
    n++;
    System.out.println(sorok[n]);
}

1 Comment

thanks now i get back 40, but all the lines are NULL :( WHY?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.