0
FileReader myReader = new FileReader(myReaderRef); Scanner input = new Scanner(myReader); int arraySize = 0; while(input.hasNextLine()){ arraySize++; input.nextLine(); } int[] numbers; numbers = new int[arraySize]; while(input.hasNextLine()){ for(int i=0; i < numbers.length; i++){ numbers[i] = input.nextInt(); } } input.close(); System.out.print(numbers[1]); } } 

and the text file it is reading from reads as follows:

10 2 5 1 7 4 9 3 6 8 

whenever i use system.out.print to output one of the array slots, it only gives me 0 no matter which array position i call. where am I going wrong?

edit: I had to close and restart both the filereader and scanner. Thanks for the help!

4 Answers 4

2

You try and read through the file twice without going back to the beginning. The first time is to count the lines, the second time is to read the data. Because you do not go back to the beginning of the file, no data is loaded / stored anywhere. So you should re-start your Scanner, eg. close and re-open.

Or you might want to consider using an ArrayList so you only need to read the file once.

List<Integer> numbers = new ArrayList<Integer>(); Scanner input = new Scanner(myReader); while (input.hasNextLine()) { numbers.add(input.nextInt()); input.nextLine(); // You might need this to get to the next line as nextInt() just reads the int token. } input.close() 
Sign up to request clarification or add additional context in comments.

1 Comment

im not quite familiar with array lists yet so I'm just experimenting with what I know so far. I may look into experimenting with that after I get what i'm currently using down. Thanks for the input though!
1

You need to restart your Scanner after you count the lines (because it's at the end of the File).

Scanner input = new Scanner(myReader); int arraySize = 0; while(input.hasNextLine()){ arraySize++; input.nextLine(); } input.close(); // <-- close it. myReader = new FileReader(myReaderRef); // Create a new FileReader input = new Scanner(myReader); // <-- create another scanner instance 

2 Comments

just did that and I am still getting 0's for all slots.
If you do input.close() won't the filereader be closed too? Which means you have to initialize it again too...
0

In your first while loop cursor already went to the last line of the file. So its not able to find anything after that in the next loop. So you have to create 2 Scanner object.

 Scanner input = new Scanner(new File("D:\\numbers.txt")); int arraySize =0; while(input.hasNextLine()){ arraySize++; input.nextLine(); } int[] numbers; numbers = new int[arraySize]; input.close(); Scanner input2 = new Scanner(new File("D:\\numbers.txt")); while(input2.hasNextLine()){ for(int i=0; i < numbers.length; i++){ numbers[i] = input2.nextInt(); } } input2.close(); System.out.print(numbers[1]); 

Comments

-1

I tried to simplify the problem as much as possible.

The easiest way to add/remove elements to an Array for your case would be to use an ArrayList object. Read through the comments and run the project.

The first list of integers below are the original input's of the file.

The second list contains the array's printed statements. These answers might not be where you expect them to be indexed but I'm sure this will get you on the right path :)

10 2 5 1 7 4 9 3 6 8 

[10, 2, 5, 1, 7, 4, 9, 3, 6, 8] 3 1 7 5 2 8 package cs1410; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import javax.swing.JFileChooser; public class ArrayReader { public static void main(String[] args) throws FileNotFoundException { // Creates an array list ArrayList<Integer> answer = new ArrayList<Integer>(); // Reads in information JFileChooser chooser = new JFileChooser(); if (JFileChooser.APPROVE_OPTION != chooser.showOpenDialog(null)) { return; } File file = chooser.getSelectedFile(); // Scan chosen document Scanner s = new Scanner(file); int count = 0; // Scans each line and places the value into the array list while (s.hasNextLine()) { int input = s.nextInt(); answer.add(input); } // Kaboom System.out.println(answer); System.out.println(answer.indexOf(1)); System.out.println(answer.indexOf(2)); System.out.println(answer.indexOf(3)); System.out.println(answer.indexOf(4)); System.out.println(answer.indexOf(5)); System.out.println(answer.indexOf(6)); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.