Results 1 to 9 of 9
- 01-21-2012, 02:50 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Stuck on a Do While logical error
Hi,
The do while loop compiles fine, its just not ending the loop as I'd like it to. Here my code (Some are calling methods from other classes) :
Ask more more questions to find out more of the problem. Thanks.
Java Code:String q = ""; do { LayoutOfQuestions layQuestions = new LayoutOfQuestions(); questions = layQuestions.getQuestionList(); for(Question currQuestion: questions) { System.out.println("This is a "+currQuestion.getType()+" question."); System.out.println(currQuestion.getName()); q = key2.nextLine(); q1.increasePoints(); //if(currQuestion.getAnswer().equalsIgnoreCase(q)) if(q1.isCorrect(currQuestion, q)) { System.out.println("Correct answer"); } else System.out.println("Incorrect. The correct answer is "+currQuestion.getAnswer()); System.out.println(currQuestion.getAnswerDescription()); } System.out.println(q1.getPoints()); }while(!q.equalsIgnoreCase("stop"));Last edited by Norm; 01-23-2012 at 02:50 PM. Reason: added code tags
- 01-21-2012, 03:09 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 19
- Rep Power
- 0
Re: Stuck on a Do While logical error
Before call q1.increasePoints(), shouldn´t you check the value of q? I mean, if it is "stop" will you still call q1.isCorrect?
- 01-23-2012, 02:43 PM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Stuck on a Do While logical error
Yes i know. Il change the statements around. Thanks. But not looking at how the code executes, when i do type in stop, it still keeps the loop executing?
Thanks again.
- 01-23-2012, 02:50 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 19
- Rep Power
- 0
Re: Stuck on a Do While logical error
You have 2 loops in your program. The first one you did with the command "do" and the second one you did with the command "for".
Even if you type "stop" when occurs "q = key2.nextLine();" the program still keeps running the "for loop" because there isn´t any instruction to get out this loop. There is only an instruction to get out the "do loop".
- 01-23-2012, 02:52 PM #5
Re: Stuck on a Do While logical error
Print out the value of q aftere you read it in to see what the computer is looking at.
System.out.println("q=" + q + "<");
- 01-23-2012, 03:00 PM #6
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Stuck on a Do While logical error
Well the for loop is inside the do while loop, so "stop" tells the compiler to finish by instructing the do while loop to stop which in turn should stop the for loop?
- 01-23-2012, 03:12 PM #7
Re: Stuck on a Do While logical error
If you are in the for loop, how will the while() see the value of q if you are still in the for loop?
- 01-23-2012, 03:15 PM #8
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Stuck on a Do While logical error
Ah i see the logic now. So the compiler is processing all the objects in the for loop and then it can "stop". So I need to figure out a way to tell the for loop also stop. Thanks guys.
- 01-23-2012, 05:05 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Logical Operator error
By MBD in forum New To JavaReplies: 2Last Post: 10-11-2011, 12:29 PM -
My program has a logical error but i cant find problem
By s0meb0dy in forum New To JavaReplies: 3Last Post: 09-26-2010, 06:29 AM -
Stuck on an error
By Peril in forum Advanced JavaReplies: 3Last Post: 03-23-2010, 06:34 AM -
Some Logical problem
By MuslimCoder in forum New To JavaReplies: 4Last Post: 03-01-2010, 08:12 AM -
stuck on same syntax error....
By Moltisanti in forum New To JavaReplies: 2Last Post: 09-01-2009, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks