Cheryl Hill

Greenhorn
+ Follow
since Apr 25, 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 Cheryl Hill

Kate... have you gotten this to work yet? Please let me know if you do. I would love to have this code to play with.
17 years ago
Below are two java classes
This programming compiles just fine but when you run it you get an OutOfMemoryError. Can anyone tell me why this would be happening?


*Program window display

*/
//
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 );
}
}

=======================================================================
//Scanner Utility
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;


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 JLabel label7;
//private JLabel label8;
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 );

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 = new JLabel( "That must not be a number. Please enter an integer." );
add( label3 );
}
}

//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 = new JLabel( Guess + " is too low. Try again!" );
add( label4 );

if (Guess > Num)
label5 = new JLabel( Guess + " is too high. Try again!" );
add( label5 );

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




}
}
=========================================
17 years ago