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 5 of 5

Thread: Help reading in data

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

    Default Help reading in data

    Hello i'm working on this program and at the beginning steps i'm just having an issue with reading data from a .txt file this is what i have so far.

    Here is the data file.

    200 1000 800
    450 845 1200
    800 250 400
    0 1500 1800
    600 500 1000
    700 1400 1700
    675 400 900

    Here is the code


    import java.util.*;
    import java.io.*;
     
    public class lab1 {
     
    	/** 
    	 *  MW 1:30 - 2:50 
    	 *  This program will read file from data and make 3 arrays that will contain
    	 *  information about the daily in take of calories for breakfast, lunch and dinner
    	 *  then compute total calories per day, average consumed each day, average calories
    	 *  consumed in each of the three meals, the max numbers of calories consumed in any specific
    	 *  day, and the max number of calories consumed in any one meal of any type.
    	 * @throws FileNotFoundException 
    	 */
     
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		File file = new File ("C:\\Users\\jfangulo\\workspace\\Lab1\\src\\data.txt");
    		Scanner in = new Scanner (file);
    		int [] breakfast = new int [7];
    		int [] lunch = new int [7]; 
    		int [] dinner = new int [7];
    		int counter = 0;
     
    		if (!(file.exists()))
    			System.out.println("No file found");
     
    		while (in.hasNext());
    		{
    			breakfast [counter] = in.nextInt();
    			lunch [counter] = in.nextInt();
    			dinner [counter] = in.nextInt();
    			counter++;
    		}
    		System.out.println(breakfast[0]);
    		System.out.println(lunch[0]);
    		System.out.println(dinner[0]);
     
    	}
     
    }

    The code just hangs. i changed the path name to File file = new File ("data.txt");
    and that didn't do anything but hang either.

    Any help is appreciated Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help reading in data

    The code just hangs
    Add some System.out.println statements in there...this will let you and us know where exactly the code 'hangs'.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help reading in data

    You have three nextInt calls for one hasNext call.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help reading in data

    while (in.hasNext());
    {
    Hmmmm.
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help reading in data

    AHHH The famous forever loop.

Similar Threads

  1. Help with Scanner class - Reading Data from a file
    By billias in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 12:07 PM
  2. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  3. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  4. Reading file Data into a "struct"
    By Brandt in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 9th, 2011, 10:02 AM
  5. Reading Chunks of Data
    By icu222much in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 22nd, 2010, 08:39 PM