• Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible to load array with data file and display in other method?  RSS feed

 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to load the entire array (50 different integers), but need to create a seperate method to display only the first 10 numbers....... I have asked a lot of people and nobody seems to have an idea! Please help



could somebody please help, maybe an example of a code that you would use to display numbers 1-50 in a seperarate method?
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Include a check for your ageCount in your while loop.

For example while(infile.hasNext && ageCount < 20) would get you the first 20 records (if the infile has that many records).

From there you can put the values in an array (or list, or whatever you choose), and pass that array to a new method when the loop is done.
 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would one display that in a seperate method?
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't edit my post quick enough for your reply:

From there you can put the values in an array (or list, or whatever you choose), and pass that array to a new method when the loop is done.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Taylor,

I've added code tags for you. Please check how to UseCodeTags (<-link) next time.
See how much easier to read your code

edit: also corrected code indentation
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some observations:
You have declared array, but you're not using it in order to store "age's".
What you need to do, is, instead "age = infile.nextInt();" on line 18 or 19 can't see which line it is, you need to store age to an array.

Fix that, then let's see what other problems you still facing.
 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i did so by changing that line to:

anArray[age] = infile.nextInt()

/*********************/

but it tells me i need to initialize it, so if i initialize it by setter age = 0, then when i try to output the array in a different method or even simply placing System.out.print(age) after the try-catch, the output is 0....

Can somebody please show me what a code would look like for a different type of program? That way i can see how an accurate code similar to this one, is supposed to look
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i did so by changing that line to:
anArray[age] = infile.nextInt()
It is not correct. "age" is the variable which holds the value you want to add to an array's certain position. Within brackets needs to be specified position (index) in which you're going to add your element.
Each array element is being accessed by index. If your array can hold 10 elements, it means your array have 10 indices starting from 0 to 9 (inclusive).
So, to add elements to array:
But manually specify positions where you want to add elements wouldn't be best solution.
You need to use "anArray[index]", where index suppose to be incremented every iteration.
An example with "for" loop:
The same effect you can achieve with "while" loop by declaring your index variable outside the loop and later by incrementing within the loop body.
 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not get that to work, i think im confusing myself the more things that i try. This is the code that i have with notes. Somebody....Anybody...

 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that your computer is on. Turn it off. Get some paper and pencils, and start writing out directions to be given to a child. Break it down step by step. When you think you have each step needed, go back and try refining each step into more discrete pieces.

Once you have that done, start coding it in pieces. Remember that the part that prints the array shouldn't care how that array gets populated. So, if you wanted to start with writing the method that prints the first ten elements of an array, build an array by hand with (say) 15 elements. Pass it in to your method, and get JUST THAT METHOD to work. Once you have it working exactly how you want it, THEN you can start writing the code to populate the array in whatever manner you wish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!