Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: inFile reader into 7 arrays

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: inFile reader into 7 arrays

    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.
    Last edited by Norm; September 3rd, 2011 at 03:47 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default 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.

Similar Threads

  1. 1d Arrays and inFile. HELP!
    By iam.alex in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 1st, 2011, 03:01 AM
  2. email reader
    By rk.kavuri in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 4th, 2011, 03:59 AM
  3. Buffered Reader to J Option
    By jk_0821 in forum Collections and Generics
    Replies: 13
    Last Post: July 19th, 2010, 03:14 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM

Tags for this Thread