User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,637 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,193 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 218 | Replies: 5 | Solved
Reply
Join Date: Jul 2008
Posts: 3
Reputation: Moth126 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Moth126 Moth126 is offline Offline
Newbie Poster

Some Help Please

  #1  
Jul 20th, 2008
I'm having a problem with my assginment. This is the Java Number Guessing Game. It compiles and runs with no errors but when executed it stops with only one user input. You must hit the "Play Again" button to continue. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.*;


public class GuessNumber extends JApplet implements ActionListener{
	private String titleStr="I have a number between 1 and 1000. Can you guess my number?";
	private JLabel title=new JLabel(titleStr);
	private JLabel ask=new JLabel("Please enter your first guess: ");
	private JTextField guess=new JTextField();
	private JLabel resultLabel=new JLabel("Result: ");
	private JLabel result=new JLabel();
	private JButton replay=new JButton("Play again");
	Container cp=getContentPane();
	private int randomNum;
	public void init() {
		randomNum=new Random().nextInt(1000)+1;
		guess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str=e.getActionCommand();
				try {
					int guessNum=Integer.parseInt(str);
					if (guessNum<randomNum) {
						result.setText("Too Low");
						guess.setBackground(Color.BLUE);
					}
					if (guessNum>randomNum) {
						result.setText("Too High");
						guess.setBackground(Color.RED);
					}
					if (guessNum==randomNum) {
						result.setText("Correct!");
						guess.setBackground(Color.WHITE);
						guess.setEditable(false);
					}
				} catch (Exception ex) {
					result.setText(ex.toString());
				}
			}
		});
		replay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				randomNum=new Random().nextInt(1000)+1;
				guess.setEditable(true);
				guess.setText("");
				guess.setBackground(Color.WHITE);
				result.setText("");
			}
		});
		JPanel midScreen=new JPanel();
		midScreen.setLayout(new GridLayout(2,2));
		midScreen.add(ask); midScreen.add(guess);
		midScreen.add(resultLabel); midScreen.add(result);
		cp.setLayout(new FlowLayout());
		cp.add(title);
		cp.add(midScreen);
		cp.add(replay);
	}
	public static void main(String[] args) {
		JApplet applet = new GuessNumber();
		JFrame frame = new JFrame("Guess my number");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane().add(applet);
		frame.setSize(400,150);
		applet.init();
		applet.start();
		frame.setVisible(true);
	}

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Some Help Please

  #2  
Jul 20th, 2008
no it doesn't, just keep putting in numbers and pressing enter, it works fine.

what problem are you having?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Moth126 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Moth126 Moth126 is offline Offline
Newbie Poster

Re: Some Help Please

  #3  
Jul 20th, 2008
Originally Posted by sciwizeh View Post
no it doesn't, just keep putting in numbers and pressing enter, it works fine.

what problem are you having?


I had hoped that the user would backspace and enter a new number. As it works now the user can only continue to input numbers after each previouis number until it throw the exception.
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Some Help Please

  #4  
Jul 20th, 2008
huh? backspace and entering a new number works fine, it gives the too high or too low message every time i press enter, and when i get it right it displays correct, hit new game it picks a new number, close the window exits the VM
i cannot reproduce what your saying, although, i would suggest clearing the text area when the user presses enter and adding a guess counter, just because i would you don't have to,

have you tried stepping through it in the debugger?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Moth126 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Moth126 Moth126 is offline Offline
Newbie Poster

Re: Some Help Please

  #5  
Jul 20th, 2008
Thank you. I tried clean and build new project in NetBeans. How dumb can I be!
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Some Help Please

  #6  
Jul 20th, 2008
no problem
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 10:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC