my assignment was to create a calculator using either if statements ir switch methosd i opted for the switch method. everythign compiles but when I type in two integers this is what comes up:Enter two integers to be calculated (separate using space):
2 3
Exception in thread "main" java.lang.NumberFormatException: For input string: "2
3"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at lhlCalculator1.main(lhlCalculator1.java:17)
Press any key to continue . . .

here is my code:

import java.io.*;
import java.util.*;

public class lhlCalculator1
{

 public static void main(String[] args) throws Exception
 {

 BufferedReader insert = new BufferedReader(new InputStreamReader(System.in)) ;

 int numberx, numbery;

 System.out.println("Enter two integers to be calculated (separate using space):");


 numberx = Integer.parseInt(insert.readLine());
 numbery = Integer.parseInt(insert.readLine());

 System.out.println("1. Add");
 System.out.println("2. Subtract");
 System.out.println("3. Multiply");
 System.out.println("4. Divide");
 System.out.println("enter your choice:");

 int insertion= Integer.parseInt(insert.readLine());
 switch (insertion){
 case 1:
 System.out.println("Enter the number one=" + (numberx+numbery));
 break;
 case 2:
 System.out.println("Enter the number two=" + (numberx-numbery));
 break;
 case 3:
 System.out.println("Enetr the number three="+ (numberx*numbery));
 break;
 case 4:
 System.out.println("Enter the number four="+ (numberx/numbery));
 break;
 default:
 System.out.println("Invalid Entry!");
 }






 }
}

Line 17 readLine returns "2 3", so parseInt throws an error. Your code would work if you typed one number on each line, or if you used an input parser that has methods like nextInt

oh wow thank you so much!!

Line 17 readLine returns "2 3", so parseInt throws an error. Your code would work if you typed one number on each line, or if you used an input parser that has methods like nextInt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.