The Artima Developer Community
Sponsored Link

Java Answers Forum
simple number format error

4 replies on 1 page. Most recent reply: Jun 6, 2002 12:23 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

simple number format error Posted: Jun 5, 2002 5:34 PM
Reply to this message Reply
Advertisement
I am trying to use the Intfield() method and I am getting this error every time, I try to get the integer from the field. It compiles fine but doesn't load the frame, this is in the dos window.
-------------------------------------------------------
Starting the login process...
Exception in thread "main" java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java:426)
at java.lang.Integer.parseInt(Integer.java:454)
at IntField.getIntValue(IntField.java:56)
at Bank_login.<init>(Bank_login.java:13)
at Bank_login.main(Bank_login.java:85)

------------------------------------------ -------------
This is my code:
______________________________________________________
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import cs1.Keyboard;

class Bank_login extends JFrame
{

//Text Areas.
TextField bankname=new TextField();
String name=bankname.getText();
IntField bankIdentification=new IntField(20);
int ID = bankIdentification.getIntValue();

//Buttons.
JButton ok=new JButton("ok");

public Bank_login()
{
super("Login");
setSize(500,300);
setLocation(200,200);

//Screen Title.
JLabel welcome=new JLabel("Welcome to the NorthField Bank login",SwingConstants.LEFT);
welcome.setFont (new Font ("Times New Roman", Font.BOLD, 28));

//Text labels.
JLabel bankn=new JLabel("Enter the name of your bank:",SwingConstants.LEFT);
bankn.setFont (new Font ("Times New Roman", Font.BOLD, 15));
JLabel bankID=new JLabel("Enter a four digit ID number:",SwingConstants.LEFT);
bankID.setFont (new Font ("Times New Roman", Font.BOLD, 15));

//Creates a new container.
Container contentPane = getContentPane();
contentPane.setLayout(null);

//Adds Items.
contentPane.add(welcome);
contentPane.add(bankname);
contentPane.add(bankIdentification);
contentPane.add(ok);
contentPane.add(bankn);
contentPane.add(bankID);

Insets insets = contentPane.getInsets();
//Sets Item location.
welcome.setBounds(10 + insets.left, 3+ insets.top, 800, 35);
bankname.setBounds(210 + insets.left, 100+ insets.top, 200, 20);
bankIdentification.setBounds(210 + insets.left, 150+ insets.top, 200, 20);
ok.setBounds(177 + insets.left, 220+ insets.top, 100, 35);

bankn.setBounds(5 + insets.left, 94+ insets.top, 200, 35);
bankID.setBounds(5 + insets.left, 144+ insets.top, 200, 35);

//Action events.
ProActionListener actionListener = new ProActionListener();
ok.addActionListener (actionListener);

}

//Caries out all the actions of the game.
private class ProActionListener implements ActionListener
{

public void actionPerformed(ActionEvent event)
{

Object source=event.getSource();

if(source==ok)
{
Main_menu mainScreen=new Main_menu();
mainScreen.show();
}

}
}

//Main is the method that runs the program.
public static void main(String[]args)
{
System.out.println("Starting the login process...");

Bank_login loginframe=new Bank_login();
//loginframe.addWindowListener(new windowcloser());
loginframe.setVisible(true);


}

}


Rhonda Warren

Posts: 2
Nickname: rhondaw
Registered: Jun, 2002

Re: simple number format error Posted: Jun 6, 2002 10:19 AM
Reply to this message Reply
In your constructor you have a line that creates the IntField object then immediately after it you have

int ID = bankIdentification.getIntValue();

The method getIntValue() is returning an int and there is no numeric value in the textfield at this point. Therefore you will get a NumberFormatException here because you are trying to parse an empty String.

Rhonda Warren

Posts: 2
Nickname: rhondaw
Registered: Jun, 2002

Re: simple number format error Posted: Jun 6, 2002 10:45 AM
Reply to this message Reply
You'll have to forgive me. I looked at the code rather quickly. I am assuming that IntField is a custom class in your own package. That probably extends TextField. getIntValue() is a method in this class. You are declaring an as instance data an int

int ID = bankIdentification.getIntValue();

This field does not have a value in it at this point, but it is trying to parse it.

Again, I am Not sure if my assumption is right!!!!
Would probably need to see other code.

Is the 20 that you pass to the constructor of the IntField class representing the value for the field? Is it being passed to a superclass like TextField? If so that 20 would represent the length of the TextField no the value in it.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: simple number format error Posted: Jun 6, 2002 12:21 PM
Reply to this message Reply
By the way Jake, at the risk of repeating myself once again, your code will be much more readable if you use the [java] tag before it and the [/java] after it. Then people won't have as much trouble trying to visually parse it.

Here is an example without the java tags:
class Slop
{
private boolean ugly = true;

public static void main( String [] args )
{
new Slop().run();
}

public Slop()
{
}
public Slop( boolean isUgly )
{
ugly = isUgly;
}

public void run()
{
for( int i = 0; i < 1000; i++ )
{
if( ugly )
System.out.println( "This code looks like slop!" );
else
System.out.println( "This code looks absolutely lovely!" );
}
}
}

And here is the same code (almost) with the mark-up tags:
class Slop
{
   private boolean ugly = true;
 
   public static void main( String [] args )
   {
      new Slop().run();
   }
 
   public Slop()
   {
   }
   public Slop( boolean isUgly )
   {
      ugly = isUgly;
   }
 
   public void run()
   {
      for( int i = 0; i < 1000; i++ )
      {
         if( ugly )
            System.out.println( "This code looks like slop!" );
         else
            System.out.println( "This code looks absolutely lovely!" );
      }
   }
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: simple number format error Posted: Jun 6, 2002 12:23 PM
Reply to this message Reply
Oops, the second sample should have "ugly = false" but my session timed out and it seems that when your session times out, pressing "Preview" will do a "Post Message" and then ask you to log in (or maybe the same in the reverse order). That is another Jive bug to fix...

Flat View: This topic has 4 replies on 1 page
Topic: How do i protect decompile. Previous Topic   Next Topic Topic: help installing jdom!  (getting crazy)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use