I'm trying to make a calculator where the person can continue to put in numbers like "2+4*7-1" until they press = and then they will get the answer, and I have no idea how to even start. I know how to make a calculator with just 2 numbers but not how to have the user giving new numbers all the time. If anyone have any tips/code I could look at that would help a lot.
Check this Creating a Calculator using JFrame , and this is a step to step tutorial
yes yes i know that i am replying after 2 years but still maybe it will come in handy to other ppl in the future.
its a simple console code no gui.
So here's how i did it on eclipse
import java.util.Scanner;
public class Adv_calc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option;
double num1, num2, result;
result = 0;
do {
System.out.println("Welcome to The Calculator app");
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.println("4) Continue");
System.out.println("5) Exit");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 4: {
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
}
break;
}
case 5: {
System.out.println("Thank you for using my program :: ");
System.out.println("Program will now exit ");
System.exit(0);
}
}
} while (option != 5);
}
}
Related
I incorporated a couple different methods I've seen on here. Does anyone know how to fix this problem I am having? When you use this code, It asks for you to enter the mathematical operator, BUT when I do if I enter +9 or -%, it will still work and use the first symbol. I want it to give an error if someone inputs */ instead of just *. I even tried switching the case to numbers (ie case 1:) and it will do the same thing if I set addition to 1 and if I enter 15, it will read the one and do addition. Any ideas?
import java.util.Scanner;
public class javacalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double num1, num2, answer;
char userOperation;
System.out.print("Enter your full name: ");
String userName = input.nextLine();
System.out.println("+ Addition");
System.out.println("- Subtraction");
System.out.println("* Multiplication");
System.out.println("/ Division");
System.out.println("% Modulus");
System.out.println("\n");
System.out.print("Enter mathematical operator e.g. + for Addition: ");
userOperation = operation.next().charAt(0);
boolean invalidOperator = false;
switch (userOperation) {
case '+':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 + num2;
System.out.print(num1 + " + " + num2 + " = " + answer);
break;
case '-':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 - num2;
System.out.print(num1 + " - " + num2 + " = " + answer);
break;
case '*':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 * num2;
System.out.print(num1 + " * " + num2 + " = " + answer);
break;
case '/':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 / num2;
System.out.print(num1 + " / " + num2 + " = " + answer);
break;
case '%':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 % num2;
System.out.print(num1 + " % " + num2 + " = " + answer);
break;
default:
System.out.println("Invalid operator!");
break;
}
}
}
Let us break this down: operation.next().charAt(0);
operation.next() -> Gives you full input string
.charAt(0); -> returns the first character of the full input string
So anything we enter +9 or */ -> it returns the first element.
Now let's handle this case:
String o = operation.next(); // here is complete input
if(o.length()!=1){
userOperation = 0; //if length of input is not 1 we set custom value
}else{
userOperation = o.charAt(0); //get and set the first and only element
}
Hopefully it helps!
I am new to Java and learning. I am making a looping menu in Java. But when I select "a" and enter the details it doesn't go back to the menu.
I have done a bit of research and I need to add a Do and While loop here, but I'm confused on how to implement that here. A bit of guidance is extremely appreciated
Heres my code below:
switch(selection) {
case 'A':
case 'a':
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 'B':
case 'b':
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 'x':
case 'X':
System.out.println("Exiting data entry menu..");
break;
//equivalent to an else
default:
System.out.println("ERROR! - Please Enter a Valid Selection!");
}
while(selection != 'X');
Is this what you needed?
char selection;
do
{
do
{
//Menu
System.out.println("Toll Road Data Entry Menu");
System.out.println("-----------------------------------------");
System.out.println("A - Record Trip");
System.out.println("B - Record Breakdown Incident");
System.out.println("X - Exit");
System.out.print("Enter Your Selection: ");
selection = input.nextChar();
if (selection!='a' || selection!='A' || selection!='b' || selection!='B' || selection!='x' || selection!='X')
{
System.out.println("Selection must be a single character, A,B or X");
continue;
}
else
break;
} while (1);
switch(selection)
{
case 'A':
case 'a':
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 'B':
case 'b':
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 'x':
case 'X':
System.out.println("Exiting data entry menu..");
break;
//equivalent to an else
default:
System.out.println("ERROR! - Please Enter a Valid Selection!");
}
} while(selection != 'X');
I usually through them in while loops that way it just through me back to main menu when done
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Scanner input = new Scanner(System.in);
boolean menu=true;
int selection,sector_break;
String date,enter_point,exit_point,
breakdown;
double rec_cost;
while(true)
{
System.out.print(String.format("\033[2J"));
System.out.print("Your Menu Title \n\n");
System.out.print("1. Selection A\n");
System.out.print("2. Selection B\n");
System.out.print("3. Selection C\n");
System.out.print("4. Selection D\n");
System.out.print("5. Exit Menu D\n");
selection = Integer.parseInt(input.nextLine());
if(selection<1 || selection>4)
return;
switch(selection){
case 1:
System.out.print("Enter a Trip Date: ");
date = input.nextLine();
System.out.print("Enter Trip Point: ");
enter_point = input.nextLine();
System.out.print("Enter Exit Point: ");
exit_point = input.nextLine();
break;
case 2:
System.out.print("Enter Breakdown Date: ");
breakdown = input.nextLine();
System.out.print("Enter Sector Breakdown Occured in: ");
sector_break = input.nextInt();
System.out.print("Enter Vehicle Recovery Cost: ");
rec_cost = input.nextDouble();
break;
case 3:
// your code here
default:
// your code here
}
}
}
}
I've made a basic calculator program and I'm getting this exception:
java.util.InputMismatchException
java.util.Scanner.next(Unknown Source)
The code runs just fine but when exception occurs it doesn't allows the user to input using Scanner. What am I doing wrong and how can I fix it?
package string;
import java.util.Scanner;
import java.lang.Exception;
public class Calculator {
double sum(double a,double b)
{
double c =a+b;
return c;
}
double subtract(double a,double b)
{
double c= a-b;
return c;
}
double multiply(double a,double b)
{
double c=a*b;
return c;
}
double divide(double a,double b)
{
double c=a/b;
return c;
}
public static void main(String[] args) {
Calculator f= new Calculator();
int choice;
int z;
Scanner s1 =new Scanner(System.in);
do{
try{
System.out.println("Welcome To Mini Calculator: Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();
System.out.println();
switch(choice){
case 1:
System.out.print("Please Enter The First Number: ");
double x= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double y= s1.nextDouble();
double u = f.sum(x,y);
System.out.println();
System.out.println("The Sum Of Two Numbers is: " + u);
break;
case 2:
System.out.print("Please Enter The First Number: ");
double q= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double w= s1.nextDouble();
double i= f.subtract(q,w);
System.out.println();
System.out.println("The Substraction Of Two Numbers is: "+i );
break;
case 3:
System.out.print("Please Enter The First Number: ");
double e= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double r= s1.nextDouble();
double o= f.multiply(e, r);
System.out.println();
System.out.println("The Multiplication Of Two Numbers " + o);
break;
case 4:
System.out.print("Please Enter The First Number: ");
double t= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double k= s1.nextDouble();
double p= f.divide(t,k);
System.out.println();
System.out.println("The Divison of Two Numbers is: "+ p);
break;
default:System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
}
catch(Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
}
System.out.println();
System.out.println("Do You Want To perform Another Functionality?");
System.out.println("Press 1 to Continue and Press 2 to Terminate The Program");
z= s1.nextInt(); // Issue comes here. It runs fine without exception. When exception occurs in above code ,it doesn't take input and shows another exception
}
while(z==1);
System.out.println();
System.out.println("Thank You For Using Calculator");
s1.close();
}
}
When you enter a wrong input, it goes in the catch but the input is still here, so z= s1.nextInt(); throws another exception which is not catched and it crashes
So you need to read the input in the catch, to clear the scanner :
} catch (Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
s1.nextLine();
}
Also, you have a lot of code duplicate, and variable names which means nothing, this is not very good compare to standards, I would suggestsomething like this to replace your whole switch{ ... }
System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double res;
String operation = "";
switch (choice) {
case 1:
res = f.sum(numb1, numb2);
operation = "Sum";
break;
case 2:
res = f.subtract(numb1, numb2);
operation = "Substraction";
break;
case 3:
res = f.multiply(numb1, numb2);
operation = "Multiplication";
break;
case 4:
res = f.divide(numb1, numb2);
operation = "Divison";
break;
default:
res = 0;
System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + res);
so I'm learning JAVA by myself and I have only the basic knowledge about programming languages in general. I wrote this simple calculator program to try and apply what I've been learning this far, but the problem is that it doesn't print the age, instead it prints a 0 and I don't know why:
this is the class
public class userinput {
private String name;
private int age;
public tuna (String name, int age){
name = "dina";
age = 3;
}
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
uage = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.println("your age is");
System.out.println(getAge());
System.out.println();
}}
and this is the main class:
import java.util.Scanner;
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
tuna objc1 = new userinput(name1, age1);
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
System.out.println("Bye!");
}
}
}
Where is the tuna class?
tuna objc1 = new userinput(name1, age1);
the userinput's Constructor:
public userinput(String n, int a){
name = n;
age = a;
}
I was just learning
the first error you don't have tuna class change the error to
tuna objc1 = new userinput(name1,age1);
to
userinput objc1 = new userinput();
then i change your code like that the method you use it in first class
and the second class to make operation
the first class
import java.util.Scanner;
public class userinput {
private String name;
private int age;
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
age= uage; }
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.printf("your age is %s", getAge());
System.out.println();}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
the second class
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
userinput objc1 = new userinput();
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
objc1.simpleMessage2();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
}
System.out.println("Bye!");}
}
I hope i help you
when user logs out the user may choose to log back in again. For Logging back in: If the user get the pin code wrong 3 times, the program terminates.
System.out.println("You have logged out");
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
while (pin != pin2){
while (ctr < 2){
System.out.print("Please Enter Pin: ");
pin2 = sc.nextInt();
ctr++;
}
}
If I understand your problem correctly you will want to have something like that:
while (pin == pin2) {
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel) {
case 1:
System.out.println("Your current balance is " + bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal = dep + bal;
System.out.println("Your new current balance is " + bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if (with > bal) {
System.out.println("You do not have that amount on your account! Please enter again.");
} else {
System.out.println("You withdrew " + with);
bal = bal - with;
System.out.println("Your new current balance is " + (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
break;
case 5:
System.out.println("Please Enter Pin: ");
pin = sc.nextInt();
break;
default:
break;
}
}
Basically, I've deleted the loop label, it is not necessary and I consider it a bad style. I've also changed the while condition, so the program runs as long as user enters exactly the same pin as he confirmed at the beginning. Moreover, I think it is better to read the value of sel after printing instructions, not before as you did.
Like what been said in the comments, avoid using Labels and/or goto rep.
Use something like that :
import java.util.Scanner;
public class AtmMachine {
public static void main(String[] args){
int actualPin = -1;
int sel = 0, pin, pin2, check, ctr = 0, dep, with, bal = 10000;
Scanner sc = new Scanner(System.in);
while(actualPin == -1)
{
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.print("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
}
boolean logged = false;
while (true){
if(logged){
System.out.println("What would you like to do?");
System.out.println("1 - Check Balance");
System.out.println("2 - Deposite");
System.out.println("3 - Withdraw");
System.out.println("4 - Change Pin");
System.out.println("5 - End Transaction");
sel = sc.nextInt();
switch (sel){
case 1:
System.out.println("Your current balance is "+ bal);
break;
case 2:
System.out.println("How much would you want to deposite? ");
dep = sc.nextInt();
bal= dep+bal;
System.out.println("Your new current balance is "+ bal);
break;
case 3:
System.out.println("How much would you want to Withdraw? ");
with = sc.nextInt();
if(with > bal){
System.out.println("You do not have that amount on your account! Please enter again.");
}
else{
System.out.println("You withdrew "+ with);
bal = bal-with;
System.out.println("Your new current balance is "+ (bal));
}
break;
case 4:
System.out.print("Please enter a new pin: ");
pin = sc.nextInt();
System.out.println("Please verify your new pin: ");
pin2 = sc.nextInt();
if(pin == pin2) actualPin = pin;
else System.out.println("Error");
break;
case 5:
logged = false;
break;
default:
break;
}
else{
System.out.println("Please Enter Pin: ");
sel = sc.nextInt();
logged = actualPin == sel;
}
}
}
}