2

I recently started learning java during my spare time. So to practice, I'm making a program that takes a temperature (Celsius or Fahrenheit) and converts it to the opposite. I've already imported the keyboard scanner.

    int temp;
    String opposite, type;
    double product;

    System.out.print("Please enter a temperature: ");
    temp = keyboard.nextInt();

    System.out.println("Was that in Celsius or Fahrenheit?");
    System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
    type = keyboard.nextLine();

    if (type == "C") // Only irrelevant temp conversion code left so I'm leaving it out

I'm new to the String and nextLine stuff and the program just skips over the user input section where you enter either C or F. Would someone explain what I can do to fix this?

Thanks!

Razib
  • 10,965
  • 11
  • 53
  • 80
js9999
  • 66
  • 1
  • 1
  • 8
  • 1
    Also, see [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PM 77-1 Apr 15 '15 at 02:46

7 Answers7

1

For you code Change nextLine(); to next(); and it will work.

System.out.println("Was that in Celsius or Fahrenheit?");
    System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
    type = keyboard.next();

to get an idea for you to what happened was this:

  • nextLine(): Advances this scanner past the current line and returns the input that was skipped.
  • next(): Finds and returns the next complete token from this scanner.

Also like the many of the answers says use equals() instead of using ==

The == checks only the references to the object are equal. .equal() compares string.

Read more Here

Community
  • 1
  • 1
Anjula Ranasinghe
  • 584
  • 1
  • 9
  • 24
0

.nextInt() does not read the end of line character "\n".

You need to put a keyboard.nextLine() after the .nextInt() and then it will work.

Undo
  • 25,519
  • 37
  • 106
  • 129
beatyt
  • 98
  • 1
  • 9
0

Never use Scanner#nextLine after Scanner#nextInt. Whenever you hit enter button after Scanner#nextInt than it will skip the Scanner#nextLine command. So, Change from

 int temp = keyboard.nextInt();

to

 int temp = Integer.parseInt(keyboard.nextLine());
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Also, use type.equals("C") instead of if (type == "C"), the later one is comparing the reference of the value.

Undo
  • 25,519
  • 37
  • 106
  • 129
PWC
  • 266
  • 3
  • 11
0

Call

  keyboard.nextLine();

After

  temp = keyboard.nextInt();

Because nextInt() doesn't consume the \n character.

Also, compare Strings with .equals(); not ==

if(type.equals("C"));
EDToaster
  • 3,160
  • 3
  • 16
  • 25
0

Use Scanner Class:

int temp;
java.util.Scanner s = new java.util.Scanner(System.in);
String opposite, type;
double product;

System.out.print("Please enter a temperature: ");
temp = s.nextInt();

System.out.println("Was that in Celsius or Fahrenheit?");
System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
type = s.nextLine();

if (type.equals("C")){
do something
}
else{
do something
}

For More Input references:

Scanner

BufferedReader

String

Here is a wonderful comparison on how to compare strings in java.

Community
  • 1
  • 1
vishu9219
  • 761
  • 6
  • 14
0

You could use keyboard.next() instead of nextLine() and as

user1770155

mentioned to compare two strings you should use .equals() and what I would do since you're comparing to an upper letter "C" is

 type = keyboard.next().toUpperCase();



if (type.equals("C"))
STIKO
  • 1,995
  • 1
  • 10
  • 9