Ryan Lenig
Ryan Lenig

Reputation: 39

Temperature incorrect user input

Repeat the temperature programming project adding a loop that allows the user to continue entering in temperatures, until they enter a Q or q to exit that portion of the program. The user should enter a Q or q to exit and an empty String to continue. (Any other entry should cause the question to be repeated) If the user enters a different letter that F or C(either upper or lower case) for the temperature, print an error message and ask the user to enter the correct temperature scale without asking for a new numeric value.

This is what I have so far....I cannot get it to read whether or not they entered 65D instead of 65C. And I can't figure out how to loop it back through the code if they enter D instead of F/f or C/c...

import java.util.Scanner;
public class AssignmentFour {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Hello, I can convert Fahrenheit to Celsius!");
    System.out.println("Please enter the temperature you want converted.");
    System.out.println("Followed by: 'C/c' or 'F/f'");

    String input2 = keyboard.next();

    do{
        String temp = input2.trim();
        String degreesAsString = temp.substring(0, temp.length()-1);
        double degrees = Double.parseDouble(degreesAsString);

        if(temp.endsWith("C") || temp.endsWith("c")){            
            double degreeF = 9 * degrees / 5 + 32;
            System.out.println(input2 + " is equal to: ");
            System.out.printf("%.2f", degreeF);
            System.out.println(" Fahrenheit.");

        } else if(temp.endsWith("F") || temp.endsWith("f")){
            double degreeC = 5 * (degrees - 32) / 9;
            System.out.println(input2 +" is equal to: ");
            System.out.printf("%.2f", degreeC);
            System.out.println(" Celsius.");

        } else if (temp.endsWith(""));{
            System.out.println("ERROR: Please enter either 'F/f or C/c'.");     
        }
        break;
    } while(!input2.equals(""));

    System.out.println("");
}

Upvotes: 0

Views: 193

Answers (2)

itwasntme
itwasntme

Reputation: 1462

Consider additional method below.

package lala;
import java.util.Scanner;  
public class AssignmentFour {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the temperature you want converted.");
System.out.println("Followed by: 'C/c' or 'F/f'");

String input2 = keyboard.next();

do{
    String temp = input2.trim();
    System.out.println(temp);
    String degreesAsString = deg(temp);
    System.out.println(degreesAsString);
    double degrees = Double.parseDouble(degreesAsString);

    if(temp.endsWith("C") || temp.endsWith("c")){            
        double degreeF = 9 * degrees / 5 + 32;
        System.out.println(input2 + " is equal to: ");
        System.out.printf("%.2f", degreeF);
        System.out.println(" Fahrenheit.");

    } else if(temp.endsWith("F") || temp.endsWith("f")){
        double degreeC = 5 * (degrees - 32) / 9;
        System.out.println(input2 +" is equal to: ");
        System.out.printf("%.2f", degreeC);
        System.out.println(" Celsius.");

    }else if(temp.equals("q") || temp.equals("Q")){

    } else{
        System.out.println("ERROR: Please enter either 'F/f or C/c'.");     
    }
    System.out.println("\nProvide new temperature:");
    input2=keyboard.next();
} while(!input2.equals(""));

System.out.println("");
}

public static String deg(String s){
if(s==null || "".equals(s)) return "";
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length();i++){
    if(Character.isDigit(s.charAt(i))) sb.append(s.charAt(i));
}
System.out.println(sb.toString());
return sb.toString();
}
}

Upvotes: 0

KtorZ
KtorZ

Reputation: 869

just move your ... = keyboard.next() inside your do-while loop to read new user inputs when you're looping.. Also, you should remove the break; statement that is currently just preventing from any loop to be done.

Upvotes: 1

Related Questions