newbie college progammer needs help!
ok, I'm making a basic gui for a black jack game I've made and I have a JTextField, called betAmt, made for people to input their bet (betStr), and since the textField returns a string, i have a variable, guiBet, that is set to equal the int parsed betStr:
guiBet = Integer.parseInt(betStr);
everything compiles, and when i run the window comes up and what not, but i get this error, which doesn't let the program run properly, i get a number format exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
and when i click on the green line it brings me to that first line of code i posted, could someone please tell me what i'm doing wrong? or what I'm not doing at all?
Thanks!
[758 byte] By [
Kruser14a] at [2007-10-2 11:03:46]

The problem is that the Integer.parseInt(String) method is throwing an exception because it cannot find a way to convert the String it is given into an integer.
My guess is that you are creating the window, then using getText() to get the text from the field, then converting it to an integer (is this what you are doing?).
The getText() method does not wait for the user to enter text into the field if there is none there. It just grabs whatever the text field contains. The instant the window shows up, this is an empty string. Which then gets parsed to find an int (this doesn't work).
If you want your program to respond only after a user enters text into a field, you'll need to use ActionListeners. There's a little too much material for me to explain them right here, but it gives you something to search the web for. Basically, an ActionListener will provide methods that get called when events take place involving your text field (such as the user pressing the 'enter' key in it). You can then fill in the code inside these methods to respond to actions the way you want (get the string and try to make it into an int).
Also, a classic java programming mistake is to create a listener and not register it. Make sure you register it. (You won't know what that means until you've looked action listeners up)
Even after you set this up, if a user enters a bad (non-integer) string it will still crash the program. There are many ways to fix that, but you probably don't have to worry about them now.
that's exactly what i'm doing (the getText() thing), i have an ActionListener, but i don't think i've registered it, so i will definately look that one up. I have an if statement so if the getText() input isn't an int between two numbers then a JOptionPane comes up w/ an error message. Thanks for your help Logic_bomb!
>a classic java programming mistake is to create
> a listener and not register it.
What exactly to you mean by 'registering' it? here's some code for what i've done w/ the action listener:
ok = new JButton("Bet");
ok.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
//guiBet = Integer.parseInt(betStr);
if(e.getActionCommand() == "Bet")
{
guiBet = Integer.parseInt(betAmt.getText());
if(guiBet <= moneyAmt && guiBet > 0)
{
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(this,"Your bet must be less than or equal to" + getMoneyAmt(), "Invalid Bet", JOptionPane.WARNING_MESSAGE);
}
}
}
oh and i do have the class implementing ActionListener
The addActionListener part registers it. That's what I meant. It's the part that people always forget and then they scratch their heads and wonder why it doesn't work. Most people have done it before. At least I know I have.
It all looks good to me. One little tip: be really careful about using the == operator with String objects. Using str1.equals( str2 ) is probably a better choice in most cases. This little program demonstrates why:
public class Strings {
public static void main( String args[] ) {
String s1 = "hello";
String s2 = "hello";
if( s1 == s2 ) {
System.out.println( "test 1 passed" );
}
s1 = new String( "hello" );
if( s1 == s2 ) {
System.out.println( "test 2 passed" );
}
}
}
Only the first message gets printed. In your situation == works, but it may not always do what you want. Not a big deal at all though.
ok...i'm still having problems with my program...now i am getting a NullPointerExeception, here's the code:
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand() == "Bet")
{
guiBet = Integer.parseInt(betAmt.getText());
if(guiBet <= moneyAmt && guiBet > 0)
{
setVisible(false);
}
else
{
JOptionPane.showMessageDialog(this,"Your bet must be less than or equal to" + getMoneyAmt(), "Invalid Bet", JOptionPane.WARNING_MESSAGE);
}
}
}
now the null pointer is at the guiBet = Integer.parseInt(betAmt.getText()) line...no idea why, i must have forgotten to do something...but i don't know what
guiBet should be of type 'int', which would mean it cannot be causing the problem. If it was type 'Integer', that could cause a problem.
The getText() method from a text component doesn't return null. It will always return a String as far as I know.
That narrows it down to betAmt. I don't know what type betAmt is, but has it been initialized?
SomeType betAmt = new SomeType( <arguments> );
wow....i'm an idiot, it's not that i hadn't intialized it....it's that I actually intiallized it twice...lol, thanks so much for your help logic.