d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question > Hm
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 07:16pm
Up top in toolbar

Window -> Show View -> Navigator
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:25pm
Quote (Minkomonster @ Jan 25 2015 09:16pm)
Up top in toolbar

Window -> Show View -> Navigator


Got it up. Just trying to configure this stuff.. Gotta use a loop that will return a max of 3 user errors on their input before it prompts an exit, as well as the previous requirements.


This post was edited by axid3nt on Jan 25 2015 07:27pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 07:27pm
It looks like your usage of System.exit for Menu item 6 is working:

Code
Menu: Welcome to Basic Calculator
1. Add
2. Subtract
3. Divide
4. Multiply
5. Generate Random Number
6. Quit

6
Enter your first number 1
Enter your second number to perform your selected operation 2


The only real problem seems to be that you ask for the numbers in the expression before you check what the choice was. If I select 6, it shouldn't ask me for my numbers. You could do something like this:


Code


String choice;
//... Your code for displaying the menu and obtaining the users choice

if(choice == "6") System.exit(0);

else
{
int num1, num2, result;

//... Read in num1 and num 2

if(choice == "1") result = num1 + num2;
else if (choice == "2") result = num1 - num2;

//... Rest of your choices


System.out.println("The result is " + result);


}


Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:28pm


This is exactly what I was thinking but couldn't figure out how to state it without error. I'll try it.

This post was edited by axid3nt on Jan 25 2015 07:48pm
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:37pm


edit:

realize the structure makes absolutely no sense and overlooked the order of operations..

This post was edited by axid3nt on Jan 25 2015 07:48pm
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:48pm
Code
import java.util.Scanner;


public class Calc {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Menu: Welcome to Basic Calculator \n "
+ "1. Add \n "
+ "2. Subtract \n "
+ "3. Divide \n "
+ "4. Multiply \n "
+ "5. Generate Random Number \n "
+ "6. Quit \n ");
//Menu for Operations

String in = input.next();
char oper = in.charAt(0);

if(oper == "6") {
System.exit(0);
}

else


System.out.print("Enter your first number ");
in = input.next();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.next();
double num2 = Double.parseDouble(in);


if(oper == '1') {
double result = num1 + num2;
System.out.println(result);

} else if(oper == '5') {
double result = (num1 * num2 + num1 + num2 + 77);
System.out.println(result);
} else if(oper == '2') {
double result = num1 - num2;
System.out.println(result);
} else if(oper == '4') {
double result = num1 * num2;
System.out.println(result);
} else if(oper == '3') {
double result = num1 / num2;
System.out.println("Your answer is: " + result);
}


{

}
System.out.println("Thank you for using Basic Calculator!");
}


}





puzzled at this point. Can't seem to get the if statement to work properly.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 07:50pm
I would use nextLine instead of next.
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 07:54pm
Quote (Minkomonster @ Jan 25 2015 09:50pm)
I would use nextLine instead of next.


How do you have patience reading this nonsense? Lol

Thanks man.. I worked it out the operation, but the original commands 1-5 wont work now.. Is it necessary to input another scanner to read whats after the first if and else statements? It wont recognize num1 and num 1 as variables now.

This post was edited by axid3nt on Jan 25 2015 08:02pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 25 2015 08:15pm
Quote (axid3nt @ Jan 25 2015 08:54pm)
How do you have patience reading this nonsense? Lol

Thanks man.. I worked it out the operation, but the original commands 1-5 wont work now.. Is it necessary to input another scanner to read whats after the first if and else statements? It wont recognize num1 and num 1 as variables now.


As long as you are showing a desire to learn, I will match that desire to teach.


One thing I see wrong is your else is not wrapping a block statement

wrap everything under your else in braces { }


Go back and look at the example I posted and compare it with what you have
Member
Posts: 8,250
Joined: May 10 2012
Gold: 0.00
Jan 25 2015 08:24pm
Quote (Minkomonster @ Jan 25 2015 10:15pm)
As long as you are showing a desire to learn, I will match that desire to teach.


One thing I see wrong is your else is not wrapping a block statement

wrap everything under your else in braces { }


Go back and look at the example I posted and compare it with what you have


Okay, I might have took a lazy way out. I had that wrap fixed, but what I did was this:

Code


import java.util.Scanner;


public class Calc {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Menu: Welcome to Basic Calculator \n "
+ "1. Add \n "
+ "2. Subtract \n "
+ "3. Divide \n "
+ "4. Multiply \n "
+ "5. Generate Random Number \n "
+ "6. Quit \n ");
//Menu for Operations

String in = input.next();
char oper = in.charAt(0);

if (oper == '6') {
System.out.println("Your have chosen to Quit. Thank you for using Basic Calculator!");
System.exit(0);

}

else





if (oper == '1') {
in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 + num2;
System.out.println("Your answer is: " + result);


} else if(oper == '5') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = (num1 * num2 + num1 + num2 + 77);
System.out.println("Your answer is: " + result);


} else if(oper == '2') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 - num2;
System.out.println("Your answer is: " + result);


} else if(oper == '4') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 * num2;
System.out.println("Your answer is: " + result);


} else if(oper == '3') {in = input.nextLine();
System.out.print("Enter your first number ");
in = input.nextLine();
double num1 = Double.parseDouble(in);

System.out.print("Enter your second number to perform your selected operation ");
in = input.nextLine();
double num2 = Double.parseDouble(in);
double result = num1 / num2;
System.out.println("Your answer is: " + result);
}
else
System.out.println("Sorry, you have entered an invlaid option. Please try again");
return;

}
{

}

}









This looks like bad coding etiquette, but it's the only way I knew how to complete the code..

The only left for me to do is loop the "else, try again" output, with a return(possibly?) and give them 3 times before I force quit the app... I'm looking into that right now. I know most of what I need to know about loops, but unsure of how to start the process of returning to a Start menu in order to ask the user for his input again.
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll