6 Replies - 3059 Views - Last Post: 03 May 2014 - 09:45 PM Rate Topic: -----

#1 PerfectGreiSky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 14

Rock, Paper, Scissors

Posted 28 April 2014 - 03:19 PM

Hello everyone. I have a problem (again) that maybe someone could assist me with. A while back I wrote a simple rock, paper, scissors program. Simple right? Well recently I've been asked to alter it.

The new format is this:

Choose 3 of: (0=rock, 1=paper, 2=scissors): 011
You chose <rock, paper, paper>
Computer chose <scissors, paper, rock>

import java.util.Scanner;

public class Game {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String personPlay;
		String pChoice = null;
		int x = (int)(Math.random()*3);
		String play;
		
		
		System.out.println("Chose 3 of (0=rock, 1=paper,2=scissors):");
		System.out.println();
		
		for(int i = 0; i < 3; i++){
			personPlay = input.next();
			if(personPlay.length() > 3 || personPlay.length() < 3)
				System.out.println("Invalid Entry.");
				System.exit(0);
			
			if (Character.isDigit(personPlay.charAt(0)) & Character.isDigit(personPlay.charAt(1)) 
					& Character.isDigit(personPlay.charAt(2))){
				if(personPlay.charAt(0)==0){
					pChoice = "Rock";
					if(personPlay.charAt(0)==1){
						pChoice = "Paper";
						if(personPlay.charAt(0)==2){
							pChoice = "Scissors";
						}
					}
				}
				if(personPlay.charAt(1)==0){
					pChoice = "Rock";
					if(personPlay.charAt(2)==1){
						pChoice = "Paper";
						if(personPlay.charAt(2)==2){
							pChoice = "Scissors";
						}
					}
				}
				if(personPlay.charAt(3)==0){
					pChoice = "Rock";
					if(personPlay.charAt(3)==1){
						pChoice = "Paper";
						if(personPlay.charAt(3)==2){
							pChoice = "Scissors";
						}
					}
				}
				System.out.println("You chose: " + pChoice);
			}
			else {
				System.out.println("Invalid entry only use numbers from 0 to 2");
			}// if	
			
		} // for
	}

}




This is the code I have so far. If I could just fix this part I'm sure the rest will come easily. I figured it would be easier to read the input in as a String and then use isDigit to check for the number choices. Was I wrong?
Do I need a array for the selections instead of a for loop? In the end I think I just confused myself... Thanks in advance for any advice given.

Is This A Good Question/Topic? 0
  • +

Replies To: Rock, Paper, Scissors

#2 IvGotAPocket   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 157
  • Joined: 12-August 13

Re: Rock, Paper, Scissors

Posted 28 April 2014 - 06:27 PM

Hey man
if(personPlay.charAt(0)==0){
					pChoice = "Rock";


this is the reason i think you code is not working the way you want it to.
when you do == you are comparing the addresses not the data you need a equals() method to compare data. Also in this case a char !equal to a int.


edit: I think i'm wrong on that == compare because i think when you use charAt() it turns the data into a primitive char. So you could just say string.chatAt(0) == '0'. If someone wants to clarify this for me that would be great.

This post has been edited by IvGotAPocket: 28 April 2014 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 TheChucklingAtom   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-March 12

Re: Rock, Paper, Scissors

Posted 29 April 2014 - 03:42 PM

In my opinion it would probably be easier to just accept the input as an int instead of a string and try to pull the chars. you would have to make sure they actually enter an int but once they have their selection it will be much easier to split it up and would probably look something like this.

package three.digit.pkgint.test;
import java.util.Scanner;
/**
 *
 * @author Atom
 */
public class ThreeDigitIntTest {
static int test;
static int digit1;
static int digit2;
static int digit3;
    
    public static void main(String[] args) {
        //input
        Scanner input = new Scanner(System.in);
        //asking for number
        System.out.println("Enter a three digit number.");
        test = input.nextInt();
        //splitting digit one
        digit1 = test / 100;
        //splitting digit 2
        digit2 = (test - (digit1 * 100)) / 10;
        //splitting digit 3
        digit3 = (test - (digit1 * 100) - (digit2 * 10));
        //confirming the split
        System.out.println("digit1=" + digit1 + "\ndigit2=" + digit2 + 
                "\ndigit3="+ digit3);
    }
}


Also when I tried the charAt(0)=='0' for me it still did not want to evaluate correctly. If you want you can use charAt(0)==48 for the int 0 and you can find what the other chars are equal to here. The only other thing I see is it will run through the loop three times, each time asking you to input another three numbers and each time only telling you the last one. Let me know if this doesn't make any sense.
Was This Post Helpful? 0
  • +
  • -

#4 Joshieboy2007   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 26-March 14

Re: Rock, Paper, Scissors

Posted 29 April 2014 - 10:43 PM

You will need a Array otherwise you will keep replacing personplay.You will need to keep your for loops to assign the person play into the array.

Also i would suggest making person play an int instead of a string.

This post has been edited by Joshieboy2007: 29 April 2014 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Rock, Paper, Scissors

Posted 29 April 2014 - 11:27 PM

There are multiple problems in this code.

I don't get reason behind for loop added, as you can take entire string of 3 options in one go (your if statement immediate below to it also expects same).

Then there is Line 20: System.exit(0); free from any block. This will never let you go ahead! :) Personally I recommend using braces for every block. Even if it's a single line. Saves such things from happening.

