I'm trying to add a while loop to count the number of attempts made and add it to the message in my GUI. After adding the while loop the program freezes, but no errors output to the console.
I've tested it without a GUI and got it to print using System.out.print fine, but I can't get this to even allow a second input. I have to stop the code from running to quit out.
Any advice would be appreciated!
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.*;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuessingGame extends JFrame {
private JTextField txtGuess;
private JLabel lblOutput;
private int theNumber;
private int attempt;
public void checkGuess() {
String guessText = txtGuess.getText();
String message = "";
try {
int guess = Integer.parseInt(guessText);
while(guess!=theNumber) {
if (guess < 0 || guess >100) {
message = "Please enter a number between 0 and 100";
attempt++;
}
else if (guess < theNumber) {
message = guess + " is too low. Try again.";
attempt++;
}
else if (guess > theNumber) {
message = guess + " is too high. Try again.";
attempt++;
}
else {
message = guess + " is correct! It took " + attempt+ " attempts. Starting new game.";
attempt++;
newGame();
}
}
}
catch(Exception e) {
message= "Enter a whole number between 0 and 100";
}
finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}
public void newGame() {
theNumber = (int)(Math.random() * 100 + 1);
}
public GuessingGame() {
setTitle("Hi-Lo Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lblHiloGuessingGame = new JLabel("Hi-Lo Guessing Game");
lblHiloGuessingGame.setFont(new Font("Tahoma", Font.BOLD, 15));
lblHiloGuessingGame.setHorizontalAlignment(SwingConstants.CENTER);
lblHiloGuessingGame.setBounds(12, 35, 408, 16);
getContentPane().add(lblHiloGuessingGame);
JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100");
lblGuessANumber.setHorizontalAlignment(SwingConstants.CENTER);
lblGuessANumber.setBounds(58, 86, 245, 16);
getContentPane().add(lblGuessANumber);
txtGuess = new JTextField();
txtGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
txtGuess.setBounds(310, 85, 32, 19);
getContentPane().add(txtGuess);
txtGuess.setColumns(10);
JButton btnGuess = new JButton("Guess!");
btnGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
btnGuess.setBounds(163, 137, 97, 25);
getContentPane().add(btnGuess);
lblOutput = new JLabel("Enter a number and click guess");
lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
lblOutput.setFont(new Font("Tahoma", Font.ITALIC, 13));
lblOutput.setBounds(84, 197, 265, 19);
getContentPane().add(lblOutput);
}
public static void main(String[] args) {
GuessingGame theGame = new GuessingGame();
theGame.newGame();
theGame.setSize(new Dimension(450,300));
theGame.setVisible(true);
}
}
checkGuessshouldn't need a loop