I am trying to write a program that ask a user for 2 numbers and then ask the user to pick a command from a menu by entering the correspond number to the command.
I can write the program if i take the input as an Int but cannot figure it out for a string, also it has to be a string.
I am having problems when it enters the while loop to validate the user input it does not stop when the statement is false it will stay in the loop I can not figure out what i am doing wrong.
Here is the code i have.
import java.util.Scanner;
public class ab {
public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");
input = stdIn.next();
System.out.println();
while (!input.equals(1) && !input.equals(2) && !input.equals(3) && !input.equals(4)) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
System.out.println();
if (input.equals(1)) {
System.out.print(L + " * " + R + " = " + (L * R));
} else if (input.equals(2)) {
System.out.print(L + " / " + R + " = " + (L / R));
} else if (input.equals(3)) {
System.out.print(L + " + " + R + " = " + (L + R));
} else {
System.out.print(L + " - " + R + " = " + (L - R));
}
}
stdIn.close();
}
}
Any help would be much appreciated.
Thank you in advanced.
The line input = stdIn.next(); is taking input as String
while your comparison is against integer. So a String never equals Int
You may try changing your while loop condition to:
while (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))
note the double quote around the numbers
Is answered, but check this
import java.util.Scanner;
public class ab {
public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");
input = stdIn.next();
while (true) {
if (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4")) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
} else {
if (input.equals("1")) {
System.out.print(L + " * " + R + " = " + (L * R));
break;
} else if (input.equals("2")) {
System.out.print(L + " / " + R + " = " + (L / R));
break;
} else if (input.equals("3")) {
System.out.print(L + " + " + R + " = " + (L + R));
break;
} else {
System.out.print(L + " - " + R + " = " + (L - R));
break;
}
}
}
stdIn.close();
}
}
Related
Essentially i've isolated the issue, the int numpersons begins as 0. I take a user input to make it a particular number which is the array size, when the second method begins it takes the 0 again and then the array has an out of bounds exception. I want to pass it from one method to the next, or make them more successive, idk how to do this
thanks in advance
import java.util.Scanner;
public class BankApp {
Scanner input = new Scanner(System.in);
int numpersons = 0;
private SavingsAccount[] clients = new SavingsAccount[numpersons];
public BankApp() {
while (numpersons < 1) {
System.out.println("How many people are there?");
numpersons = input.nextInt();
if (numpersons < 1 || 2147483647 < numpersons) {
System.out.println("invalid number, please enter again");
}
}
input.nextLine();
}
public void addClients() {
int i = 0;
while (i < numpersons) {
System.out.println("enter account id " + (i + 1));
String AccountID = input.nextLine();
System.out.println("enter account name " + (i + 1));
String AccountName = input.nextLine();
System.out.println("enter account balance " + (i + 1));
Double AccountBalance = input.nextDouble();
clients[i] = new SavingsAccount(AccountID, AccountName, AccountBalance);
input.nextLine();
i++;
}
}
public void displayClients() {
int i = 0;
while (i < numpersons) {
System.out.println("======================================");
System.out.println("Account ID " + (i + 1) + ": " + clients[i].getID());
System.out.println("Account Name " + (i + 1) + ": " + clients[i].getName());
System.out.println("Account Balance " + (i + 1) + ": " + clients[i].getBalance());
System.out.println("======================================");
i++;
}
}
public static void main(String args[]) {
BankApp ba = new BankApp();
ba.addClients();
ba.displayClients();
}
}
Hello I can't seem to get my while loop to terminate the code when I'm typing the String "quit". Any help would be appreciated.
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
#SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
num1 = userinput.next();
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
}
else if(calc.equals("-")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
}
else if(calc.equals("*")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
}
else if(calc.equals("/")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
}
else{
System.out.println("The operation is not valid.");
}
if(input[0].equalsIgnoreCase("quit")){
break;
}
}
System.exit(0);
}
}
The code is waiting on user input at calc = userinput.next();
public boolean hasNext()
Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.
You should first validate the input inside its own condition.
Example:
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args) {
BinaryExpression operation = new BinaryExpression();
#SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while (true) {
String calc;
String num1;
String num2;
num1 = userinput.next();
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if (!input[0].equalsIgnoreCase("quit")) {
if (calc.equals("+")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
} else if (calc.equals("-")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
} else if (calc.equals("*")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
} else if (calc.equals("/")) {
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
} else {
System.out.println("The operation is not valid.");
}
} else {
break;
}
}
System.exit(0);
}
}
Try checking for the 'quit' before any of the other conditions:
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
#SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
num1 = userinput.next();
if(num1.equalsIgnoreCase("quit")){
break;
}
calc = userinput.next();
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Add(x, y));
}
else if(calc.equals("-")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Subtract(x, y));
}
else if(calc.equals("*")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Multiply(x, y));
}
else if(calc.equals("/")){
System.out.print(num1);
System.out.print(" ");
System.out.print(calc);
System.out.print(" ");
System.out.print(num2);
System.out.print(" ");
System.out.print("=");
System.out.print(" ");
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(operation.Divide(x, y));
}
else{
System.out.println("The operation is not valid.");
}
}
System.exit(0);
}
}
The problem may have been that it is trying to parse 'quit' to an integer, which would cause an error and never reach the quit condition.
Or it could be that the code is waiting for an input at 'calc = userinput.next();', but i'm not sure if you tried entering the next two inputs
Code is probably stuck on the second next() call. Try just using a single call to nextLine() and a call to String::split
String input = userinput.nextLine(); // Get an entire line
if (input.equalsIgnoreCase("quit")){ // Check the exit first
break;
}
// If it hasn't exited assume that its an equation
String[] inputSplit = input.split(); // Split the input by spaces
String calc inputSplit[1];
String num1 inputSplit[0];
String num2 inputSplit[2];
I hope it will fit for you. I give it to some helping information to users when the program is running. You can try it out! :D
package CalculatorProg;
import java.util.Scanner;
public class MainDriver {
public static void main(String[] args){
BinaryExpression operation = new BinaryExpression();
#SuppressWarnings("resource")
Scanner userinput = new Scanner(System.in);
while(true){
String calc;
String num1;
String num2;
System.out.print("Number 1 (if quit then program exits) =");
num1 = userinput.next();
if(num1.equalsIgnoreCase("quit")) {
System.out.println("program quit");
break;
}
System.out.print("Operator =");
calc = userinput.next();
System.out.print("Number 2 =");
num2 = userinput.next();
String[] input = new String[5];
input[0] = num1;
input[1] = " ";
input[2] = calc;
input[3] = " ";
input[4] = num2;
if(calc.equals("+")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Add(x,y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x+y));
}
else if(calc.equals("-")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Subtract(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x-y));
}
else if(calc.equals("*")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Multiply(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x*y));
}
else if(calc.equals("/")){
int x, y;
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + operation.Divide(x, y));
//System.out.println(num1 + " " + calc + " " + num2 + " " + "=" + " " + (x/y));
}
else{
System.out.println("The operation is not valid. You can give +, -, *, / for operator!");
}
}
System.exit(0);
}
}
I just writed this program, it is to train myself for the upcomming exam this monday.
A thing i would like to add is: after a user is done with one of the exchange options 1/2/3 i would like to give the option to let the user return to the beginning welcome to the money exchange! etc.....
i have tried some a for loop and a while loop but i couldn't get it to work.
Would be cool if after the money exchange process that the user get the option to return to the beginning by typing y or n is this possible?
/* This program is written as a excercise to prep myself for exams.
* In this program the user can:
* 1. Select a currency (other than euro's)
* 2. Input the amount of money
* 3. transfer the amount of currency to euro's
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n ");
System.out.print("Input : ");
DecimalFormat df = new DecimalFormat() ;
df.setMaximumFractionDigits(2);
int choice = input.nextInt() ;
double transfee = 2.41 ;
double USrate = 0.9083 ;
double GBrate = 1.4015 ;
double YENrate = 0.0075 ;
if (choice > 3 || choice < 1) {
System.out.println("Invalid input!...... Please try agian\n");
} else {
if(choice == 1) {
System.out.println("You have choosen for US dollar \n");
System.out.print("Please enter amount US dollar: ");
double USamount = input.nextDouble() ;
double deuros = USamount * USrate ;
double ddisburse = deuros - transfee ;
System.out.print("\nInput amount US dollar:. " + USamount + "\n");
System.out.print("Worth in euro's:........ " + df.format(deuros) + "\n");
System.out.print("Transfer cost:.......... " + transfee + "\n");
System.out.print("Amount to disburse:..... " + df.format(ddisburse) + "\n" );
}else {
if(choice == 2){
System.out.println("You have choosen for GB pounds");
System.out.print("Please enter amount GB ponds: ");
double GBamount = input.nextDouble();
double geuros = GBamount * GBrate ;
double gdisburse = geuros - transfee;
System.out.print("\nInput amount GB pound:. " + GBamount + "\n");
System.out.print("Worth in euro's........ " + df.format(geuros) + "\n");
System.out.print("Transfer cost:......... " + transfee + "\n");
System.out.print("Amount to disburse:.... " + df.format(gdisburse) + "\n");
}else {
if(choice == 3){
System.out.println("You have choosen for Yen");
System.out.print("Please enter amount Yen: ");
double YENamount = input.nextDouble();
double yeuros = YENamount * YENrate ;
double ydisburse = yeuros - transfee ;
System.out.print("\nInput amount Yen:... " + YENamount + "\n");
System.out.print("Worth in euro's..... " + df.format(yeuros) + "\n");
System.out.print("Transfer cost:...... " + transfee + "\n");
System.out.print("Amount to disburse:. " + df.format(ydisburse) + "\n");
}
}
}
}
}
}
You could wrap your program with a while loop, which checks if the user entered 'y' at the end like this:
import java.text.DecimalFormat;
import java.util.Scanner;
class YourClassName
{
public static void main(String[] args)
{
boolean askAgain = true;
while (askAgain)
{
Scanner input = new Scanner(System.in);
System.out.println(
" Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n ");
System.out.print("Input : ");
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
int choice = input.nextInt();
double transfee = 2.41;
double USrate = 0.9083;
double GBrate = 1.4015;
double YENrate = 0.0075;
if (choice > 3 || choice < 1)
{
System.out.println("Invalid input!...... Please try agian\n");
} else
{
if (choice == 1)
{
System.out.println("You have choosen for US dollar \n");
System.out.print("Please enter amount US dollar: ");
double USamount = input.nextDouble();
double deuros = USamount * USrate;
double ddisburse = deuros - transfee;
System.out.print(
"\nInput amount US dollar:. " + USamount + "\n");
System.out.print("Worth in euro's:........ "
+ df.format(deuros) + "\n");
System.out.print(
"Transfer cost:.......... " + transfee + "\n");
System.out.print("Amount to disburse:..... "
+ df.format(ddisburse) + "\n");
} else
{
if (choice == 2)
{
System.out.println("You have choosen for GB pounds");
System.out.print("Please enter amount GB ponds: ");
double GBamount = input.nextDouble();
double geuros = GBamount * GBrate;
double gdisburse = geuros - transfee;
System.out.print(
"\nInput amount GB pound:. " + GBamount + "\n");
System.out.print("Worth in euro's........ "
+ df.format(geuros) + "\n");
System.out.print(
"Transfer cost:......... " + transfee + "\n");
System.out.print("Amount to disburse:.... "
+ df.format(gdisburse) + "\n");
} else
{
if (choice == 3)
{
System.out.println("You have choosen for Yen");
System.out.print("Please enter amount Yen: ");
double YENamount = input.nextDouble();
double yeuros = YENamount * YENrate;
double ydisburse = yeuros - transfee;
System.out.print("\nInput amount Yen:... "
+ YENamount + "\n");
System.out.print("Worth in euro's..... "
+ df.format(yeuros) + "\n");
System.out.print(
"Transfer cost:...... " + transfee + "\n");
System.out.print("Amount to disburse:. "
+ df.format(ydisburse) + "\n");
}
}
}
}
System.out.println("Do you want to do another calculation? (y/n)");
String againAnswer = input.next();
askAgain = againAnswer.equalsIgnoreCase("y");
}
}
}
Setting the boolean variable to true first lets you enter the loop. The user will be asked as long as he types an y at the end. Every other character would exit the loop:
String againAnswer = input.next();
askAgain = againAnswer.equalsIgnoreCase("y");
You could also check for explicit n, but that is up to you.
Put the code inside a while loop (while(true)). At the end of each if block
add one nested if.
System.out.print(Do you want to continue?");
if(in.next().equals("Y")) {
continue;
}
And you have add one extra menu(4th) for exit :
if(choice == 4){
break;
}
I am attempting to simplify my long code of a calculator program, but I have a road block. I have a new else if statement for each calculator operator, but what I want to do is allow the user to manually type in, on one line, the entire operation they would like to perform and have the code compute it.
Here's what I have:
do {
System.out.println("What function would you like to perform?");
System.out.print("Exit Calculator (Q), Add (+), Subtract (-), Multiply (x), Divide (/): ");
maininput = in.next();
if (maininput.equals("+")) {
System.out.print("Enter the first number to add: ");
num1 = in.nextDouble();
System.out.print("Enter the second number to add: ");
num2 = in.nextDouble();
System.out.println();
answer = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + answer);
System.out.println();
}
else if (maininput.equals("-")) {
System.out.print("Enter the first number to subtract: ");
num1 = in.nextDouble();
System.out.print("Enter the second number to subtract: ");
num2 = in.nextDouble();
System.out.println();
answer = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + answer);
System.out.println();
}
else if(maininput.equals("x")) {
System.out.print("Enter the first number to multiply: ");
num1 = in.nextDouble();
System.out.print("Enter the second number to multiply: ");
num2 = in.nextDouble();
System.out.println();
answer = num1 * num2;
System.out.println(num1 + " x " + num2 + " = " + answer);
System.out.println();
}
else if(maininput.equals("/")) {
System.out.print("Enter the first number to divide: ");
num1 = in.nextDouble();
do {
System.out.print("Enter the second number to divide: ");
num2 = in.nextDouble();
System.out.println();
if (num2 == 0) {
System.out.println("Cannot divide by 0! Please enter a different number.");
}
} while (num2 == 0);
answer = num1 / num2;
System.out.println(num1 + " / " + num2 + " = " + answer);
System.out.println();
}
else if(maininput.equals("Q") || maininput.equals("q") || maininput.equals("EXIT") || maininput.equals("exit")) {
in.close();
System.exit(0);
}
else {
System.out.println(maininput + " is not a valid operand. Please try again.");
System.out.println();
}
} while (maininput != "Q" && maininput != "q");
This is what I want the output to be:
Enter operation:
4 * 6
4 * 6 = 24
Should be able to enter any operation here on one line. I am not asking you to write my calculator for me, I am asking how to allow the computer to read in the entire operation off one line and compute it, then print it.
If you use scanner readLine then you can read a whole line
e.g.
4 * 6
This line can then be split to get three tokens
String tokens [] = line.split (" ");
then you can see what operation to do based upon token[1]
if (token[1].equals ("-") {
//lets minus token[2] from token[0]
// need to convert String to Number
}
You can use String.split and store it in an array. Then it will return an array of string, parse those back to integers. the do the operation you want. The x variable will be the result.
if(maininput.contains("+")) {
String[] stringarr = string.split("\\+");
int x = Integer.parseInt(stringarr[0]) + Integer.parseInt(stringarr[1]);
System.out.println(stringarr[0] + " + " + stringarr[1] + " = " + x);
} else if(maininput.contains("-")) {
String[] stringarr = string.split("\\-");
int x = Integer.parseInt(stringarr[0]) - Integer.parseInt(stringarr[1]);
System.out.println(stringarr[0] + " - " + stringarr[1] + " = " x);
}
... And so on.
You could try parsing the line using a Pattern object, something like this:
Pattern opPattern = Pattern.compile("(\\d+) *([+-*/]) *(\\d+)");
Matcher matcher = opPattern.matcher(userLine);
if(matcher.find()) {
int op1 = Integer.toValue(matcher.group(1));
int op2 = Integer.toValue(matcher.group(3));
String op = matcher.group(2);
if(op.equals("+")) {
// do + op ...
} else ... {
// etc...
}
} else {
// error in line, not the form of an operation
}
Have a look at the javadoc, as I'm not sure if I used the correct method names and the like, just tried to illustrate the idea...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried doing a loop, but the "{" symbols prevent the all the print lines from repeating I think. I want the loop to redisplay the entire first string after the user finishes entering values for the sequence.
import java.util.Scanner;
public class FtoC{
static Scanner cin = new Scanner (System.in);
public static void main(String[] args)
{
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");
userInput = cin.next().charAt(0);
if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
}
}
private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");
}
private static void far2cel() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");
}
private static void print(String string) {
System.out.print("\n" + string);
}
}
I don't think that I get what you want exactly, but this way you can run the program until user enters something different than "F", "f", "C", "C" in the first part of the program.
import java.util.Scanner;
public class FtoC{
static Scanner cin = new Scanner (System.in);
public static void main(String[] args)
{
while(true) {
char userInput = ' ';
System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
+ "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit"
+ "\nEnter something else to quit." +"\n");
userInput = cin.next().charAt(0);
if ((userInput == 'F') || (userInput =='f'))
{print("Enter a temp in Fahrenheit"
+"\n I will tell you temp in Celsius" +"\n");
far2cel();
}
else if ((userInput == 'C') || (userInput =='c')) {
print("Enter a temp in Celsius:"
+"\n I will tell you temp in fahrenheit" +"\n");
cel2far();
} else {
print("Terminating the program" + "\n");
break;
}
}
}
private static void cel2far() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
+ " Fahrenheit");
}
private static void far2cel() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
+ " celsius");
}
private static void print(String string) {
System.out.print("\n" + string);
}
}
I have placed it into while loop and added "break;" in the "Terminating program" part.