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: Jumps over while-loop

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Jumps over while-loop

    Hello!

    I'm creating a program with the source code below. It doesn't work, because it seems to jump over the while-loop. The code:

    public class Ordjaktsfusk
    {
     
        public static void main(String[] args)throws FileNotFoundException, IOException
        {
            int plats2, plats3;
            String finalOrd = "";
     
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Vilka bokstäver? ");
     
            String bok = in.readLine();
     
            Scanner fil = new Scanner(new File("ord.txt"));
     
            while (fil.hasNext())
            {
                String testOrd = fil.nextLine();
     
                hittaOrd:
                if (bok.charAt(0) == testOrd.charAt(0))
                {
     
                    if(testOrd.indexOf(bok.charAt(1))>-1)
                    {
                        plats2 = testOrd.indexOf(bok.charAt(1));
                        plats3 = testOrd.indexOf(bok.charAt(2));
                    }
                    else
                    {
                        break hittaOrd;
                    }
     
                    if(plats3>plats2)
                    {
                        if (testOrd.length()>finalOrd.length())
                        {
                           finalOrd = testOrd;
                        }
                    }
                    else
                    {
                        break hittaOrd;
                    }
     
                }
     
            }
     
            System.out.println(finalOrd);
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Jumps over while-loop

    What would cause the while loop to be skipped?

    Since the while loop appears in main, we can assume for now that main is being called...
    What is the value of fil.hasNext() the first time the while loop is encountered?
    Try a print line before the while loop to see the value of fil and fil.hasNext() and see what is going on

  3. The Following User Says Thank You to jps For This Useful Post:

    iHank (April 9th, 2013)

  4. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Jumps over while-loop

    Ok, I tested "fil.hasNext" before the While-loop, and the value was "false". The value of "fil" was:

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\*][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

    I really can't figure out what is wrong. The file does exist and it is not empty.

    --- Update ---

    Ok, I tested "fil.hasNext" before the While-loop, and the value was "false". The value of "fil" was:

    java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\*][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]

    I really can't figure out what is wrong. The file does exist and it is not empty.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Jumps over while-loop

    A common cause of this problem is there is more than one version of the file present in different folders and the program is trying to read the wrong one.

    How are you executing the program?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    iHank (April 9th, 2013)

  7. #5
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Jumps over while-loop

    Okey, thanks! I'm executing the program through Netbeans - run file. I found a solution:

    I just replace Scanner fil = new Scanner(new File("ord.txt")) with Scanner fil = new Scanner(new FileReader("ord.txt")).

    Why does that work and not just with "File"? I don't have a clue.

    Another question: Do you recommend having "String testOrd = fil.nextLine()" inside the while-loop or is it another solution? A new object is created for each lap and the old ones will act as garbage in the memory. I've read that Java can sort this out by itself, but it may slow the execution down a bit.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Jumps over while-loop

    Look at the contents of the ord.txt file in a hex editor and see what the first few characters of the file is.

    What OS are you on?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. 'else if' jumps to final else.
    By Omnamah in forum Loops & Control Statements
    Replies: 9
    Last Post: December 15th, 2011, 05:36 AM
  5. Jumps over one scanner in if else loop
    By MikalD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2011, 08:34 AM