-2

In the below code, during compilation, i get 'incompatible types' error. If I user other logic, like charAt(0) and all, this works fine. Isn't there any way to use string in a java switch statement? I'm using JDK 7.

Thanks.

import java.util.Scanner;

class cCode
{
    public static void Main(String args [])`
    {
        System.out.println("Enter country code\nChoices: IND, USA, JPN, NZ, WI");
        Scanner cc = new Scanner(System.in);
        switch(cc)
        {
            case "IND":
                System.out.println(cc+" refers to INDIA");
                break;  
            case "USA":
                System.out.println(cc+" refers to UNITED STATES");
                break;
            case "JPN":
                System.out.println(cc+" refers to JAPAN");
                break;
            case "NZ":
                System.out.println(cc+" refers to NEW ZEALAND");
                break;
            case "WI":
                System.out.println(cc+" refers to WEST INDIES");
                break;
            default:
                System.out.println("Invalid choice");
        }
    }
}
Sjoerd222888
  • 2,750
  • 3
  • 23
  • 52
Shreyas
  • 3
  • 1
  • 4

2 Answers2

1

I think you want to do this:

String theText = cc.next();
switch(theText) {
   //your code.
}

Hope this solves your problem.

Mubashar Abbas
  • 4,346
  • 2
  • 32
  • 42
  • 1
    I suspect the OP needs to put in a variable as he/she prints it later. – Peter Lawrey Aug 13 '15 at 07:10
  • I also am getting this error: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=4][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negati ve prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q∞\E] refers to NEW ZEALAND – Shreyas Aug 13 '15 at 07:18
  • Fixed it. that was due to cc at SoP. – Shreyas Aug 13 '15 at 07:27
0

You can use below code by taking input from user.

import java.util.Scanner;
public class cCode {

public static void main(String args[]) {

    System.out.println("Enter country code\nChoices: IND, USA, JPN, NZ, WI");
    Scanner cc = new Scanner(System.in);
    String txt = cc.nextLine();
    switch (txt) {
        case "IND":
            System.out.println(txt + " refers to INDIA");
            break;

        case "USA":
            System.out.println(txt + " refers to UNITED STATES");
            break;

        case "JPN":
            System.out.println(txt + " refers to JAPAN");
            break;

        case "NZ":
            System.out.println(txt + " refers to NEW ZEALAND");
            break;

        case "WI":
            System.out.println(txt + " refers to WEST INDIES");
            break;

        default:
            System.out.println("Invalid choice");
   }
}}
Rafiq
  • 722
  • 1
  • 5
  • 15
  • You can see : http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class – Rafiq Aug 13 '15 at 08:15