0
import java.util.Scanner;
class G2Ex3 {
public static void main (String args[]) {

Scanner in = new Scanner (System.in);
System.out.print("Enter password: ");
String pw = in.nextLine();

switch (pw) {
case "JRU":
case "jru":
System.out.println("Password Accepted!");
break;
default:
System.out.println("Invalid Password!");
}
}

}

My code, as you can see has nothing to do with integers. My question is why does the program keeps telling me that it only founds java.lang.String and that it requires int? Thank you.

1 Answers1

0

You cannot switch over String, unless you're using Java 7.

In Java 6 or lower, allowed arguments to switch are: int, char, short, byte or an Enum.

Michał Rybak
  • 8,648
  • 3
  • 42
  • 54
  • you can use it with jdk 7. – Kick Buttowski Dec 11 '13 at 01:01
  • I've tried using char and it too don't work. I guess I have to switch to the higher version of Java then. Thank you! – Arnold Toong Dec 11 '13 at 01:16
  • @MichałRybak I've changed the - case "JRU": , into - case 'JRU' because its a chartype. But why does it still gives me the error? – Arnold Toong Dec 11 '13 at 01:33
  • `char` type contains a single character, therefore 'JRU' is not a valid `char`. I suggest reading more about [types in Java](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) to get a solid base before trying to write more complex code. – Michał Rybak Dec 11 '13 at 01:43
  • check your java version. http://www.java.com/en/download/help/version_manual.xml – Kick Buttowski Dec 11 '13 at 01:44
  • @SotiriosDelimanolis but how will use it? I've looked for examples on the internet, but its too complicated to follow – Arnold Toong Dec 11 '13 at 01:59