Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading file into an array

 
Yafet Negussie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I'm trying to read list of numbers from a file to an array. The numbers are read fine, but I'm unable to put them into an array. I cannot see what I'm doing wrong. Can you help please? Here is my code
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it's not working because this line prints out 0's
System.out.println(numbers[index]);
look at the order of your lines of code

starts at
index = 0,
then numbers[0] = ...
then index++
so, in this first time through
System.out.println(numbers[index]);
is actually
System.out.println(numbers[1]);

or, it's printing out the value of the next numbers[]

 
Yafet Negussie
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Michael
that was indeed the problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic