-2

What I want to do is simply read a input (Char/String , preferably char), its a single char and then print some outputs based on the inputs. My problems faced is that, if i convert my input 'choice' into a char, my error messages are:

Type mismatch: cannot convert from String to char Incompatible operand types char and String

any idea whats wrong? Thanks!

*If I leave it like this it just gives me "Invalid Choice"

import java.util.*;

public class P1 {

    public static void main(String[] args) {

        // Create a scanner
        Scanner userInputScanner = new Scanner(System.in);
        String choice = userInputScanner.nextLine();

        System.out.println("Your choice is " + choice);

        if ((choice == "A") || (choice == "a"))
            System.out.println( " Action Movie Fan"); 
             else if ((choice == "C") || (choice == "c")) 
                 System.out.println( " Comedy movie fan ");
            else if ((choice == "D") || (choice == "d")) 
                 System.out.println(" Drama movie fan "); 
             else 
                 System.out.println( "Invalid choice" );


    }

}
user2999509
  • 97
  • 2
  • 4
  • 13
  • 1
    I don't think you get an error with the snippet of code you provided. But use `equals()` (`equalsIgnoreCase()` would be better for you in this case) to compare String values. – Alexis C. Jan 19 '14 at 14:00
  • nextLine() will return String if you want input as Char you have to read String and then String.toCharArray().[0] will return you character a first position – praveen_mohan Jan 19 '14 at 14:05

4 Answers4

2

You compare strings in Java using equals:

if ("A".equals(choice) || "a".equals(choice)) {
    ...
}

or equalsIgnoreCase:

if ("A".equalsIgnoreCase(choice)) { // "a".equalsIgnoreCase(choice) works too
    ...
}

However, in your case you need to compare a single character, so you can do this:

if (choice.length() == 1) {
    // Convert to upper case for case insensitivity
    char selection = Character.toUpperCase(choice.charAt(0));
    switch (selection) {
    case 'A':
        ...
        break;
    case 'C':
        ...
        break;
    case 'D':
        ...
        break;
    }
    ...
}
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @user2999509 You are welcome! If your problem is fixed, consider accepting the answer by clicking the grey check mark next to it. This would let other site visitors know that you are no longer actively looking for an improved solution to your problem, and earn you a brand-new badge on Stack Overflow. – Sergey Kalinichenko Jan 19 '14 at 14:25
0

For your case, try to use String.equalsIgnoreCase. You might want to explore usage of Enum as well.

merton
  • 11
  • 2
0
import java.util.*;

public class P1 {

    public static void main(String[] args) {

        // Create a scanner
        Scanner userInputScanner = new Scanner(System.in);
        String choice = userInputScanner.nextLine();

        System.out.println("Your choice is " + choice);

        if (choice.trim().equalsIgnorecase("A"))
            System.out.println( " Action Movie Fan"); 
        else if (choice.trim().equalsIgnorecase("B")) 
            System.out.println( " Comedy movie fan ");
        else if (choice.trim().equalsIgnorecase("D")) 
            System.out.println(" Drama movie fan "); 
        else 
            System.out.println( "Invalid choice" );

    }
}

otherwise compare only first character of string like choice.charAt(0) == 'A'

AndRSoid
  • 1,777
  • 2
  • 13
  • 25
0

What you should know is that "A" and "a" are not chars, they are Strings. What you should do is call String's equals() (and not the == operator) method to compare. Just like that:

if(stringOne.equalsIgnoreCase("A")) {
    //methods
}
Ferdz
  • 1,182
  • 1
  • 13
  • 31