8 Replies - 6702 Views - Last Post: 12 October 2012 - 05:54 PM Rate Topic: -----

#1 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Error: Incompatible types Found: java.lang.String Required: int

Posted 11 October 2012 - 08:41 PM

I created this code in class and it compiled just how I have it written, but now I am home wanting to add a couple more things to it and I keep getting these errors:
RockPaperScissors.java:17: incompatible types
found   : java.lang.String
required: int
		switch(userChoice){
		       ^
RockPaperScissors.java:19: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
RockPaperScissors.java:25: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
RockPaperScissors.java:31: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
RockPaperScissors.java:41: incompatible types
found   : java.lang.String
required: int
			switch(userChoice){
			       ^
RockPaperScissors.java:44: incompatible types
found   : java.lang.String
required: int
				switch(computerChoice){
				       ^
RockPaperScissors.java:50: incompatible types
found   : java.lang.String
required: int
				switch(computerChoice){
				       ^
RockPaperScissors.java:56: incompatible types
found   : java.lang.String
required: int
				switch(computerChoice){
				       ^
RockPaperScissors.java:65: incompatible types
found   : java.lang.String
required: int
		switch(userChoice){
		       ^
RockPaperScissors.java:67: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
RockPaperScissors.java:73: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
RockPaperScissors.java:79: incompatible types
found   : java.lang.String
required: int
			switch(computerChoice){
			       ^
12 errors



I've been looking up anything to help me but I can't seem to figure it out...I know it may be something simple that I am over thinking, but anyone have any insight on this?
Like I said, my code is not yet done. I still need to add random and a return but here is my code thus far:
import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors
{
	
	public static void main(String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
	
	String winner = "computer";
	String computerChoice = "paper";
	String userChoice = "rock";
	System.out.println("Enter your choice");
	userChoice = keyboard.next();
		
		switch(userChoice){
		case "rock":
			switch(computerChoice){
			case "scissors":
			winner = "user";
			System.out.println("Computer chose scissors");
			break;
			}
			switch(computerChoice){
			case "paper":
			winner = "computer";
			System.out.println("Computer chose paper");
			break;
			}
			switch(computerChoice){
			case "rock":
			winner = "tie";
			System.out.println("Computer chose rock");
			break;
			}
			
			System.out.println("The winner is " + winner);
			}
			
			switch(userChoice){
		
			case "paper":
				switch(computerChoice){
				case "rock":
				winner = "user";
				System.out.println("Computer chose rock");
				break;
				}
				switch(computerChoice){
				case "scissors":
				winner = "user";
				System.out.println("Computer chose scissors");
				break;
				}
				switch(computerChoice){
				case "paper":
				winner = "tie";
				System.out.println("Computer chose paper");
				break;
				}
				System.out.println("The winner is " + winner);
				}
			
		switch(userChoice){
		case "scissors":
			switch(computerChoice){
			case "paper":
			winner = "user";
			System.out.println("Computer chose paper");
			break;
			}
			switch(computerChoice){
			case "rock":
			winner = "computer";
			System.out.println("Computer chose rock");
			break;
			}
			switch(computerChoice){
			case "paper":
			winner = "tie";
			System.out.println("Computer chose paper");
			break;
			}
			System.out.println("The winner is " + winner);
			}
	}
}



Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Error: Incompatible types Found: java.lang.String Required: int

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 11 October 2012 - 08:50 PM

If your JDK is 1.6 or lower you can't switch() on a String

P.S.
switching on a String is probably not the best solution
Was This Post Helpful? 0
  • +
  • -

#3 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 11 October 2012 - 08:59 PM

Yeah in java 1.6 you can only switch int, boolean, double, long, and float I believe.
Was This Post Helpful? 0
  • +
  • -

#4 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 11 October 2012 - 09:46 PM

Got it thanks for the tip
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,785
  • Joined: 27-December 08

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 11 October 2012 - 09:51 PM

View PostYtry, on 11 October 2012 - 11:59 PM, said:

Yeah in java 1.6 you can only switch int, boolean, double, long, and float I believe.

Not even those. Just int, byte, short, and char.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 12 October 2012 - 04:29 AM

int which may be an enum constant of course :)
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 12 October 2012 - 04:34 AM

I would suggest you to to change your print statement to something like this.

  System.out.println("Enter your choice in the following way.");
  System.out.println("1.rock 2.Paper 3.Scissors");



Now, use the values of 1,2 and 3 in your switch statements so you can avoid string altogether. :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 jmart  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 29-September 12

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 12 October 2012 - 05:50 PM

How do I get it to pick a random choice for the computer?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,785
  • Joined: 27-December 08

Re: Error: Incompatible types Found: java.lang.String Required: int

Posted 12 October 2012 - 05:54 PM

See the Random class: http://docs.oracle.c...til/Random.html

int choice = rand.nextInt(3) + 1; //rand.nextInt(3) generates a random from 0-2


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1