Error on the display statement even after importing the package - java

I can't seem to understand what is the problem with this code. I'm new to classes in Java. I made a new Java Class here. I've tried almost everything but the error on the lines below my constructor are really getting annoying now. Can someone please help me what is wrong here?
P.s the main class is completely empty and the lines in bold are the ones where the compiler shows errors.
import java.util.Scanner;
public class calculator extends FinalConsole {
float x;
float y;
float v;
float w;
float z;
char op;
Scanner in= new Scanner(System. in);
public calculator(){
System.out.println("You have selected calculator interface");
}
**System.out.println("Enter the number of digits: ");
int values;
values=in.nextInt();
System.out.println("Select your funcion");
op = in.next().charAt(0);**
**if (values==2){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
switch (op){
case '+':
z=x+y;
System.out.println("sum is:" +z);
break;
case '-':
z = x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
}
}
**if (values==3){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
System.out.println("Enter third number: ");
v= in.nextFloat( );
switch(op){
case '+':
z=w+x+y;
System.out.println("sum is:" +z);
break;
case '-':
z=x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=w*x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
break;
}
}
**if (values==4){**
System.out.println("Enter first number: ");
x= in.nextFloat( );
System.out.println("Enter second number: ");
y=in.nextFloat();
System.out.println("Enter third number: ");
v= in.nextFloat( );
System.out.println("Enter fourth number: ");
w= in.nextFloat( );
switch (op){
case '+':
z=v+w+x+y;
System.out.println("sum is:" +z);
break;
case '-':
z=x-y;
System.out.println("sum is:" +z);
break;
case '/':
z=y/x;
System.out.println("sum is:" +z);
break;
case '*':
z=v*w*x*y;
System.out.println("sum is:" +z);
break;
default:
System.out.println("Invalid operator");
break;
}
}
}

you should put your statements in blocks or methods, like following
import java.util.Scanner;
public class calculator {
float x;
float y;
float v;
float w;
float z;
char op;
Scanner in= new Scanner(System. in);
public calculator(){
System.out.println("You have selected calculator interface");
}
{
System.out.println("Enter the number of digits: ");
int values;
values = in.nextInt();
System.out.println("Select your funcion");
op = in.next().charAt(0);
}
}

Related

how do I get back to an outer switch if I am inside an inner switch?

