hi low GUI

I have this guessing game. Z is the enter button after you click the number or letter. Y will then clear out the number and it should also start a new game. I can't get it to clear out and start a new game. When I hit 5 or f it should say Try again and then resest itself, and I can't get it to do that either. I just need it to generate 1 number and tell if it is the correct number or if it is wrong. There are not mulitple guesses.

//GUI Calculator Program

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

publicclass calcextends JFrameimplements ActionListener

{

private JTextField displayText =new JTextField(30);

private JButton[] button =new JButton[18];

private String[] keys ={"1","2","3","4",

"5","6","7","8",

"a","b","c","d",

"e","f","g","h","z","y"};

private String numStr1 ="";

private String numStr2 ="";

boolean done =false;

privatechar op;

privateboolean firstInput =true;

int num = (int) (Math.random() * 16);

public calc()

{

setTitle("My Calculator");

setSize(230, 200);

Container pane = getContentPane();

pane.setLayout(null);

displayText.setSize(200, 30);

displayText.setLocation(10, 10);

pane.add(displayText);

int x, y;

x = 10;

y = 40;

for (int ind = 0; ind < 18; ind++)

{

button[ind] =new JButton(keys[ind]);

button[ind].addActionListener(this);

button[ind].setSize(50, 30);

button[ind].setLocation(x, y);

pane.add(button[ind]);

x = x + 50;

if((ind + 1) % 4 == 0)

{

x = 10;

y = y + 30;

}

}

this.addWindowListener(new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

publicvoid actionPerformed(ActionEvent e)

{

String resultStr;//Step 1

String str = String.valueOf(e.getActionCommand());//Steps 1 and 2

char ch = str.charAt(0);//Steps 1 and 3

switch (ch)//Step 4

{

case'1':case'2':case'3'://Step 4a

case'4':case'6':case'7':

case'8':case'a':case'b':case'c'://Step 4b

case'd':case'e':case'g':

if (firstInput)

{

//numStr1 = "";

numStr1 = numStr1 + ch;

displayText.setText(numStr1);

}

break;

case'z':

resultStr = evaluate();

//Step 4c

//displayText.setText(resultStr);

if (resultStr =="1"){

displayText.setText("Correct");

done =true;

}

if (resultStr =="2"){

displayText.setText("Wrong");

}

numStr1 = resultStr;

// numStr2 = "";

firstInput =false;

break;

case'f':case'5':

displayText.setText("Try again");

resultStr = evaluate();

if (resultStr =="1"){

displayText.setText("Correct");

done =true;

}

if (resultStr =="2"){

displayText.setText("Wrong");

}

numStr1 = resultStr;

// numStr2 = "";

firstInput =false;

break;

case'y':

resultStr = clear();

break;

}

}

private String evaluate()

{

int num=0;

boolean done;

// try

// {

int num1 = Integer.parseInt(numStr1);

// int num2 = Integer.parseInt(numStr2);

String result;

//booleandone = false;

if (num1 == num)

{

result ="1";

return result;

// done = true;

}

else

if (num1 != num){

result="2";

return result;

}

else{

result="3";

return result;

}

}

private String clear(){

numStr1="";

return numStr1;

}

publicstaticvoid main(String[] args)

{

calc c =new calc();

}

}

[9782 byte] By [thunderbbolta] at [2007-11-26 22:19:02]
# 1
Looks [url= http://forum.java.sun.com/thread.jspa?threadID=5150637&messageID=9564847#9564847]familiar[/url].
kevjavaa at 2007-7-10 11:14:20 > top of Java-index,Java Essentials,New To Java...
# 2
I redid some of the code so I reposted so there was not so much to look through, didn't realize I wasn't suppose to do that
thunderbbolta at 2007-7-10 11:14:20 > top of Java-index,Java Essentials,New To Java...
# 3

What is all this about?

(You dont have to code unused cases)

switch (ch)//Step 4

{

case '1': case '2': case '3'://Step 4a

case '4': case '6': case '7':

case '8': case 'a': case 'b': case 'c'://Step 4b

case 'd': case 'e': case 'g':

TuringPesta at 2007-7-10 11:14:20 > top of Java-index,Java Essentials,New To Java...
# 4

> I redid some of the code so I reposted so there was

> not so much to look through, didn't realize I wasn't

> suppose to do that

It's okay, it's just that continuity between the various threads is a good thing.

In your event handler, you're using 'b' and 'f' as numbers... if they don't convert to integers, then you get a dump when you try to evaluate it. So, don't use the numbers, or you can convert it to hex first.

Your clear() routine doesn't work because you're just setting a string to "". You're not setting your text field to "".

kevjavaa at 2007-7-10 11:14:20 > top of Java-index,Java Essentials,New To Java...