Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Please help with my code!  (Read 602 times)
0 Members and 1 Guest are viewing this topic.
Offline ItzKR15

Senior Newbie


Medals: 1



« Posted 2013-05-31 08:48:22 »

I just started programming with java and I have started to make a text-based game, Here is the code:
The problem is every time I run it and type either 'left' or 'right' it prints out "Unknown Command"
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
import javax.swing.JOptionPane;


public class netProject1 {
   public static void main(String[] args) {
      String userInput = JOptionPane.showInputDialog("What's your name?");
      System.out.println("Hello " + userInput + "!");
      System.out.println("");
      System.out.println("Let's Play!");
      String userInput1 = JOptionPane.showInputDialog("Do you go left or right?");

      String room = "";
     
        if (userInput1 == "left") {
            room = ("Kitchen");
          System.out.println("You are in the "+room);  
        } else if (userInput1 == "right") {
            room = ("Living Room");
          System.out.println("You are in the "+room);  
        } else {
            System.out.println("Unknown Command");
        }  
   }
}

Just keep coding, Just keep coding!

http://internetometer.com/image/39407.png Please Give me Internets!
Offline ReBirth
« Reply #1 - Posted 2013-05-31 08:51:20 »

Use equals not ==.

1  
if ("left".equals(userInput1)) ...

Offline Danny02
« Reply #2 - Posted 2013-05-31 09:04:29 »

btw when you want to use the console also as input you can do it like this
1  
2  
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        String line = console.readLine();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mrdeathjockey

Senior Devvie


Medals: 5
Projects: 1
Exp: 3 years


Trying to be a good programmer, step by step


« Reply #3 - Posted 2013-05-31 09:06:02 »

I agree with above, when comparing strings, be careful and do the following:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
String string = "string";
String anotherString = "STRING";

//Compare 'case-matching'
if(string.equals(anotherString)) {
  //string matches anotherString
}

//Compare 2 strings disregarding case
if(string.equalsIgnoreCase(anotherString)) {
  //string matches anotherString with a possibility of different case
}


This is a better explaination: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

Offline ItzKR15

Senior Newbie


Medals: 1



« Reply #4 - Posted 2013-05-31 09:08:18 »

Thanks!

Just keep coding, Just keep coding!

http://internetometer.com/image/39407.png Please Give me Internets!
Offline Sammidysam
« Reply #5 - Posted 2013-05-31 14:07:31 »

I suggest if you are trying to make a text-based game that you get input from the console rather than from a frame being created.  Then it would seem more natural.  Also, for your directions you should really use .equalsIgnoreCase(String anotherString).  It'll really help out your left and right, because I spelled it with a capital letter.
Offline actual

JGO Coder


Medals: 24



« Reply #6 - Posted 2013-05-31 14:49:41 »

Another option, if case doesn't matter is to simply set the input to lower case up front.

1  
String userInput1 = JOptionPane.showInputDialog("Do you go left or right?").toLower();


Now you don't have to worry about using equalsIgnoreCase() everywhere.
Offline ReBirth
« Reply #7 - Posted 2013-06-01 02:40:25 »

equalsIgnoreCase() is better since you won't lost your original String.

Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2013-06-01 07:59:13 »

equalsIgnoreCase() is better since you won't lost copy your original String.

String is immutable. Everytime you change it, you need to recreate it. That's what equalsIgnoreCase was written for.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ReBirth
« Reply #9 - Posted 2013-06-01 08:10:25 »

Hmmmmm

.....

Dang right!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!