-1

What I've been trying to do:

-Ask for the temperature

-Ask what to convert to ("C", "c", "F", "f")

It compiles fine, but when it comes for the second question to be asked, I can't input anything, which doesn't allow me to move on. Any help? Thanks!

import java.util.Scanner;

public class TemperatureConversionSelection
{
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a temperature in degrees (for example 29.6): ");
    double temp;
    temp = keyboard.nextInt();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter;
    letter = keyboard.nextLine();

    if (letter == "F"){
       double total = (9)*(temp)/(5) + (32);
    System.out.println(temp + " degrees F = " + total + "degrees Celsius");}
    if (letter == "f"){
        double total = (9)*(temp)/(5) + (32);{
    System.out.println(temp + " degrees F = " + total + "degrees Celsius");}
    if (letter == "C"){
        double total2 = (5)*(temp - 32)/(9);
    System.out.println(temp + " degrees C = " + total + "degrees Fahrenheit");}
    if (letter == "c"){
        double total2 = (5)*(temp - 32)/(9);
    System.out.println(temp + " degrees C = " + total + "degrees Fahrenheit");}
    }
}
}
Matt
  • 15
  • 1
  • 3
  • 2
    But also http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Savior Apr 15 '16 at 00:59
  • You don't compare `Strings` with `==` as this is a test for reference equality. You use `String.equals(String)` or `String.compareTo(String)`. – robotlos Apr 15 '16 at 01:03

2 Answers2

1

The issue is that Scanner.nextInt() method does not consume the last newline character of your input

You need to consume the last newline with keyboard.nextLine();

Try this instead:

    temp = keyboard.nextInt();

    //Consume the newline that nextInt left
    keyboard.nextLine();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter;
    letter = keyboard.nextLine();

Sample Run:

run:
Enter a temperature in degrees (for example 29.6):
5
Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius:
f
5.0 degrees F = 41.0degrees Celsius


Couple other things: You don't compare Strings with == as this is a test for reference equality. You use String.equals(String) or String.compareTo(String). For example, your line

if (letter == "F"){

should be

if (letter.equals("F")){

temp is a double, so you need to replace line

temp = keyboard.nextInt();

with

temp = keyboard.nextDouble();

You have repetitive code, you don't need 2 separate if blocks to check for upper and lower case letters, you can do it with one like so:

    if (letter.equals("F") || letter.equals("f")) {

Practice code formatting, this will make it easy to catch things like the following:
You'll notice that the following code

if (letter == "f"){
    double total = (9)*(temp)/(5) + (32);{
System.out.println(temp + " degrees F = " + total + "degrees Celsius");}

has an extra { on line double total = (9)*(temp)/(5) + (32);{

This will make it so that your code checking for the letter C/c will never be reached. Fix it by properly indenting and removing the extra { like so:

    if (letter == "f") {
        double total = (9) * (temp) / (5) + (32);
        System.out.println(temp + " degrees F = " + total + "degrees Celsius");
    }

Practice good code syntax so that it's easy to read. For example line

   double total = (9)*(temp)/(5) + (32);

would be much easier to read (and make more sense) if you wrote it as such:

   double total = 9 * (temp / 5) + 32;

In this case you actually don't even need parentheses since the order of operations will handle evaluating the equation correctly, so you could just write:

   double total = 9 * temp / 5 + 32;

Less parentheses make it easier to understand.

robotlos
  • 526
  • 2
  • 10
0

In Java, strings are compared like this:

String whatever = "hi";
if(whatever.equals("hi"){
    //do stuff
}
else{
    //do other stuff
}

Just correct if statements and it should work.

EDIT:

I didn't address your issue of the skipped input. I don't think that the input is actually skipped. It takes the input, and tries comparing it in your if-statements. However, because the if-statements are written incorrectly, it returns false for all of them, and then goes to the end of the program.

EDIT2:

Sorry about my incorrect assumption there. I tried running myself and I got the code below to work:

import java.util.Scanner;

public class temp
{
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a temperature in degrees (for example 29.6): ");
    double temp;
    temp = keyboard.nextDouble();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter = keyboard.next();
    double total = 0;

    if (letter.equals("F")){
        total = (9)*(temp)/(5) + (32);
        System.out.println(temp + " degrees F = " + total + " degrees Celsius");}
    if (letter.equals("f")){
        total = (9)*(temp)/(5) + (32);
        System.out.println(temp + " degrees F = " + total + " degrees Celsius");}
    if (letter.equals("C")){
        total = (5)*(temp - 32)/(9);
        System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");}
    if (letter.equals("c")){
        total = (5)*(temp - 32)/(9);
        System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
    }
}

}

a.deshpande012
  • 677
  • 8
  • 18
  • 1
    This is one problem, but doesn't address the issue they're asking about (concerning skipped input). – Savior Apr 15 '16 at 01:09
  • I don't think there is any skipped input. It takes the input, but because it isn't compared correctly, it fails all the if statements, and then ends the program. – a.deshpande012 Apr 15 '16 at 01:12
  • _but when it comes for the second question to be asked, I can't input anything_ Look at `nextInt` and `nextLine`. But, _really_, don't answer questions which have obvious duplicates. – Savior Apr 15 '16 at 01:13
  • You know you could just run their code and see for yourself if it's skipped or not. If you don't have a compiler and runtime environment handy, ideone will do the trick. – Savior Apr 15 '16 at 01:20
  • 1
    sorry I assumed that it would work. I tried running it, and keyboard.next() works. – a.deshpande012 Apr 15 '16 at 01:31