I updated your code a bit. You are now able to Type 15F, 15C, ... (without whitespace) to convert. After this you will be asked if you want to continue.
public static void main(String[] args) {
double deg = 0;
Scanner input = new Scanner(System.in);
do {
System.out
.println("Enter a number and F to convert from degF to degC or C to convert to degC to degF");
String text = input.next().toUpperCase();
try {
deg = Double.parseDouble(text.substring(0, text.length() - 1));
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (text.endsWith("C")) {
double cTemp = (deg - 32) * 5.0 / 9.0;
System.out.printf("%f degC converted to degF is %.2f%n", deg, cTemp);
;
} else if (text.endsWith("F")) {
double fTemp = (deg * 9 / 5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n", deg, fTemp);
continue;
} else {
System.out
.println("That character does not correspond to a valid unit of measure ");
}
System.out.println("Type YES to continue");
} while (input.next().equalsIgnoreCase("YES"));
input.close();
}
Answer from nidomiro on Stack OverflowI updated your code a bit. You are now able to Type 15F, 15C, ... (without whitespace) to convert. After this you will be asked if you want to continue.
public static void main(String[] args) {
double deg = 0;
Scanner input = new Scanner(System.in);
do {
System.out
.println("Enter a number and F to convert from degF to degC or C to convert to degC to degF");
String text = input.next().toUpperCase();
try {
deg = Double.parseDouble(text.substring(0, text.length() - 1));
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (text.endsWith("C")) {
double cTemp = (deg - 32) * 5.0 / 9.0;
System.out.printf("%f degC converted to degF is %.2f%n", deg, cTemp);
;
} else if (text.endsWith("F")) {
double fTemp = (deg * 9 / 5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n", deg, fTemp);
continue;
} else {
System.out
.println("That character does not correspond to a valid unit of measure ");
}
System.out.println("Type YES to continue");
} while (input.next().equalsIgnoreCase("YES"));
input.close();
}
You are taking the USER input only one time so i m not sure why you are using do-while loop.Also if you are trying to iterate on the basis on YES\NO then add the inputs in the do loop and add one for input about YES/NO and add this check in while.
Use below code :
public class Temperature
{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
double deg;
char dg;
String con;
do{
System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF and YES to continue");
deg= input.nextDouble();
String letter = input.next().toUpperCase();
dg = letter.charAt(0);
con = input.next();
if( dg == 'C'){
double cTemp= (deg-32)*5.0/9.0;
System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
}
else if(dg == 'F'){
double fTemp = (deg*9/5) + 32;
System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
continue;
}
else{
System.out.println("That character does not correspond to a valid unit of measure ");
break;
}
}while(con.equalsIgnoreCase("YES"));
} }
I am having trouble with Java Basics
variables - Prompt for user yes or no input in Java - Stack Overflow
While Loop not working for a yes or no question
Trouble creating a java loop to loop based on yes or no input - Stack Overflow
- If you don't use Java 7 you can't use switch-strings
Change
while (true)withwhile (yn)so it will stop when he type "no", and changeboolean yn;toboolean yn = true;And change the rules inside the cases too.case "yes": yn = false; break; case "no": yn = true; break;yn = true;if"yes";yn = false;if"no";You could change the condition inside the while, with
while (!yn)but is more intuitive to letyntrueif yes;falseif no.return default;don't make much sense, if you want to let user repeat in case of error well.. you should do a newwhile (true)to repeat until he writes a correct one. I would write another method.
This is how you could do it
Scanner kbd = new Scanner (System.in);
String decision;
boolean yn = true;
while(yn)
{
System.out.println("please enter your name");
String name = kbd.nextLine();
System.out.println("you entered the name" + name );
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch(decision)
{
case "yes":
yn = true;
break;
case "no":
yn = false;
break;
default:
System.out.println("please enter again ");
boolean repeat = true;
while (repeat)
{
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch (decision)
{
case "yes":
yn = true;
repeat = false;
break;
case "no":
yn = repeat = false;
break;
default:
repeat = true;
}
}
break;
}
}
Yes it will repeat decision code, but how it is created i think it is the only way to do it.
Yes, but you'd want while(yn), not while(true), which goes on forever. The break only breaks from the switch statement.
Alright, try this:
import java.util.Scanner;
public static void main(String args[]){
Scanner s = new Scanner(System.in);
boolean checking = true, valid = true;
String[] names = new String[50];
int i = 0;
while(checking){
System.out.println("Enter name...");
me = s.nextLine();
System.out.println("You entered " + me + ".");
while(valid){
System.out.println("Enter another? y/n");
you = s.nextLine();
if(you.equals("n")){
valid = false;
checking = false;
}else if you.equals("y")){
names[i] = you;
i++;
valid = false;
}else{
System.out.println("Sorry, try again (y/n)...");
}
}
}
}
Creating a tax calculator that lets users set their own custom tax rate if they wish or it will set to a default rate, but my while loop doesn't work.
System.out.println("Would you like to set a custom Tax Rate?");
answer = ScanUserInput.nextLine();
while (answer != "Yes" && answer != "No") {
System.out.println("Invalid input, please try again");
if (answer.equals("Yes")) {
System.out.println("Please enter your custom Tax Rate");
defaultTaxRate = ScanUserInput.nextDouble();
} else {
System.out.printf("Default Tax Rate of " + defaultTaxRate + " will be applied.");
If the user enters something that is not "Yes" or "No" it gives the message "Invalid input, please try again" but keeps producing the message over and over again on the console.
How do I get it to display only once?
Also if the user answers yes or no, it still gives the message, however only once, and then goes on to display the corresponding message for either yes or no, any tips?
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*;
{
public static void main(String args[]) throws ParseException{ Scanner input = new Scanner(System.in); System.out.print("Enter first time (hh:mm a/pm): "); String time = input.nextLine(); System.out.println(); System.out.print("Enter second time (hh:mm a/pm): "); String time2 = input.nextLine(); System.out.println(); DateFormat simpleDF = new SimpleDateFormat ("hh:mm aa"); //specifying time format using time format syntax Date date1 = simpleDF.parse(time); Date date2 = simpleDF.parse(time2);
System.out.println("Time is: " + simpleDF.format(date1));
System.out.println("Time is: " + simpleDF.format(date2));
if(date1.after(date2)){ //creating conditions for output
long diffMs = date1.getTime() - date2.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes.");
}
if(date1.before(date2)){
long diffMs = date2.getTime() - date1.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes.");
}
if(date1.equals(date2)){
System.out.println("There is no difference.");
}
// if(!(cont.equalsIgnoreCase("Yes") || (cont.equalsIgnoreCase("No")))){
System.out.println();
System.out.println("Do you wish to continue? (Yes/No) ");
String cont = input.next();
if(!(cont.equalsIgnoreCase("Yes") || (cont.equalsIgnoreCase("No")))){
System.out.println("Invalid option.");
System.out.println();
System.out.println("Enter valid option: (Yes/No)");
cont = input.next();
}
if(cont.equalsIgnoreCase("Yes")){
}
if(cont.equalsIgnoreCase("No")){
System.out.println("Thanks, that is all. ");
}
}
}Please, read and follow the Posting rules and also the Code Posting rules.
You keep making low effort, code only posts that are not even properly formatted. This behavior is unacceptable when asking for help.
The Posting Rules mentioned above explain how a proper post needs to be made. The Code posting rules explain how to post code.
Since this is not the first time you do this, consider it a warning. Further similar behavior will lead to consequences such as a permanent or temporary ban from this subreddit.
Where it says '"Do you wish to continue? (yes/no)" @ line #24, when entered "Yes", how can I make it so my program loops the entire program? Help would be highly appreciated, sooner than later if possible :d
Your code only checks the first character of the input, so it's no wonder words starting with y or n are considered valid. You might want to compare the entire String :
String response = keyboard.next();
while (!response.equalsIgnoreCase("y") && !response.equalsIgnoreCase("n")) {
System.out.println("\nInvalid response. Try again.");
response = keyboard.next();
}
if (response.equalsIgnoreCase("n")) {
System.out.println("\nCome back next time, " + customerName + ".");
} else {
System.out.println("\nGreat! Let's get started.");
}
The issue is that you are checking only the first letter of the read string:
char response = keyboard.next().charAt(0)
You should read the whole string:
String response = keyboard.next()
And use it in the comparison. In order to ensure that also 'Y' and 'N' are considered valid, you can use the String.equalsIgnoreCase(String):
while (!"Y".equalsIgnoreCase(response) && "N".equalsIgnoreCase(response))
System.out.println("\nWould you like to order some coffee, " + customerName + "? (y/n)");
So, wrapping all together this would look like this:
String response = keyboard.next();
while (!"Y".equalsIgnoreCase(response) && "N".equalsIgnoreCase(response)) {
System.out.println("\nInvalid response. Try again.");
response = keyboard.next();
}
if ("N".equalsIgnoreCase(response)) {
System.out.println("\nCome back next time, " + customerName + ".");
} else if ("Y".equalsIgnoreCase(response)) {
System.out.println("\nGreat! Let's get started.");
}