Ghetto Man
Ghetto Man

Reputation: 1

Basic Calculator Error

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));

        }
    }
}

Upvotes: 0

Views: 338

Answers (6)

Zeeshan Malik
Zeeshan Malik

Reputation: 1

import java.util.Scanner;

public class Calculator {

 public static void main(String[] args){
 
 System.out.println("Hello user welcome to calculator");
 

 Scanner sc = new Scanner(System.in);
 char sign;
 
do{
 System.out.println("Enter a sign + or - or * or /:");

  sign = sc.next().charAt(0);
 }
 while(sign != '+' && sign!='-' && sign != '*' && sign != '/');

 
 System.out.println("Enter first number :");
 
 double num1 = sc.nextDouble();
 
 System.out.println("Enter second number :");
 
 double num2 = sc.nextDouble();

 if(sign == '+'){
 
double result = num1 + num2;

System.out.printf("The addition of the two numbers is : %,f", result );



 }
 else if (sign == '-'){
 
double  result = num1 - num2;

 System.out.printf("The subtraction of the two numbers is : %,f", result );
 
 
 }
 else if (sign == '*'){
 
 double result = num1 * num2;
 
  System.out.printf("The multiplication of the two numbers is : %,f", result );
 
 
 }
 
  else if (sign == '/'){
 
 double result = num1 / num2;
 
  System.out.printf("The division of the two numbers is : %,f", result );
  }
  else
     
      System.out.println("Please enter a valid sign ");

 }              

}

Upvotes: 0

Sakshi Naik
Sakshi Naik

Reputation: 1

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);
       }
        }

Upvotes: 0

Patrick Sebastien
Patrick Sebastien

Reputation: 342

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.

Upvotes: 0

Java Devil
Java Devil

Reputation: 10969

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.

Upvotes: 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")

Upvotes: 0

Dummy Code
Dummy Code

Reputation: 1898

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

Upvotes: 3

Related Questions