Results 1 to 9 of 9
  1. #1
    JavaSal is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default java skips a block of code...

    I can't figure out what's wrong with my code... According to all logic everything should be correct, and the compiler doesn't complain either. But the program skips a block of code.

    Here is the block that it skips for some reason:

    boolean success=false;
    while(success==false){
    int lowerBound=LasIn.läsHeltal("Enter the lower bound. ");
    int upperBound=LasIn.läsHeltal("Now enter the upper bound. ");
    if(lowerBound>upperBound){
    System.out.println("The lower bound cannot be higher than the upper"+
    "bound, silly!");
    }
    else{
    success=true;
    }
    }

    After this it just complains that lowerBound and upperBound cannot be resolved. And it never prints the "Enter the lower/upper bound.". In this code LasIn is a class that has a method called läsHeltal. These should work well and should not be the cause of the problem.

    The condition in the while-loop should trigger the loop right? As I had just set success to false... I don't see why it doesn't execute...

    Here is the full script as well: Java pastebin - collaborative debugging tool.

    Thank you for the help!!!

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Hmm i dnt see anything wrng in that loop.

    I think the problem is with your LasHeltal,
    I only found one error in your present code.

    Your closing the class with the end brack twice at the line 47 and 68.

    Try posting LasIn too. It would help.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    It's very hard to see what's going on without the code and with only negative descriptions of the program behaviour ("it never prints..." etc).

    Post a Short, Self Contained, Correct Example - with emphasis on the first "S" for short.

    -----

    As regards lower/upperBound not being resolved: that's because you declare them within the while loop block, so they are only accessible from inside that block.

    The condition in the while-loop should trigger the loop right?

    Yes: in the sense that if (1) the while loop is reached and (2) the condition evaluates to true then the block will be executed.

    This fact has a consequence. You initially claimed that "the program skips a block of code". I take this "skipping" to be a claim that the while loop was reached, that the condition expression did evaluate to true but yet the while body did not execute. The claim is false. Please put it aside and construct your SSCCE.

  4. #4
    JavaSal is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Thank you RaIdEn and pbrockway2 for responding.

    Quote Originally Posted by [RaIdEn] View Post
    Hmm i dnt see anything wrng in that loop.

    I think the problem is with your LasHeltal,
    I only found one error in your present code.

    Your closing the class with the end brack twice at the line 47 and 68.

    Try posting LasIn too. It would help.
    Yes RaIdEn, you are right, I had closed the class twice, that is now fixed.

    Here is the class LasIn. I've copied it exactly from our course book on Java, and I went through it again to check that it's exactly like in the book.
    I've also used it plenty of times yesterday(ok not a long history of using it...) and it's worked well so far.
    pastebin - collaborative debugging tool

    Yea that's what I think too, the loop should work...

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaSal View Post
    After this it just complains that lowerBound and upperBound cannot be resolved.
    What is 'it'? Your compiler?

    kind regards,

    Jos

  6. #6
    JavaSal is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What is 'it'? Your compiler?

    kind regards,

    Jos
    Sorry for being unclear, I am not very used to java yet. I use a program called Eclipse for Java. In the program I get a red ball marking an error, before even running the program. And if I then persist on running it I get an error in the box marked "console". In Eclipse there is no separate compiling and executing a program, it all happens at once in the same workspace. So for me compiling and running the program are the same thing... So I guess I mean the compiler but I'm not even sure. :S

    Also as for pbrockway2 I tried the loop on it's own, and it worked. It just doesn't work in this program. So not knowing where the problem actually is it is pretty impossible to post a SSCCE displaying the problem. I did post the full code tho, sorry that it is so long... I'll still work on figuring it out... Any suggestions are apreciated. ^^

  7. #7
    JavaSal is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Oh no, sorry for double posting, but the loop actually doesn't work on it's own. It refuses to run the program if I don't declare the variables outside the loop.
    So I really have to give them a value to begin with...
    Well it works now.
    Thank you all for helping!

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaSal View Post
    Sorry for being unclear, I am not very used to java yet. I use a program called Eclipse for Java. In the program I get a red ball marking an error, before even running the program. And if I then persist on running it I get an error in the box marked "console". In Eclipse there is no separate compiling and executing a program, it all happens at once in the same workspace. So for me compiling and running the program are the same thing... So I guess I mean the compiler but I'm not even sure. :S
    Eclipse does compile even when you type but you always have to give explicit commands to make it run your class. The difference between compilation and execution is clear: you make a class run, that's execution, everything else (except for housekeeping tasks) is compilation. Compiler errors are always shown at the position in the source text and/or in the left margin.

    kind regards,

    Jos

  9. #9
    JavaSal is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Eclipse does compile even when you type but you always have to give explicit commands to make it run your class. The difference between compilation and execution is clear: you make a class run, that's execution, everything else (except for housekeeping tasks) is compilation. Compiler errors are always shown at the position in the source text and/or in the left margin.

    kind regards,

    Jos
    Ok, I didn't know this. Thank you for making it clear.

Similar Threads

  1. Replies: 2
    Last Post: 11-11-2009, 06:56 AM
  2. Replies: 1
    Last Post: 10-13-2009, 08:31 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. while loop skips code
    By ejs7597 in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 04:07 AM
  5. Replies: 5
    Last Post: 10-17-2008, 03:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •