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.");
    }
}

Recommended Answers

All 5 Replies

no it doesn't, just keep putting in numbers and pressing enter, it works fine.

what problem are you having?

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.

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?

Thank you. I tried clean and build new project in NetBeans. How dumb can I be!

no problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.