-2

A majority of my code is working code it is just when I tried adding in a multiplication of the users input which I began to get errors. Can you give me advice how to do it better or get rid of the errors?

package resittests;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Inputting {

    public static void main(String[] args) {
        Scanner Scan = new Scanner (System.in); // Creates the Scanner to allow input

        System.out.print("Enter a Number");
        String input = null;
        String first = Scan.next();
        int num1 = Integer.parseInt(input); 

        String Second; // Second Declared as a string
        Second = JOptionPane.showInputDialog("Enter Another Number");
        String input2 = null;
        int num2 = Integer.parseInt(input2);

        System.out.print("What is your Name?");
        String name = Scan.next();

        String Age; // Age Declared as a String 
        Age = JOptionPane.showInputDialog("Enter your age ");

        System.out.print( name + " "+ "(Aged" + " " + Age +  ")" + "," + "your answer is " + (num1 * num2) );
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    "can you give me advice how to do it better or get rid of the errors" Start by reading the errors, and addressing the usually clearly stated causes for them. – ceejayoz May 20 '15 at 16:32
  • Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:542) at java.lang.Integer.parseInt(Integer.java:615) at resittests.Inputting.main(Input – adam dewhurst May 20 '15 at 16:35
  • didnt understand what this ment – adam dewhurst May 20 '15 at 16:35
  • 1) Words typed in all lower case are hard to read, like trying to listen to someone who is mumbling. Please use an upper case letter at the start of sentences, for the word I, and proper names like `ArrayList` or Oracle. 2) Always copy/paste error and exception output! 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 4) This (the problem) seems to have nothing to do with Swing! – Andrew Thompson May 20 '15 at 16:46

1 Answers1

0

Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:542)

A NumberFormatException is thrown if Integer.parseInt cannot parse the parameter String into an int - the stack trace indicates the value it attempted to parse, in this case null. The reason: the string you are passing to Integer.parseInt is null. If you want to parse the user input, then pass the user input String to Integer.parseInt, for example:

//String input = null;
String first = Scan.next();
//int num1 = Integer.parseInt(input); 
int num1 = Integer.parseInt(first); 

Note that if the user does not input a valid integer Integer.parseInt will throw a NumberFormatException

copeg
  • 8,290
  • 19
  • 28
  • so how would i get it to store the input once they have been commented out – adam dewhurst May 20 '15 at 16:46
  • I'm not exactly sure what you are asking - `first` is the variable that stores the value the user input, `num1` is a variable that is `first` converted to an `int`. – copeg May 20 '15 at 16:56