Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:

896976Nov 25 2012 — edited Nov 26 2012
Hi I am new to Java and I am having problems with this code:

here is the error
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "3 "
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:492)
	at java.lang.Integer.parseInt(Integer.java:527)
	at NewJFrame.jButton_CalculateActionPerformed(NewJFrame.java:161)
	at NewJFrame.access$100(NewJFrame.java:10)
	at NewJFrame$2.actionPerformed(NewJFrame.java:61)
 
  private void jButton_CalculateActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
          int numTables = Integer.parseInt(jTextField_NumberOfTables.getText());
        int numCalc = Integer.parseInt(jTextField_NumberOfCalculations.getText());
        String operator = jComboBox_Operation.getSelectedItem().toString();
        
        String table = ""; 
        
        if (operator.equalsIgnoreCase("powers")) {
               table = CalculateTables.powersTable(numTables, numCalc);
               jLabel_TableType.setText("powers");
        }
public class CalculateTables {
    
  public static String
           powersTable(int numTables, int numCalc) {
           
           //fill temArray with powers using nested for statements
           String output = "";
       for (int i =0; i <=  numTables; i++) {
          output += i + " Tables\r\n";      
                for (int j =0; j <= numCalc; j++) {
                    output += String.format("%s ^ %s = %.2f\r\n",i,j,Math.pow(i,j)); 
                }
    output += "\r\n";
}    
 return output;    
  }
It seems to me anyway that the Integer.parseInt(jTextField_NumberOfTables.getText());
is not working
this piece of code, I an getting text from the user and I need to onverted to an integer
 
 int numTables = Integer.parseInt(jTextField_NumberOfTables.getText());
        int numCalc = Integer.parseInt(jTextField_NumberOfCalculations.getText());
This post has been answered by MrBabakishiyev on Nov 26 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2012
Added on Nov 25 2012
3 comments
8,317 views