Results 1 to 9 of 9
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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

  2. #2
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default 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?

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  4. #4
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default 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".

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default 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 + "<");

  6. #6
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default 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?

  8. #8
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Stuck on a Do While logical error

    It's not the compiler.
    It's the JVM.
    The compiler simply creates the byte code (ie the class files).

Similar Threads

  1. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 12:29 PM
  2. Replies: 3
    Last Post: 09-26-2010, 06:29 AM
  3. Stuck on an error
    By Peril in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 06:34 AM
  4. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 08:12 AM
  5. stuck on same syntax error....
    By Moltisanti in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 04:26 AM

Posting Permissions

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