0
   1
   0
   0
   0
   1
   1
   1

Have a text file that has to be read line by line to code below in the array, however it only stop at the first line. As in it gets the first ineger "1" and doesn't get to the rest of the integers, line by line

String fileName = "input.txt";
File file = new File(fileName);
Scanner scanner = new Scanner(file);
//  String s = "";
while(scanner.hasNextLine()){
     data1 = scanner.nextLine();
}


for ( int i = 0; i < data1.length(); i++)
{
     covertDataArray[i] = Byte.parseByte(data1.substring( i, i+1));
}
4
  • 3
    data1 loses his value everytime Commented Apr 22, 2014 at 17:01
  • What I see is that your code reads all the way down to the last element that is also 1. You will have to use an array to store data. Commented Apr 22, 2014 at 17:02
  • Yup, you're overwriting the value of data1 for each line, so you actually only get the last integer, not the first. Commented Apr 22, 2014 at 17:02
  • This is where using a debugger would help debug your program and give you a better understand of what your program is doing. Commented Apr 22, 2014 at 17:10

4 Answers 4

3

Let's walk through the code.

    while(scanner.hasNextLine()){
      data1 = scanner.nextLine();
    }

data1 will always have only the content of the latest line you have read in it.

To fix, simply append to the variable instead:

    while(scanner.hasNextLine()){
      data1 += scanner.nextLine();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Even to append he will have to use a buffered string, right?
Yes this was it, ugh the Detail smh... the detail
If the file is very long this code will create X strings everytime, use a StringBuilder.
1

That'll do:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LineByLine {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "input.txt";
        File file = new File(fileName);
        Scanner scanner = new Scanner(file);

        int len = 0;
        int [] covertDataArray = new int[100];
        while (scanner.hasNextLine()) {
            String data1 = scanner.nextLine();
            covertDataArray[len] = Integer.parseInt(data1.trim());
            len++;
        }

        for (int i = 0; i < len; i++) {
            System.out.println(covertDataArray[i]);
        }
    }

}

Comments

1

you always replace the content of data1, in the while loop use:

data1 += scanner.nextLine();

Comments

0

This may be due to the fact that "Data" is not an array. What it is doing is it is rewriting over the object data every time it iterates through the while loop.

I would recommend making Data and ArrayList (because it is a flexible array) and using that to store the data.

Here is a sample of what the code could be:

ArrayList<Integer> data1 = new ArrayList<>;
while(scanner.hasNextLine()){
     data1.addObject(scanner.nextLine());
}

If you use this method, it will not be necessary to parse the individual integers, which will improve the performance of your program.

Comments

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.