so let''s say I have a rock paper scissors program and I want the user to enter ONLY rock, paper and scissors. If the user enters anything else, the program would display an error message and ask the user to try again. How would I do this in java?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
so let''s say I have a rock paper scissors program and I want the user to enter ONLY rock, paper and scissors. If the user enters anything else, the program would display an error message and ask the user to try again. How would I do this in java?
Show us some code that does any of what you describe, and we'll help you add the features you need.
This is quite straight forward name (don't mind if I call you name do you?). It can be split up into 3 main components. So where to begin? Well we need to define what characters you define to be legal. We would do this as follows:
final String[] legal = {"rock", "paper", "scissors"};
Now we have a clean and simple data structure storing all the values you are willing to accept. But how does this translate into checking if a value matches? Well easy... with an array, we dont need to fiddle about with a long column of ifs and elses, we just need a simple loop.
If the input string matches any of the array content, the loop will stop in its tracks and return TRUE. This indicates that a match has been found. However, if the input doesn't match any of the legal keywords, it will exit the loop and return FALSE as default. This method means we can easily and cleanly check for a value inside the loop, and anytime you want to add an extra keyword, all you need to do is add it to the original array.
So we've got the legal keywords, we know how to check if a word matches the keywords, all we need now is the main loop. We start off by using a Scanner object. Using the scanner, we poll the user for an input and once we receive it, we check the loop condition before we re-enter or exit the loop.
Using a do-while loop, we can immediately enter the loop without checking anything, but in-order to exit the loop and not be asked to input another value, the condition must be satisfied. Using the method we made, we can check if the contents of the array match the users input. If its not legal, we re-enter the loop.
Good luck.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
import java.util.*;
class rps{
public static void main(String args[]){
Random number = new Random();
Scanner input = new Scanner(System.in);
// player chooses
String choice;
System.out.println("Enter your throw (rock, paper, scissors)(enter exactly as shown): ");
choice = input.nextLine();
//input validation loop
while(choice!="rock" && choice!="paper" && choice!="scissors"){
System.out.println("error, try again");
System.out.println("Enter your throw (rock, paper, scissors)(enter exactly as shown): ");
break;
}
System.out.println("The player throws "+choice);
Use String.equals(String) to compare Strings. For objects (which a String is), .equals() method compares "equality", while == compares if they are physically the same object in memory.
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
can you show me exactly how to do it?
Did I just give you the code and a full explanation for nothing? >.>
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
Your sort really drive me up the wrong way. Pull your finger out of your arse for once and read.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
That spelling reflects the North Wales pronunciation along with some of the more rural parts of the US.
But let's reset to your problem. Simple input validation is:
// wordy pseudo-code
get input
if input does not match the required values:
- post error message and return to "get input"
otherwise, continue with processing input
What help do you need to program that?
Note that the if statement could be changed to the positive:
if input DOES match the required values, etc.
That approach may simplify the looping.
Funnily enough, American English is different to British English.
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code