Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Problem in while loop

    Hi

    In the following code, there is a problem with the while loop which is when I enter a number and press button, it executes once:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class TryJframe extends Frame implements WindowListener,ActionListener {
    	
    	Label lbl = new Label ("Hello     ");
    	TextField txt = new TextField(10);
    	Button b = new Button("Click me");
    	Random rand = new Random();
    	int temp;
    	
    	public static void main(String[] args) {
    		
    		TryJframe myWindow = new TryJframe("My first window");
    		
    		int frameWidth = 200;
    	    int frameHeight = 100;
    	    
    	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	    myWindow.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);
    		myWindow.setVisible(true);
    		
    		
    	}
    
    	public TryJframe(String title) {
    		super(title);
    		setLayout(new FlowLayout());
    		addWindowListener(this);
    		add(b);
    		add(lbl);
    		add(txt);
    		b.addActionListener(this);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		String tt="";
    		int t=0;
    		temp = rand.nextInt(100);
    		int Stop=0;
    		if(e.getSource()==b){
    		while(Stop==0){
    			
    			tt = txt.getText();
    			t = Integer.parseInt(tt);
    			b.disable();
    			if(t==temp){
    				lbl.setText("Perfect");
    				Stop=1;
    			}else if(t<temp){
    				lbl.setText("Smaller");
    							
    			}else{
    				lbl.setText("Larger");
    				
    			}
    			txt.setText(" ");
    			
    		}
    		}
    
    	}
    
    	public void windowClosing(WindowEvent e) {
    		dispose();
    		System.exit(0);
    	}
    
    	public void windowOpened(WindowEvent e) {}
    	public void windowActivated(WindowEvent e) {}
    	public void windowIconified(WindowEvent e) {}
    	public void windowDeiconified(WindowEvent e) {}
    	public void windowDeactivated(WindowEvent e) {}
    	public void windowClosed(WindowEvent e) {}
    
    }
    I am thinking about the while loop, there is a problem the loop. It executes once then it will not take the new number and compare it with the random because of the button :confused:

    I am really confused. It seems easy !!

    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    How do you know it only executes once?
    When is the user supposed to enter a different value in the text field?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why use a loop? Using a GUI already tends to work how you would like. Just make it so it checks to see if the guess == the randomly chosen number. Perhaps an instance variable will be helpful for the random number. When the number is guessed correctly you can change the random number to be guessed.

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

    Default

    Quote Originally Posted by Junky View Post
    How do you know it only executes once?
    When is the user supposed to enter a different value in the text field?
    I entered a number then it gave me "smaller" when I tried to enter another number it didn't do anything.

    The user is supposed to enter a different value if he gets "smaller" or "larger" and he has to do that until he get "perfect".

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    More nitpicks

    When do you enable the button again?
    Why use an if statement? Since your button is the only component you attached a listener to it is impossible for that if statement to be false.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    I entered a number then it gave me "smaller" when I tried to enter another number it didn't do anything.
    That is because you are stuck in an infinite loop. Try adding a print statement inside the loop and watch it goooooooooooooooooo!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Using a loop in a GUI listen to repeatedly receives user input is a bad choice to me. Try to see what happens in your code when you hit enter by adding a print statement like this

    Java Code:
    int i = 0; //put this before the loop
    ...
    While loop
    System.out.println("I = " + i + "\n");
    Do this and run it and see what happens.
    Another consideration, when does the inside of the loop actually prompt for input? Does it? It looks like it merely extracts and parses the value regardless of what it is.

    It may be a fairly generally statement but I can't think of anything that proves it false. Using a loop in an action listener should not rely on user input, it should do something like loop through and read a file. There are times however when it can rely on input but it's rare.
    Last edited by sunde887; 04-21-2011 at 01:23 AM.

  8. #8
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    ok guys. I replaced the while loop with :

    Java Code:
    public void actionPerformed(ActionEvent e) {
    		String tt=txt.getText();
    		int t=Integer.parseInt(tt);
    		int temp1=10;	
    		if(t==temp1){
    				lbl.setText("Perfect");
    				b.disable();
    		}else if(t<temp1){
    				lbl.setText("Smaller");
    				txt.setText(" ");	
    				
    		}else{
    				lbl.setText("Larger");
    				txt.setText(" ");
    				
    		}
    		tt="";
    		t=0;
    	}
    and it still gives the same problem.

    Just to explain the idea behind this code:
    The user will guess a number until he gets the number that is equal to the random number.

Similar Threads

  1. Problem with while loop
    By sassy1 in forum New To Java
    Replies: 12
    Last Post: 03-24-2011, 11:01 AM
  2. for loop problem
    By donal-ctn in forum JCreator
    Replies: 1
    Last Post: 01-31-2011, 07:26 PM
  3. Problem with a for loop
    By ile4 in forum New To Java
    Replies: 3
    Last Post: 12-02-2010, 02:23 PM
  4. For Loop Problem
    By saqib15 in forum New To Java
    Replies: 6
    Last Post: 02-20-2010, 12:59 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 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
  •