Results 1 to 10 of 10
  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question While Loop is being Glitchy

    Hello Fellow Java Makers,

    I have made this program in Java which is supposed to ask you to press the letter Z into the console. If you press the letter Z, the code ends, and if you press any other letter than the letter Z, the while loop will start over again asking you to press the letter Z.
    Right now, the part where you press the letter Z is working fine, but if you press any other letter than Z, the code will go to a weird infinite loop.
    Please give me advice/guide me to fix this situation.

    Thank you
    - Simeon

    Java Code:
    public class WhileLoop {
    
    	public static void main(String[] args)
    			throws java.io.IOException {
    		
    			System.out.println("Please Type the letter Z.");
    			// Getting the input 
    			char userInput  = (char) System.in.read();
    			
    			// If user presses letter Z
    			if (userInput == 'Z') {
    				System.out.println("The code will now self distruct");
    			}
    			else {
    				// The while loop to loop if user does not press Z
    				while (userInput != 'z') {
    					System.out.println("PRESS Z!");
    				}
    			}
    		
    
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: While Loop is being Glitchy

    the code will go to a weird infinite loop.
    A while loop will execute forever if the condition to end the loop is never changed. Make sure any while loop has code inside of it that changes the condition for the loop.

    You need to rework the logic for the program so it does what you want.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: While Loop is being Glitchy

    Thanks! Can you give me an example?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: While Loop is being Glitchy

    Java Code:
      int i = 1;
      while(i == 1) {
        i = 2; // change i so the loop will exit
        // the loop will go forever if i is not changed.
      } // end while()
    Can you make a list of the simple, detailed steps your code needs to do to solve your problem?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: While Loop is being Glitchy

    1. Asks user to press the letter Z
    - If user presses letter Z, then the code stops
    - If the user does not press letter Z, then the console will tell you to press letter Z and keep on telling you if you don't.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: While Loop is being Glitchy

    If it must be 'Z', then why are you checking for 'z' ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: While Loop is being Glitchy

    Think about this: read your code and count the number of times you actually read the input from the keyboard. That's exactly once. If you write loops that need to exit one day, then, as Norm said, make sure you to get the keyboard input again. The variable that lets the loop exit isn't going to change itself you know :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: While Loop is being Glitchy

    If the user does not press letter Z,
    then the console will tell you to press letter Z
    and keep on telling you if you don't.
    What does this mean: if you don't?
    The "keep on" implies a loop. What does the program do for this?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: While Loop is being Glitchy

    If you do not type the letter Z, the console will keep on asking you until you do.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: While Loop is being Glitchy

    Quote Originally Posted by SimeonTG View Post
    If you do not type the letter Z, the console will keep on asking you until you do.
    But your program doesn't ask for new input, i.e. lines #16 and #17 keep on checking the previous input ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. While Loop inside a While Loop
    By steveday77 in forum New To Java
    Replies: 3
    Last Post: 05-10-2013, 01:43 AM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 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
  •