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

Thread: Reading a text file + Nested Loop

  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 a text file + Nested Loop

    Hello. I have asked other questions about this, but keep running into more things I do not understand.

    The above blocks of text (Each Unit), carries on until Unit 13.
    I have the following code, but I keep getting errors. I want the loop to start when it reads the word "Unit" and stop when it reads a "Blank Line".

    I do appreciate anyones help. I am very new to this, and it is causing me major stress and frustration

    Thanks again.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reading a text file + Nested Loop

    And how many times have you been told to post your errors. Of if if the code is not providing the desired results, give sample runs and describe what needs to change.

  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 a text file + Nested Loop

    Do you mean this?

    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at packagename.Task_3.main(Task_3.java:43)

    Line 43 is
    if (inFile.next() == "Unit")

    I am sorry. I didn't realise you wanted that. I just commented saying I got an error in that "Unit" line. Thought that was enough.

    What do you mean give sample runs? :S

    I do appreciate your help. I am trying.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reading a text file + Nested Loop

    Yes. It's important for us to see the error to understand the problem and to explain to you what it means so that you don't need us anymore. Otherwise, we're suggesting fixes, you don't know why, and you haven't gotten anything from the experience.

    In this case, from the last line of what you posted, something's going wrong in line 43 of the main() method of the class Task_3 in the package packagename. The something wrong is that there is no element being found for the next() method, like maybe the end of file has been reached or a blank line. You might try fixing this by using the nextLine() method rather than next() to find blank or empty lines. For lines in the text file with multiple 'tokens', use the nextLine() method and split the tokens into a String array using the String.split() method. There are other options, too, but (IMO) this is the easiest.

    You have another flaw with the line you posted, and that is you shouldn't use the comparison operator, '==', to compare Strings. Use the equals() method instead.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading a text file + Nested Loop

    Another suggestion: read the String into a variable and then test its contents. The posted code doesn't save what was read so if you want to use it for something, it is gone.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    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 a text file + Nested Loop

    Thank you for your advice. I will try all of the things mentioned and get back to you. I thought learning Spanish was hard, Java is something else.

  7. #7
    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 a text file + Nested Loop

    Quote Originally Posted by GregBrannon View Post
    array using the String.split() method. There are other options, too, but (IMO) this is the easiest.
    Is there a way of doing it without using an Array?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading a text file + Nested Loop

    Put the String into a Scanner class constructor and use the Scanner class's methods to test for and read the tokens.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  2. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  3. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  4. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  5. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM

Tags for this Thread