inFile reader into 7 arrays
Hey guys, im still struggling with this homework /:< so i have the intro but when the program reads the file i can only get it to read each integers separately instead of making an array of 3. it should print out something like this:
day 1: 200 500 800
day 2: 300 400 700
day 3: 100 0 500
day 4: 1700 400 600
day 5: 300 100 400
day 6: 800 900 600
day 7: 1000 300 700
but ends up doing this...
day 1: 200
day 2: 500
day 3: 800
...
day 20: 300
day 21: 700
this is the inFile part of the code:
---------------------------------------------------------------------------------
while(in.hasNext())
try
{
int value = Integer.parseInt(in.next());
System.out.println("Day " + lineNumber + ": "+ value);
lineNumber++;
}
catch (NumberFormatException e)
{
System.out.println("Input was not a number");
in.nextLine();
}
---------------------------------------------------------------------------------
My question is, how can i make an array that can print the first three integers then make another array that would read the next three and so on... im thinking of doing a for loop but i have no idea how to set it up /: anyone?
Re: inFile reader into 7 arrays
Quote:
how can i make an array that can print the first three integers
Your terminology is weird. arrays hold data, they do not print.
If you mean that you want an array to hold integers,
define an int array and an int variable that will be an index to that array.
Read an int value into the the array element indexed by the value of the index variable:
anIntArray[index] = <read the int value here>; // read into into an array element
then increment the value of index by one and do it again.
One way that this is easily done is by putting the code in a loop.
If you are reading in 7 rows of data with 3 integers in each row, then you should look at using a 2 dimension array. Have one dim be the 7 rows and the other the 3 integers.
Then have a nested loop. The outer loop for rows changes the first index from 0 to 6 and the inner loop for columns changes its index from 0 to 2.
Re: inFile reader into 7 arrays
hmm is because the numbers are supposed to be calories that a member of a fitness company inputs. & the seven rows are for the 7 days of the week. In my assignment instructions it asks to print out:
- a list of total number of calories consumed each day
- the avg # of cals consumed each day
- avg # of cals consumed in each of the 3 meals
- max # of cals consumed in any specific day
- the max # of cals consumed in any one meal (of any type).
So what you're telling me that in order to print out all this info it would have to be in a String variable? So Arrays can't be printed in any form? What can I do then?
Re: inFile reader into 7 arrays
You can print out the contents of an array. Depending on the data type. There are methods to convert most data types to Strings so you can print them.
There is a method in the Arrays class (toString) that will print out the contents of an array.