I am trying to program a menu for my calculator. I do not know how to get out of my inner loop and get back to my outer loop once I have executed the inner loop. I have to finish the whole case of the inner loop before I can get back to the outer loop. Is there a way that I can go back and forth of the nested switch whenever I want to? and when I execute my outer switch case 3, it executes infinitely. Maybe because it's inside a loop.
import java.util.Scanner;
public class MainInterface {
static Scanner input = new Scanner(System.in);
static boolean hasRun = false;
public static void main(String args[])
{
Calculator Mycal = new SimpleCalculator();
ScientificCalculator Mycal2 = new ScientificCalculator();
mainMenu();
int choice = input.nextInt();
do {
System.out.println("\n");
switch(choice) // outer switch
{
case 1: simpleCalculatorMenu();
int choice2 = input.nextInt();
switch(choice2) // inner switch
{
case 1: //ADDITION
System.out.println("ADDITION");
System.out.println("\n");
System.out.println("Enter first number: ");
float x = input.nextFloat();
System.out.println("Enter second number: ");
float y = input.nextFloat();
System.out.println("\n");
System.out.println("The sum of " + x + " " + y + " is " + Mycal.add(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
}
break;
case 2: System.out.println("SCIENTIFIC CALCULATOR");
System.out.println("\n");
System.out.println("1. power(x, pow) 2. sin(xDeg) 3. cos(xDeg) 4. tan(xDeg)");
System.out.println("\n");
System.out.println("5. pi() 6. fact(x) ");
int choice3 = input.nextInt();
switch(choice3) {
case 1:
System.out.println("POWER");
System.out.println("\n");
System.out.println("Enter first number: ");
double x = input.nextDouble();
System.out.println("Enter second number: ");
double y = input.nextDouble();
System.out.println("\n");
System.out.println("The power of " + x + " to the power of " + y + " is " + Mycal2.power(x, y));
System.out.println("\n");
System.out.println("Would you like to return to the main menu?");
System.out.println("\n");
System.out.println("Enter 1. to return 2. to exit");
choice = input.nextInt();
break;
case 2:
}
break;
case 3: mainMenu();
break;
case 4: System.exit(choice);
break;
}
} while(choice != 0);
}
public static void simpleCalculatorMenu () {
System.out.println("SIMPLE CALCULATOR");
System.out.println("\n");
System.out.println("1. Addition 2. Subtraction 3. Multiplication 4. Division");
System.out.println("\n");
System.out.println("5. Squared Root 6. Squared 7. Cube 8. Discount ");
}
public static void mainMenu () {
System.out.println("What calculator do you want to use?");
System.out.println("\n");
System.out.println("1. Simple Calculator \t 2. Scientific Calculator");
}
}
by adding label you can try like this
OUTER:
switch(condition1) {
case x:
switch(condition2) {
case y:
System.out.println("hello");
break OUTER;
}
}

How can I restrict the input char value?

I created simple calculator using switch case. When I enter the invalid operators, but it takes that value .And at last it gives the default switch case .How can I restrict it.
package calculator;
import java.util.*;
public class Calculator {
public static void main(String[] args) {
char operator;
Double num1, num2, result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the operator: +,-,*,/,% ");
operator = input.next().charAt(0);
//user input
System.out.println("Enter the First Number:");
num1 = input.nextDouble();
System.out.println("Enter the Second Number:");
num2 = input.nextDouble();
switch (operator) {
case '+':
result = num1+num2;
System.out.println(num1+" + "+num1+" = " + result);
break;
case '-':
result = num1-num2;
System.out.println(num1+" - "+num1+" = " + result);
break;
case '*':
result = num1*num2;
System.out.println(num1+" * "+num1+" = " + result);
break;
case '/':
result = num1/num2;
System.out.println(num1+" / "+num1+" = " + result);
break;
case '%':
result = num1%num2;
System.out.println(num1+" % "+num1+" = " + result);
break;
default:
System.out.println("Invalid operator");
break;
}
input.close();
}
}
console output
Enter the operator: +,-,*,/,%
7
Enter the First Number:
5
Enter the Second Number:
5
Invalid operator
if(Character.isDigit(c)){
// what you want for true
}
else{
// what you want for false
}
This may help you.
Java or any other programming languages run code sequentially.
Here once the operator is entered you can check to proceed further for other statements.
The logic of code how you write, that way it is executed.
So in this case, once you take value for operator check whether that operator is allowed in your case or not.
If allowed then run the further code else not run that.

Java Calculator wrong operator prompt

i have a calculator class and i want the operation input to be specific to these strings "+, -, /, *" if its not, i want to print a message to enter the operator again.
and please if someone would lessen my code please do it. thanks
here is my code
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
String operation;
Double fnum, lnum, answer;
System.out.println("Enter first Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter first Number: ");
}
fnum = num.nextDouble();
System.out.println("Enter Operation: ");
//HERE IM BEING CONFUSED
while (!num.equals("+, -, /, *"))
{
num.next();
System.out.print("Enter Operator: ");
}
operation = num.next();
//End
System.out.println("Enter Second Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter Second Number: ");
}
lnum = num.nextDouble();
switch (operation) {
case "+":
answer = fnum + lnum;
System.out.print("Equals= " + answer);
break;
case "-":
answer = fnum - lnum;
System.out.print("Equals= " + answer);
break;
case "*":
answer = fnum * lnum;
System.out.println("Equals= " + answer);
break;
case "/":
answer = fnum / lnum;
System.out.println("Equals= " + answer);
break;
default:
System.out.println("wrong operator");
break;
}
}
}
This is a way to use operators with SWITCH sentence.
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
double v1,v2;
String v3;
System.out.print("Enter first number: ");
v1 = num.nextDouble();
System.out.print("Enter second number: ");
v2 = num.nextDouble();
System.out.print("Enter operation [+] [-] [*] [/]: ");
v3 = num.next();
System.out.println( ("+".equals(v3) ) ? Operators.ADD.calculate(v1, v2) : "....");
}
public enum Operators {
ADD;
double calculate(double x, double y) {
switch (this) {
case ADD:
return x + y;
default:
throw new AssertionError("Unknown operations " + this);
}
}
}

Using intAt for a Switch

I'm struggling to understand why intAt will not work in this program. The goal of program is to simply convert weights for a specific planet.
package weightonotherplanets;
import java.util.Scanner;
public class WeightonOtherPlanets
{
public static void main(String args[]){
System.out.println("What is your weight on the Earth?");
Scanner weightInput = new Scanner(System.in); // Enter your weight
int weight = weightInput.nextInt();
System.out.println("1. Voltar\n2. Krypton\n3. Fertos\n4. Servontos\n"); // Choice of planets
System.out.println(" Selection?");
Scanner selectionChoice = new Scanner(System.in);
int selection = selectionChoice.nextInt();
int select = selection.intAt(0); // This is the problem in the code
switch (select)
{
case '1':
System.out.println("Your weight on Voltor would be " + weight * 0.091);
break;
case '2':
System.out.println("Your weight on Krypton would be " + weight * 0.720);
break;
case '3':
System.out.println("Your weight on Fertos would be " + weight * 0.865);
break;
case '4':
System.out.println("Your weight on Servontos would be " + weight * 4.612);
break;
default:
System.out.println("Please make a selection.");
}
}
}

Having issues with a menu function and a case switch function for a simple calculator

I'm trying to write a simple calculator with 2 functions (for right now). I'm using Java in Eclipse. I can't get what I'm currently attempting to work. I want to have just the menu item as its own function and then the case switch to be its own function after taking in the entryChoice. When I run this code as is and I make a selection all it does is repeats "Enter two numbers..." OR if I separate the userInput into each case to be printed after the entryChoice is passed, it breaks. Any advice?
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7)
{
userSelection(entryChoice);
}
System.exit(0);
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x,y);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
break;
case 7:
result = 0;
break;
default:
}
return result;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7) {
System.out.println(userSelection(entryChoice));
displayMenu();
entryChoice = scanChoice.nextInt();
}
System.exit(0);
}
public static void displayMenu() {
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice) {
Scanner userInput = new Scanner(System.in);
double x = 0;
double y = 0;
double result = 0;
if (entryChoice == 6) {
System.out.println("Enter one number: ");
x = userInput.nextDouble();
} else {
System.out.println("Enter two numbers seperated by a space");
x = userInput.nextDouble();
y = userInput.nextDouble();
}
switch (entryChoice) {
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x, y);
break;
case 6:
result = Math.sqrt(x);
break;
case 7:
result = 0;
break;
default:
}
return result;
}
I think what you want to have is the following:
public static void main(String[] args) {
// TODO Auto-generated method stub
int entryChoice = -1;
while (entryChoice != 7)
{
displayMenu();
Scanner scanChoice = new Scanner(System.in);
entryChoice = scanChoice.nextInt();
if (entryChoice != 7)
System.out.println(userSelection(entryChoice));
}
System.exit(0);
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
case 5:
result = Math.pow(x,y);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
break;
default:
}
return result;
}
Please try this, it is working as per your expectation
import java.util.*;
class Calculator
{
public static void main(String[] args) {
// TODO Auto-generated method stub
displayMenu();
}
public static void displayMenu()
{
System.out.println("Please select from the following choices:");
System.out.println();
System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Raise to a Power");
System.out.println("6) Square Root");
System.out.println("7) Exit Program");
System.out.println();
System.out.println("Enter your choice here: ");
Scanner scanChoice = new Scanner(System.in);
int entryChoice = scanChoice.nextInt();
while (entryChoice != 7)
{
userSelection(entryChoice);
}
System.exit(0);
}
public static double userSelection(int entryChoice)
{
double result = 0;
System.out.println("Enter two numbers seperated by a space");
Scanner userInput = new Scanner(System.in);
double x = userInput.nextDouble();
double y = userInput.nextDouble();
switch (entryChoice)
{
case 1:
result = x + y;
System.out.println("Result is :"+result);
break;
case 2:
result = x - y;
System.out.println("Result is :"+result);
break;
case 3:
result = x * y;
System.out.println("Result is :"+result);
break;
case 4:
result = x / y;
System.out.println("Result is :"+result);
break;
case 5:
result = Math.pow(x,y);
System.out.println("Result is :"+result);
break;
case 6:
System.out.println("Enter one number: "); // some kinks to work out here..
result = Math.sqrt(x);
System.out.println("Result is :"+result);
break;
case 7:
result = 0;
break;
default:
}
displayMenu();
return result;
}
}

Categories