Here : if (Character.isDigit(personPlay.charAt(0)) & Character.isDigit(personPlay.charAt(1)) & Character.isDigit(personPlay.charAt(2)))
I would recommend a logical and operator instead of using bitwise and.

Then on this line if(personPlay.charAt(0)==0) you are comparing a character to integer. ascii codes of 0,1 and 2 are not integer 0,1 and 2. So when you try to compare ASCII characters 0,1,2 with integers it's not going to work. Compare with '0','1','2' instead.

Also, there is unnecessary nesting of if statements in those 3 if combinations. All you needed there was 3 single line if statements to compare 1st character of string and determine if it's Rock Paper or Scissor. same with remaining two characters.

That changes will get your code working.
As others said, it can be done with integer as well, but that's up to you.

I saw you are doing error handling as well, so I would suggest to validate for numbers other than 0,1 or 2. But that's after we get this thing working. :)
Was This Post Helpful? 0
  • +
  • -

#6 PerfectGreiSky   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 14

Re: Rock, Paper, Scissors

Posted 30 April 2014 - 08:59 AM

View PostAmitTheInfinity, on 29 April 2014 - 11:27 PM, said:

There are multiple problems in this code.

I don't get reason behind for loop added, as you can take entire string of 3 options in one go (your if statement immediate below to it also expects same).

Then there is Line 20: System.exit(0); free from any block. This will never let you go ahead! :)/>/> Personally I recommend using braces for every block. Even if it's a single line. Saves such things from happening.

Here : if (Character.isDigit(personPlay.charAt(0)) & Character.isDigit(personPlay.charAt(1)) & Character.isDigit(personPlay.charAt(2)))
I would recommend a logical and operator instead of using bitwise and.

Then on this line if(personPlay.charAt(0)==0) you are comparing a character to integer. ascii codes of 0,1 and 2 are not integer 0,1 and 2. So when you try to compare ASCII characters 0,1,2 with integers it's not going to work. Compare with '0','1','2' instead.

Also, there is unnecessary nesting of if statements in those 3 if combinations. All you needed there was 3 single line if statements to compare 1st character of string and determine if it's Rock Paper or Scissor. same with remaining two characters.

That changes will get your code working.
As others said, it can be done with integer as well, but that's up to you.

I saw you are doing error handling as well, so I would suggest to validate for numbers other than 0,1 or 2. But that's after we get this thing working. :)/>/>


Thanks everyone for the for the advice! I didn't even consider that the code was having problems because I was using ascii code. Also there's such a thing as unnecessary nesting? lol I think I did it that way because my instructor is so anal about indention while also being the most unhelpful person when it comes to questions. I think I'm well on the way to fixing this stupid code but I've run into something else. Also, I tried using string because that what was suggested to make the program work. Maybe it's just made things harder for me?
Can someone explain what this means?

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)

it comes from Line 22. Or from this:

if (Character.isDigit(personPlay.charAt(0)) && Character.isDigit(personPlay.charAt(1))
&& Character.isDigit(personPlay.charAt(2))){
Was This Post Helpful? 0
  • +
  • -

#7 TheChucklingAtom   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-March 12

Re: Rock, Paper, Scissors

Posted 03 May 2014 - 09:45 PM

So java.lang.StringIndexOutOfBoundsException tells us exactly what the error is. You can read the exact api text Here. Basically it means that the index is negative or greater than the length of the string. Now the next part. String index out of range: 1. This tells us exactly where it is out of range. At string[1]. Which means the string has a char at position 0, but not at 1. What I would do is run step by step through a debug and see what the value of your string is right before that exception happens. Edit: something else I wanted to note on your code is those nested if statements. Those will cause any number aside from zero to return null. What you should do there instead is an
if(check0){
is 0
}else if(check1){
is1
}else{
is2
}

This post has been edited by TheChucklingAtom: 03 May 2014 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1