Kate King

Greenhorn
+ Follow
since Apr 26, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kate King

Wow. Okay, I see.

If I don't remove the brackets, it won't incorporate the user's remaining guess input, will it?
17 years ago
Sorry, I am not sure where you are referring to. Within the ActionListener?

Thanks!
17 years ago
I guess, I would want to go ahead and remove the brackets at the end, so that the action listener incorporates the following inputs right?

guessInput.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )
{
Guess = Integer.parseInt(guessInput.getText() );
} // remove?
} // remove?
17 years ago
Thanks a million for all of your suggestions! This is really starting to make a little more sense to me!

I did add the text field and an action listener. I am now getting an error message in the last bracket of the Action Listener. I am not quite sure what to do. Your expert advice is greatly appreciated!

public class GuessLabel
{

public static void main( String args[] )
{
Guess guess = new Guess();
guess.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
guess.setSize( 250, 200 );
guess.setVisible( true );
}
}
___________________________________________________________________________

import java.util.Scanner;
import java.util.NoSuchElementException;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



public class Guess extends JFrame
{
// Java Random Utility -
final int Num = new java.util.Random().nextInt(1001);
Scanner input = new Scanner( System.in);

int Guess;
int playagain;

private JLabel instructionLabel;
private JTextArea instructions;
private Container contentPane;
private JTextField guessInput;
private int previousDistance;
private int currentDistance;




public Guess()
{
super( "Kate's Guessing Game" );

contentPane = getContentPane();
contentPane.setBackground( Color.WHITE );
contentPane.setLayout( new FlowLayout() );
setBackground( Color.WHITE );

previousDistance = 0;
currentDistance = (Guess - Num );

instructionLabel = new JLabel( "The computer has selected a random number between 1 and 1000. Go ahead, see if you can guess it!" );
add( instructionLabel );

guessInput.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent event )
{
Guess = Integer.parseInt(guessInput.getText() );
}
}

while ( true )
{
try
{

instructions = new JTextArea( "Please enter your guess: " );
contentPane.add( instructions );

JTextField guessInput = new JTextField( 12 );
add( inputJTextField );

// If the guess is less than 1, more than 1000, continue to the next guess
if (Guess >= 1 && Guess <= 1000)
break;
}

//Exception Handling- Only accept Integer values from 1-1000
catch ( NumberFormatException nfe )
{
instructions.setText( "That must not be a number. Please enter an integer." );

}
catch ( NoSuchElementException elementException )
{
instructions.setText( "Invalid input. Please try again." );
input.nextLine();
}
}

//If / Else Guess Results- Too high, or too low
if ( previousDistance != 0 )
{
if ( currentDistance == previousDistance )
guessInput.setBackground( Color.WHITE );

else if ( currentDistance < previousDistance )
guessInput.setBackground( Color.RED );

else
inputJTextField.setBackground( Color.BLUE );
}

if (Guess < Num)
instructions.setText( Guess + " is too low. Try again!" );


if (Guess > Num)
instructions.setText( Guess + " is too high. Try again!" );


if ( Guess != Num )
instructions.setText( "Congratulations! The number was " + Num + "." );





}
}
17 years ago
Wow. I should have caught that. Thanks a million for all of the help. I have made some changes to my code, including your advive. I attempted to compile and run, however, I received a "NoSuchElement" error. So, I added exception handling code to handle the error, however, now I receive the NullPointerException, in line 97 and 13 in main. I am not quite sure how to correct this error.

Thanks!
~Kate


import javax.swing.JFrame;

public class GuessLabel
{

public static void main( String args[] )
{
Guess guess = new Guess();
guess.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
guess.setSize( 250, 200 );
guess.setVisible( true );
}
}
____________________________________________________________________________


import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.util.NoSuchElementException;


public class Guess extends JFrame
{
// Java Random Utility -
final int Num = new java.util.Random().nextInt(1001);
Scanner input = new Scanner( System.in);
int Guess;
int playagain;


private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
private JLabel label6;
private JTextField inputJTextField;



private int previousDistance;
private int currentDistance;


public Guess()
{
super( "Kate's Guessing Game" );
setLayout( new FlowLayout() );

previousDistance = 0;
currentDistance = (Guess - Num );

label1 = new JLabel( "The computer has selected a random number between 1 and 1000. Go ahead, see if you can guess it!" );
add( label1 );


while ( true )
{
try
{

label2 = new JLabel( "Please enter your guess: " );
add( label2 );
Guess = input.nextInt();
JTextField inputJTextField = new JTextField( 12 );
add( inputJTextField );

// If the guess is less than 1, more than 1000, continue to the next guess
if (Guess >= 1 && Guess <= 1000)
break;
}

//Exception Handling- Only accept Integer values from 1-1000
catch ( NumberFormatException nfe )
{
label3.setText( "That must not be a number. Please enter an integer." );

}
catch ( NoSuchElementException elementException )
{
System.err.println( "Invalid input. Please try again." );
input.nextLine();
}
}

//If / Else Guess Results- Too high, or too low
if ( previousDistance != 0 )
{
if ( currentDistance == previousDistance )
inputJTextField.setBackground( Color.WHITE );

else if ( currentDistance < previousDistance )
inputJTextField.setBackground( Color.RED );

else
inputJTextField.setBackground( Color.BLUE );
}

if (Guess < Num)
label4.setText( Guess + " is too low. Try again!" );


if (Guess > Num)
//Line 97 label5.setText( Guess + " is too high. Try again!" );


if ( Guess != Num )
label6.setText( "Congratulations! The number was " + Num + "." );


}
}
17 years ago
Is the input portion wrong as well??


label2 = new JLabel( "Please enter your guess: " );
add( label2 );
// Will this accept input?
JTextField inputJTextField = new JTextField( 12 );
add( inputJTextField );
17 years ago
Thanks for the help!

So, instead of

add( label2 );

You could
text.setText( label2 );

Would that be correct?
17 years ago
I am not quite sure how this would be corrected. How would you eliminate the memory error, without eliminating the add method?
17 years ago