1

My code is just printing out the last number from the list I create in a different program. I need help storing the data into an array so I can sort it after. edit: I need to take data from a file which is 'numbers.txt' and store it into an array.

public static void main(String[] args) throws Exception { int numberArray = 0; int[] list = new int[16]; File numbers = new File("numbers.txt"); try (Scanner getText = new Scanner(numbers)) { while (getText.hasNext()) { numberArray = getText.nextInt(); list[0] = numberArray; } getText.close(); } System.out.println(numberArray); int sum = 0; for (int i = 0; i < list.length; i++) { sum = sum + list[i]; } System.out.println(list); } } 
7
  • I'm also not supposed to use arrayList<> Commented Nov 17, 2014 at 0:46
  • 2
    You've got to increment the list index when putting things in it. You're only placing items in list[0]! Use the numberArray variable. Increment it inside the loop and use it as the list array index. Commented Nov 17, 2014 at 0:48
  • 1
    You also need a catch or finally block after your try block. Without it this code won't compile Commented Nov 17, 2014 at 0:49
  • 1
    @hagbear: I didn't mean for you to delete your answer, just add an explanation. Commented Nov 17, 2014 at 0:53
  • 1
    My code is just printing out the last number from the list because you're printing numberArray which contains the last read number. And you don't need to call getText.close();. The try-with-resources statement does that for you. Commented Nov 17, 2014 at 1:01

1 Answer 1

1

Correction in the code.

1.) Inside while loop, list[0] = numberArray;, will keep adding elements on the same index 0, so lat value will override. SO something like list[i] = numberArray; will work, and increement i inside while loop. Take care of ArrayIndexOutOfBound Exception here.

public static void main(String[] args) throws Exception { int numberArray = 0; int[] list = new int[16]; File numbers = new File("numbers.txt"); int i =0; // Check for arrayIndexOutofBound Exception. SInce size is defined as 16 try (Scanner getText = new Scanner(numbers)) { while (getText.hasNext()) { numberArray = getText.nextInt(); list[i] = numberArray; i++; } getText.close(); } System.out.println(numberArray); int sum = 0; for (int i = 0; i < list.length; i++) { sum = sum + list[i]; } System.out.println(list); } } 
Sign up to request clarification or add additional context in comments.

6 Comments

The body of the while (getText.hasNext()) loop could be a one-liner: list[i++] = numberArray = getText.nextInt(); :D.
@Tom yes we can make optimizations in the code, but OP is not asking about optimizing the code, it is about the functionality itself is not working.
@ankur-singhal thank you for showing me. Although I did as you did and when I print out the array, it only prints out the memory address I believe.
@odus yes this statement System.out.println(list); will do that, so iterate as you are doing just above and print System.out.println(list[i]);
@ankur-singhal ahh I knew that! thank you! how do I get as good as you?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.