0

So I am trying to test a basic calculator, but the program ends right after it asks for "adding, subtracting, multiplying or dividing" - it doesn't give the user a chance to type. help appreciated please :) thanks!

import java.util.Scanner;
public class Lator {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        float num1;
        float num2;
        String choice;

        Scanner imput = new Scanner(System.in);

        System.out.println("This is a calculator.");
        System.out.println("Enter your first number.");
        num1 = imput.nextFloat();

        System.out.println("Enter your second number.");
        num2 = imput.nextFloat();

        System.out.println("Would you like to add, subtract, divide, or multiply?");
        choice = imput.nextLine();

        if(choice.equals("Add")||choice.equals("+")||choice.equals("Addition")) {
        System.out.println("Number1 " + num1 + " + " + "number2" + num2 + "=" + (num1 + num2));

        }
    }
}
Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702

5 Answers5

3

Use the following code. Comments in the code explain the answer.

import java.util.Scanner;

    public class Lator {

        public static void main(String[] args) {
            float num1;
            float num2;
            String choice;

            Scanner imput = new Scanner(System.in);

            System.out.println("This is a calculator.");
            System.out.println("Enter your first number.");
            num1 = imput.nextFloat();

            System.out.println("Enter your second number.");
            num2 = imput.nextFloat();

            imput.nextLine(); //ADDED LINE

            System.out.println("Would you like to add, subtract, divide, or multiply?");
            choice = imput.nextLine();

            if(choice.equals("Add")||choice.equals("+")||choice.equals("Addition")) {
            System.out.println("Number1 " + num1 + " + " + "number2" + num2 + "=" + (num1 + num2));

            }
        }
    }

We added imput.nextLine(); because imput.nextFloat(); leaves a leftover new line character that we need to clear out before scanning for the user's selection.

It was putting this new line character as the selection when the user is supposed to enter "Add". Let me know how this works out for you. See a similar answer here, Java For Loop Issue

-Henry

Community
  • 1
  • 1
Dummy Code
  • 1,818
  • 4
  • 19
  • 38
0

Change the following line:

choice = imput.nextLine();

to:

choice = imput.next();

This stores what the user writes in the String 'choice'.

You can test it by adding this as the next line:

System.out.println(choice);

Edit: This will only get the next word. As per comments, if you were to expand your application to handle input like Square Root or something like that, you would want to go to the next line first:

imput.nextLine();
System.out.println("Would you like to add, subtract, divide, or multiply?");
choice = imput.nextLine();

The reason for needing to go to the next line is that the Scanner 'imput' is still on the previous line, just after the second float entered by the user.

I think that the best option for your application is to use my first example though and use imput.next(); because your application only uses one word inputs. Using imput.nextLine(); could possibly expand the possibility of input errors, because it gets the whole line.

  • Thanks, that worked. :) Why exactly would .nextLine(); not work though, I thought .nextLine(); would work for all strings? thanks again – Ghetto Man Jul 08 '13 at 20:26
  • @GhettoMan I changed my answer to reflect this question, Henry Harris' answer works in this manner. It's up to you which one you use, depending on the needs of the application. – Patrick Sebastien Jul 08 '13 at 20:33
  • @Henry Harris I actually think using imput.next(); is a better idea for this application. Right now, the application only needs one word. Using only next() instead of nextLine() prevents possible errors from input. – Patrick Sebastien Jul 08 '13 at 20:39
0

Not Related to Question but just an observation (unless this is a simplified version of course!) i.e. I misread the question

The program will just end after your last next..() call as it computes what it needs to then exits as the program has finished.

Add something to 'pause' the execution of the program such as

 // After calculating
 System.out.println("Press any key to continue");
 imput.next();
 // continue with what you want.
Java Devil
  • 10,281
  • 7
  • 31
  • 46
0

you could use this code

import java.util.Scanner;
public class Lator {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        float num1;
        float num2;
        String choice;

        Scanner imput = new Scanner(System.in);

        System.out.println("This is a calculator.");
        System.out.println("Enter your first number.");
        num1 = imput.nextFloat();

        System.out.println("Enter your second number.");
        num2 = imput.nextFloat();

        System.out.println("Would you like to add, subtract, divide, or multiply?");
        choice = imput.next();

        if(choice.equals("Add")||choice.equals("+")||choice.equals("Addition")) {
        System.out.println("Number1 " + num1 + " + " + "number2 " + num2 + " = " + (num1 + num2));

        }
    }
}

Use choice.equalsIgnoreCase("add") instead of choice.equals("Add")

Anirban Nag 'tintinmj'
  • 5,392
  • 4
  • 35
  • 51
0
import java.util.*;
class Calculate
 {public static void main(String args[])
 {int a,b,choice;
 Scanner sc=new Scanner(System.in);
   System.out.println("enter numbers");
   a=sc.nextInt();
   b-sc.nextInt()
   System.out.print("1.add\n2.sub\n3.mul\n4.div\n5.mod");
    System.out.println("enter choice");
    choice=sc.nextInt();
 do
 {
switch(choice)
{case 1:System.out.println("result is" +(a+b));
     break;
case 2:System.out.println("result is" +(a-b));
     break;
case 3:System.out.println("result is" +(a*b));
     break;
case 4:if(a>b)
   { System.out.println("result is" +(a/b));
     break;}
  else { System.out.println("invalid");
   break;}
 case 5:System.out.println("result is" +(a%b));
     break;
   default: System.out.println("invalid");
   break;
        }
        }
      while(choice!=6);
       }
        }