Results 1 to 9 of 9
Thread: java skips a block of code...
- 02-06-2010, 11:51 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
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!!!
- 02-07-2010, 12:23 AM #2
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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.
- 02-07-2010, 12:35 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 02-07-2010, 08:49 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Thank you RaIdEn and pbrockway2 for responding.
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...
- 02-07-2010, 08:54 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 02-07-2010, 09:04 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
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. ^^
- 02-07-2010, 09:19 AM #7
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
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!
- 02-07-2010, 09:57 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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
- 02-07-2010, 09:58 AM #9
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
java regular expression to search block/string/word exist in a paragraph?
By sidharth in forum Advanced JavaReplies: 2Last Post: 11-11-2009, 05:56 AM -
How do I make this if-else block accept the enum types in Java?
By Jessaurum in forum New To JavaReplies: 1Last Post: 10-13-2009, 07:31 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
while loop skips code
By ejs7597 in forum New To JavaReplies: 3Last Post: 02-28-2009, 03:07 AM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks