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

Thread: Reading until a Blank Line. File input.

  1. #1
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Reading until a Blank Line. File input.

    Hello, I am wondering if someone can help me with a problem I have.

    I am using the hasNext() method to read from a file. I have multiple parts in the text file.

    Shop A
    NoOfStaff
    HoursWorked GrossPay

    Shop B
    NoOfStaff
    HoursWorked GrossPay

    Then it goes onto Shop C, D, E, etc.

    All the units have different amounts of staff, different hours and a diifferent wage.

    I have got it to read the file, the number of staff, number of hours, and pay rate, but I want it to stop reading at the empty line, or the next TEXT line... so I can manipulate data for the next Shop Unit. The main question is do I use a break statement after the first loop as I want it to repeat for each Shop Unit.

    Thanks for reading.

    - @iCurtisIT


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Reading until a Blank Line. File input.

    Can you post the code you are asking questions about?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading until a Blank Line. File input.

     
    		while (inFile.hasNextDouble())
    		{
    			if (inFile.hasNext())
    			{	
    			number_of_staff = inFile.nextDouble();
    			hours_worked = inFile.nextDouble();
    			pay_rate = inFile.nextDouble();
    			hours_worked = hours_worked + inFile.nextDouble();
    			pay_rate = pay_rate + inFile.nextDouble();
    			}
    		}

    I don't know if this is correct. I have been reading this Java book I have and I have been searching online, but I can't seem to work it out.

    - @Cu

    EDIT: Thanks for your reply.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Reading until a Blank Line. File input.

    There are 4 extra calls to nextDouble() in the if() that tested if the input had ONE MORE input.

    Some of the calls to nextDouble() replace the values in variables that were read by an earlier statement.

    In other words, the posted code does not make sense.

    Can you make a list of the steps describing what the code is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member iCurtisIT's Avatar
    Join Date
    Oct 2013
    Location
    UK
    Posts
    22
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Reading until a Blank Line. File input.

    Sorry if it does not make sense, I am sort of new to all of this.

    I have to do calculations with the data from the input file. The input file is laid out as:

    Shop A (Shop A)
    NoOfStaff (6)
    HoursWorked (40) GrossPay (7.5)

    Shop B (Shop B)
    NoOfStaff (8)
    HoursWorked (32) GrossPay (6.5)

    I want to take data from Shop A, manipulate it, then (break), then do the same onto Shop B, then Shop C.

    As you can see above there is a blank line between each shop. I was thinking maybe there is some code to recognize this and stop there, or something of the sort.

    Thanks again for your reply. I appreciate it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Reading until a Blank Line. File input.

    The nextLine() method should return an empty String when the empty line is read.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. File reading from a specific point on a line
    By lordofrandom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 13th, 2013, 10:25 PM
  2. Re: Reading a file line by line using the Scanner class
    By rajaveluswamy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2013, 03:38 AM
  3. [SOLVED] reading a file line by line
    By shenaz in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 20th, 2013, 11:10 AM
  4. Trouble Reading Line in File
    By Mandraix in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 4th, 2011, 10:47 PM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM