Using intAt for a Switch - java

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

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 do I use the users choice in the change method

I'm making a vending machine program and I don't know how to use the users choice in my for loop because it gives me an error when i put choice in the loop.
public class PopGenerator {
double price[] = {2.49, 1.25, 3.49, 3.25, 2,25, 1.30, 3.40, 3.49, 2.50, 3.00};
public void beveragechoice()
{
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
double price[] = {2.49, 1.25, 3.49, 3.25, 2.25, 1.30, 3.40, 3.49, 2.50, 3.00};
switch(choice)
{
case 1:
System.out.println("This beverage costs $" + price[0]);
break;
case 2:
System.out.println("This beverage costs $" + price[1]);
break;
case 3:
System.out.println("This beverage costs $" + price[2]);
break;
case 4:
System.out.println("This beverage costs $" + price[3]);
break;
case 5:
System.out.println("This beverage costs $" + price[4]);
break;
case 6:
System.out.println("This beverage costs $" + price[5]);
break;
case 7:
System.out.println("This beverage costs $" + price[6]);
break;
case 8:
System.out.println("This beverage costs $" + price[7]);
break;
case 9:
System.out.println("This beverage costs $" + price[8]);
break;
case 10:
System.out.println("This beverage costs $" + price[9]);
}
}
public void change()
{
System.out.println("Enter money put into the machine: ");
Scanner m = new Scanner(System.in);
int money = m.nextInt();
for(int x = choice; x >= 0 ; x--)
if (money == price[x])
{
System.out.println("No change.");
}
else
{
System.out.print("Your change is: ");
System.out.print(money - price[x]);
}
}
}
You can simplify both of your methods to avoid having multiple Scanners or pass variables through them, like this:
public void beveragechoice() {
double prices[] = {2.49, 1.25, 3.49, 3.25, 2,25, 1.30, 3.40, 3.49, 2.50, 3.00};
System.out.println("Select one beverage");
Scanner c = new Scanner(System.in);
int choice = c.nextInt();
if (choice > 0 && choice <= prices.length) {
double p = prices[choice - 1];
System.out.println("This beverage costs $" + p);
System.out.println("Enter money put into the machine: ");
double money = c.nextDouble();
if (money == p) {
System.out.println("No change.");
} else {
System.out.println("Your change is: $" + (money - p));
}
}
}

Error on the display statement even after importing the package

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

Java Restaurant Menu, calculation issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to calculate the total for a menu item, I cannot quite figure out how to get the code to take the proper inputs and read them as item values. I need it to register that the user wants say 2 pizzas that are 12$ each for a total of 24, however I cannot figure it out. Here is what I have.
package javaapplication4;
/**
*
* #author Travis
*/
import java.util.Scanner;
public class JavaApplication4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float Chicken = 9;
float Pizza = 12;
float Veal = 15;
float Alfredo = 9;
float Garlic = 8;
float Veggie = 7;
float Spaghetti = 9;
float Raviolli = 7;
float Meat = 8;
float Canolli = 5;
// Display menu and prices
System.out.println("1. Chicken $9");
System.out.println("2. Pizza $12");
System.out.println("3. Veal $15");
System.out.println("4. Alfredo $9");
System.out.println("5. Garlic $8");
System.out.println("6. Veggie $7");
System.out.println("7. Spaghetti $9");
System.out.println("8. Raviolli $7");
System.out.println("9. Meat $8");
System.out.println("10. Canolli 5$");
System.out.print("Enter your menu choice name: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();
//prompt user for quantity
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();
//Switch to determine price
double price = orderNumber + orderAmount;
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
}
System.out.println("Your total is " + price);
System.out.println("Please enter your payment amount");
int payment = input.nextInt();
double total = payment - price;
System.out.println ("Thank you your change is $" + total);
}
Take a look at your switch statement (you're never really using it).
You should have something like this:
double price = 0.0; //You will reassign this
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
double total_price = price * orderAmount; //You currently have this as an addition
You wrote
double price = orderNumber + orderAmount;
I think it should be:
double price = orderNumber * orderAmount;

How do I make my if conditions repeat in Java?

Basically I want to be able to input a number that isn't an option and then be given the option to choice again and get the statement to repeat.
Scanner keyboard = new Scanner(System.in);
double weight;
int Choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune");
Choice =keyboard.nextInt();
if (Choice == 1){
System.out.println("Your weight on Venus would be " + (weight * 0.78));
}
else if (Choice == 2){
System.out.println("Your weight on Mars would be " + (weight * .39));
}
else if (Choice == 3){
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
}
else if (Choice == 4){
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
}
else if (Choice == 5){
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
}
else if (Choice == 6) {
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
}
else
System.out.println("This was not a choice, try again!");
Choice = keyboard.nextInt();
}
This is an easier way to go, using a do-while loop and a switch.
Also fixed choice
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune\n 7. Exit");
choice = keyboard.nextInt();
switch(choice) {
case 1:
System.out.println("Your weight on Venus would be " + (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be " + (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
break;
case 7:
System.out.println("Bye");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
You have to learn the basic in programming language... do..while and switch statements.
Here is the sample program.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double weight;
int choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
do {
System.out
.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune \n7.EXIT");
System.out.println();
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("Your weight on Venus would be "
+ (weight * 0.78));
break;
case 2:
System.out.println("Your weight on Mars would be "
+ (weight * .39));
break;
case 3:
System.out.println("Your weight on Jupiter would be "
+ (weight * 2.65));
break;
case 4:
System.out.println("Your weight on Saturn would be "
+ (weight * 1.17));
break;
case 5:
System.out.println("Your weight on Uranus would be"
+ (weight * 1.05));
break;
case 6:
System.out.println("Your weight on Neptune would be "
+ (weight * 1.23));
case 7:
System.out.println("Leaving...");
break;
default:
System.out.println("This was not a choice, try again!");
break;
}
} while (choice != 7);
}
Use the Java Naming conventions to give names to variables. Start with lowercase letter.
This can be done with a while loop. Also consider using arrays.
String[] planets = {"Mars", "Jupiter", .... };
double[] factors = {0.45, 1.5, ....};
double mass = keyboard.nextDouble();
int choice = keyboard.nextInt();
while (choice < 0 || choice >= planets.length)
{
System.out.println("Not a valid option!");
choice = keyboard.nextInt();
}
double weight = mass * factors[choice];
System.out.println("Your weight on " + planets[choice] + " would be " + weight);
So, basically, you ask it once politely. Afterwards, start shouting that it is wrong, until the user gave a proper input.

Categories