Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class Placeholdername {
	
           public static void main(String args[]){
		InputStreamReader keyboard = new InputStreamReader(System.in) ;
		BufferedReader bufRead = new BufferedReader(keyboard) ;

		System.out.println("Some text goes here");
		System.out.println("Which way do you want to go? North, East, West, South.");
		String userinput = bufRead.readLine();
		//NORTH ROUTE:
				if (userinput) == ("North")

so that last line is what i need help with i've tryed all the methods i know and have found. So, how do i read the user input and see if it maches North or any other word?
Posted
Updated 13-Aug-13 2:23am
v3
Comments
pasztorpisti 13-Aug-13 9:59am    
You made serious mistakes despite the fact that you are using only very basic language elements. I highly suggest reading at least the "Trails Covering the Basics" section of the official java tutorial: http://docs.oracle.com/javase/tutorial/ The "basics" paragraph isn't that long and its much easier to deal with a language after reading a basic tutorial at least once (but rather twice or more). Reading the tutorial may be a bit boring but after it you will be able to find out more creative solutions without committing a lot of silly mistakes.

I think you can try something like:
Java
if (userinput.contains("North"))
      System.out.println("It is North"); //do whatever you want
    else if (userinput.contains("East"))
      System.out.println("It is East");
    else if (userinput.contains("West"))
      System.out.println("It is West");
    else if (userinput.contains("South"))
      System.out.println("It is South");
    else
        System.out.println("None of these");
 
Share this answer
 
v4
Comments
pasztorpisti 13-Aug-13 9:56am    
5ed. Good solution. It could be even better with some explanation about identity vs equality with which OP got in trouble.
On the assumption that the user of your program is supposed to use only the provided words for routes, then you can try the following: This assumes you're using JDK 1.7 for switch with strings.
Java
switch(userinput.readLine().toUpperCase()){
  case "NORTH":
     // North Route
     break;
  case "SOUTH":
     // South Route
     break;
  case "EAST":
     // East Route
     break;
  case "WEST":
     // West Route
     break;
  default:
     // Throw Error or Show message
}

However, if you're using JDK 1.6 or below, you can implement your code in the following way:
Java
String input = userinput.readLine().toUpperCase();
if(input.equals("NORTH"))
  // North
else if(input.equals("SOUTH"))
  // South
else if(input.equals("EAST"))
  // East
else if(input.equals("WEST"))
  // West
else
  // Throw Error or Show message


However, if neither of the above solutions work for you, you can check Java's Regular Expressions API[^] or the Java's Regular Expression Tutorial[^].

In case this satisfies your requirements, you can always mark it as an accepted solution and upvote it so that others may also benefit from the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900