0

I am trying to program a basic calculator which takes two numbers and adds them. First it assumes the entered numbers are integer if not, it throws and catches an exception and assumes the entered input is double. The program works fine unless the first entered number is integer and the second is double it just hangs and does nothing.

        /* Reads two numbers. Assuming the entered input is integer, Adds
         * the numbers and prints out the result If not integer, throws an
         * InputMismatchException.*/

        Scanner inputSource = new Scanner(System.in);

        try 
        {
            Integer input1, input2, result;

            input1 = inputSource.nextInt();
            input2 = inputSource.nextInt();

            result = input1 + input2;

            System.out.println("The Sum of " + input1 + " and " + input2 + " is " + result);
        }

        catch (java.util.InputMismatchException e) 
        {

            double input1, input2, result;

            input1 = inputSource.nextDouble();
            input2 = inputSource.nextDouble();

            result = input1 + input2;


            System.out.println("The Sum of " + input1 + " and " + input2 + " is " + result);
        }
Farooq Khan
  • 305
  • 1
  • 2
  • 8

2 Answers2

-1

If i am interpreting your problem correctly when you enter a double as the second number it is entering the catch block of your statement and thus you need to enter a second number. To fix this you need to separate your nextInt() method calls to check for doubles instead of having them in the same try catch block, then you should use logic to decide which two values to add together.

Beryllium
  • 546
  • 7
  • 19
-1

I am reading the input as a string and converting the entire string into double and displaying it.

This can be done in different ways by BufferedReader, try and catch blocks can be added. Hope this helps!!

import java.util.*;
import java.io.*;
public class HelloWorld
{
    public static void main(String []args)
        {
             Scanner inputSource = new Scanner(System.in);
             String input1 = null; 
             String input2 = null; 
             double result;
             System.out.println("PLease enter your input sir:");
             input1 = inputSource.nextLine();
             System.out.println("PLease enter your input sir:");
             input2 = inputSource.nextLine();
             double value1 = Double.parseDouble(input1);
             double value2 = Double.parseDouble(input2);
             result = value1 + value2;
             System.out.println("The Sum of " + input1 + " and " + input2 + " is " + result);

     }
}
JumpMan
  • 2,016
  • 3
  • 22
  • 37
  • 1
    Thanks buddy! It helped a lot. I tried this way as well but i was getting an error that input1 wasn't initialized which i didn't know how I can resolve. I am a C++er and just started learning java so don't know a lot about it. Anyways, thanks again. – Farooq Khan Sep 21 '15 at 01:24
  • I understand, Java is cool, C++ is not that easy but its high payed and very popular because of its complexcity. If you know C++, I am pretty sure, you can pick any language soon. You don't have to deal with any memory managements or garbage collections, pointers, etc in Java. All the best dude :) – JumpMan Sep 21 '15 at 01:32
  • Hi Farooq Khan, I noticed that this question is marked duplicate. Please accept this answer, since you said its working. So that this topic can be closed. Thanks!! – JumpMan Sep 21 '15 at 23:08