-1

Background: Beginner in programming, very new to this whole "Java" thing.

I love the help, but I do not want a direct answer, more like a point in the right direction, or any which way where I can learn instead of blindly copy/paste. Instead of "heres the right code" more of "hears how you can get the right code" Thank you :)

Ok so my question is, what is wrong with the prompt ad the IF/THEN statement. When I run it with the prompt, it says cannot find symbol - method prompt(java.lang.String) Without the prompt, when I run it, after I input my choice, whether right or wrong, it always returns "Sorry, that is'nt a choice. Choose rock, paper or scissors!" even if it is right!

If you need any more info on my problem, let me know :) Anyway here is the class:

//Player class 
import java.util.Scanner;
import java.lang.String;

public class Player
{
 private String name;
 private String choice;

 public Player(String nm)
 {
     name = nm;
 }  
 
 public Player(String nm, String ch)
 {
     name = nm;
     choice = ch;
 } 
  
 public void setName( String nm)
 {
     name = nm;
 } 
  
 public void setChoice( String ch )
 {  
 } 
  
 public String getChoice()
 {
  Scanner scan = new Scanner (System.in);
        System.out.println("Choose rock, paper or scissors:");
        String player = scan.next(); player.toLowerCase();

     if ((player != ("rock"))
        || (player != ("paper"))
        || (player != ("scissors")))
        {
            System.out.println("Sorry, that is'nt a choice. Choose rock, paper or scissors!");
            player = prompt("Choose rock, paper or scissors:");
        }
        else
        {
            System.out.println("Good choice!");
        }
     System.out.println("You chose " + player);
        return "";
     

 } 
  
 public String getName()
 {
        Scanner scan = new Scanner (System.in);
        System.out.println("Whats your name?");
        String name = scan.next(); 
     return "";
 }
 
 public String toString()
 {
  return "";
 }
}

And the runner:

public class PlayerRunner
{
 public static void main(String[] args)
 {
  Player s = new Player("Michael Jackson", "rock");

  
  System.out.println(s.getChoice());  
  System.out.println(s.getName()); 
  //outs rock
  //call the getName() method
  System.out.println(s);        //outs Michael Jackson rock
  
  //set the choice to paper
  System.out.println(s);        //outs Michael Jackson paper 
  
  //instantiate a new Player named jb named Jim Bob that chose scissors
  //print out Jim Bob 
 }
}
The goal of this portion was titled to: "Use the Player.java file to create the player. Complete the Player constructors, set methods, get methods, and the toString. Use PlayerRunner.java to test your Player class."

Let me know if you see that I did anything else wrong :) Again I'm a beginner, but I'm here to learn, not just paste the right answer and move on. Thank you!!

Nicole I.
  • 87
  • 1
  • 3
  • 10
  • Without going through all your code, the first obvious issue I see is answered here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – John3136 Feb 02 '15 at 00:33
  • 1
    Well, for one, you're using `==` and `!=` for `String` (and, in general, anything that isn't a number or boolean). That's a big no-no in Java. You need to use the `.equals()` method. Try fixing that first and then update your question as needed. – aruisdante Feb 02 '15 at 00:33
  • AHA! Yes thank you both! I thought I had searched this site through and through makes a lot of sense thank you! – Nicole I. Feb 02 '15 at 00:36

2 Answers2

1

How about:

public String getChoice() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Choose rock, paper or scissors:");
        String player = scan.next();

        while ((!player.equalsIgnoreCase("rock"))
                && !player.equalsIgnoreCase("paper")
                && !player.equalsIgnoreCase("scissors")) {
            System.out.println("Sorry, that is'nt a choice. Choose rock, paper or scissors!");
            System.out.println("Choose rock, paper or scissors:");
            player = scan.next();
        }

        System.out.println("Good choice!");
        System.out.println("You chose " + player);
        return "";
    }
0

When you use the == operator, you're comparing whether or not two objects are the same, or whether two primitive type's values are the same.

Since a string is actually an object in Java,

String a = "string";
String b = "string";
System.out.println(a == b);

will return false, but

String a = "string";
String b = a;
System.out.println(a == b);

will return true. Instead, if you call the equals method of the String class, you can compare whether or not the two values are the same.

String a = "string";
String b = "string";
System.out.println(a.equals(b));

In your case, player != ("rock") will always evaluate as true, which looks to be causing some problems.

You can read more here: Java String.equals versus ==

Community
  • 1
  • 1
Joseph Nields
  • 5,527
  • 2
  • 32
  • 48