Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Help with strings and if statements.  (Read 2717 times)
0 Members and 1 Guest are viewing this topic.
Offline Thsotus

Senior Newbie





« Posted 2010-10-05 12:02:12 »

How can I check if "y" is true?

My current code is:  if (yOrN == null ? "y" == null : yOrN.equals("y"))

How can I check if y is true?


___________________________________________________


Check out Thsotus Games http://thsotusgames.wordpress.com/.

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline divxdede

Junior Member





« Reply #1 - Posted 2010-10-05 12:05:52 »

1  
2  
3  
if( "y".equalsIgnoreCase(yOrN) ) {
  // todo
}
Offline dbotha

Senior Newbie





« Reply #2 - Posted 2010-10-05 12:11:57 »

I assume you are trying to check if the String yOrN equals "y"? If this is the case you want

1  
if (yOrN.equals("y")) {...}


which is a shorter way of stating

1  
if (yOrN.equals("y") == true) {...}


What you are currently doing does not make a lot as sense as "y" == null will always evaluate to false.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Thsotus

Senior Newbie





« Reply #3 - Posted 2010-10-05 12:18:12 »

My code still doesn't work, even with the == true statement.

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline dbotha

Senior Newbie





« Reply #4 - Posted 2010-10-05 12:24:54 »

Could you post some of the code in context to give us a better idea of where you may be going wrong?
Offline Thsotus

Senior Newbie





« Reply #5 - Posted 2010-10-05 12:27:05 »

How about the whole code?


package twindays;

import java.util.Scanner;

public class Main {
    private static int yesOrNoInt = -1;
    public static void main(String[] args) {
        System.out.println("You have reached a yes or no choice.");

        yesOrNo(yesOrNoInt);
        System.out.println(yesOrNoInt);
        yesOrNoMessage(yesOrNoInt);

       
           

    }

    public static int yesOrNo(int yesOrNoInt){

        System.out.println("Please input (y)es or (n)o.");
        Scanner scan = new Scanner(System.in);
        String yOrN = scan.nextLine();
        if (yOrN.equals("y")) {
            yesOrNoInt = 1;
        }
        if (yOrN.equals("n")) {
            yesOrNoInt = 0;
        }
        return yesOrNoInt;
    }
    public static void yesOrNoMessage(int yesOrNoInt)  {
                    if (yesOrNoInt == 1) {
                    System.out.println("You have chosen yes.");
                    }
                    if (yesOrNoInt == 0) {
                    System.out.println("You have chosen no.");
                    }
        }
}


The output for the program when I put "y" in is this:

You have reached a yes or no choice.
Please input (y)es or (n)o.
y
-1

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2010-10-05 12:29:46 »

        yesOrNo(yesOrNoInt);
        yesOrNoMessage(yesOrNoInt);
This does not do what you think it does. The value yesOrNoInt (horrible name by the way) is copied in to each of those methods, but not changed.  You want something like:

int yesOrNoInt = yesOrNo(); // Get the input from the user
yesOrNoMessage(yesOrNoInt); // Display the response

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Thsotus

Senior Newbie





« Reply #7 - Posted 2010-10-05 12:33:03 »

So, I'll just change the variable to the name "yORn."


Thank you, but can I ask some more?

How does the method yesOrNo import the variable yesOrNoInt without having it in the parenthesis?
Wait, so Java performs the two methods at the same time?

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline dbotha

Senior Newbie





« Reply #8 - Posted 2010-10-05 12:41:18 »

The variable is available to the method as it is a class member hence visible to all methods within the class. Notice how you declared the variable outside the method at class scope. Java does not run the two methods at the same time; rather your first method is run which sets the yesOrNoInt variable (which is visible to both methods) followed by the second method which accesses this variable. The same variable is shared between the methods.
Offline Thsotus

Senior Newbie





« Reply #9 - Posted 2010-10-05 12:46:32 »

Ah, I see.

By the way, this will be used for a text-based game I call "Twin Days."  It will be a simple text based adventure.

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #10 - Posted 2010-10-05 13:57:58 »

There are too much beginners faults in your code, so go on reading one of the tutorials linked in http://www.java-gaming.org/topics/hello-game-developers/23071/msg/191130/view.html#msg191130.

You have to at least understand the basics of variable handling like the scope they are valid in, differences between objects and primitives, knowing when a value is copied and when (and why) it is only referenced. Also you need an understanding of what methods are, how they execute and what return really does.

This isn't to be mean, it's just that you probably won't get anywhere with your current knowledge.

How can I check if "y" is true?

Take the advice from divxdede, it has two advantages: first it avoids NullPointerExceptions and second it ignores case (which you usually want in a text adventure interface).

How does the method yesOrNo import the variable yesOrNoInt without having it in the parenthesis?

Like dbotha explained this is because you declared it as a static member variable of your main class. While it makes your code work, it is a horrible solution and will lead to a lot of holes in your feet...  Shocked Wink As a rule of thumb: whereever possible use local variables and don't declare static variables at all unless you _REALLY_ are sure there is no other way to achieve your goal. (Even static methods should be avoided, but thats another story)

Other than that, your whole code is more complex than it needs to be:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
package twindays;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("You have reached a yes or no choice.");
        boolean yesChoosen= "y".equalsIgnoreCase(scan.nextLine());
        System.out.print("You have chosen ") ;
        System.out.println(yesChoosen? "yes.": "no.");
    }
}


You probably wanted to modulize input and output here, but it just made simple things complicated. Beneficial modularization in java would incorporate some more object oriented design (like encapsulating the Scanner in an own class with dedicated methods for the type of questions you want to ask etc.)

Again, you are not quite there now, do more tutorials!


Mathias - I Know What [you] Did Last Summer!
Offline Thsotus

Senior Newbie





« Reply #11 - Posted 2010-10-05 18:38:54 »

Thanks for taking the time to address me.

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #12 - Posted 2010-10-05 20:18:47 »

I just want to point out that (in my opinion), it is bad practice to say if ("a static string".equals(aVariable)). Instead, you should reverse the order: if (aVariable.equals("a static string")).

This is because, logically speaking, you are checking to see if your variable equals something. You aren't checking to see if something equals your variable. Even those are mutually exclusive and both will "work" just fine, you want to make your code as clear and as readable as possible. That in itself is an important skill to work on.

See my work:
OTC Software
Offline lhkbob

JGO Knight


Medals: 32



« Reply #13 - Posted 2010-10-05 20:30:27 »

I just want to point out that (in my opinion), it is bad practice to say if ("a static string".equals(aVariable)). Instead, you should reverse the order: if (aVariable.equals("a static string")).

A benefit of doing "static string".equals(var) is that var can be null and you don't need to worry about null pointer exceptions because equals() will handle it properly. On the other hand, I do think it looks funny.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #14 - Posted 2010-10-05 20:46:32 »

I actually always use "literal".equals(var), besides avoiding NPE's I think it makes code a little easier to peruse when you see a string of if/else conditions checking the value of a String variable.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
if ("one".equals(var)) {
  // ...
}
else if ("two".equals(var)) {
 // ...
}
else if ("three".equals(var)) {
 // ...
}
else if ("four".equals(var)) {
 // ...
}
else ...


That's personal preference, of course.
Offline Thsotus

Senior Newbie





« Reply #15 - Posted 2010-10-06 00:37:24 »

Please expand on this.  I don't know how to use the command you are trying to show me.

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 747
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2010-10-06 00:49:14 »

There are too much beginners faults in your code, so go on reading one of the tutorials linked in http://www.java-gaming.org/topics/hello-game-developers/23071/msg/191130/view.html#msg191130.

You have to at least understand the basics of variable handling like the scope they are valid in, differences between objects and primitives, knowing when a value is copied and when (and why) it is only referenced. Also you need an understanding of what methods are, how they execute and what return really does.

This isn't to be mean, it's just that you probably won't get anywhere with your current knowledge.

...

Again, you are not quite there now, do more tutorials!

This! And buy a book, it's often a better way to learn it than browsing through online tutorials.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Draps

Senior Newbie





« Reply #17 - Posted 2010-10-06 03:53:20 »

How about the whole code?


package twindays;

import java.util.Scanner;

public class Main {
    private static int yesOrNoInt = -1;
    public static void main(String[] args) {
        System.out.println("You have reached a yes or no choice.");

        yesOrNo(yesOrNoInt);
        System.out.println(yesOrNoInt);
        yesOrNoMessage(yesOrNoInt);

       
           

    }

    public static int yesOrNo(int yesOrNoInt){

        System.out.println("Please input (y)es or (n)o.");
        Scanner scan = new Scanner(System.in);
        String yOrN = scan.nextLine();
        if (yOrN.equals("y")) {
            yesOrNoInt = 1;
        }
        if (yOrN.equals("n")) {
            yesOrNoInt = 0;
        }
        return yesOrNoInt;
    }
    public static void yesOrNoMessage(int yesOrNoInt)  {
                    if (yesOrNoInt == 1) {
                    System.out.println("You have chosen yes.");
                    }
                    if (yesOrNoInt == 0) {
                    System.out.println("You have chosen no.");
                    }
        }
}


The output for the program when I put "y" in is this:

You have reached a yes or no choice.
Please input (y)es or (n)o.
y
-1


OH DEAR GOD MOTHER OF MERCY
Offline Gudradain
« Reply #18 - Posted 2010-10-06 04:34:50 »

I actually always use "literal".equals(var), besides avoiding NPE's I think it makes code a little easier to peruse when you see a string of if/else conditions checking the value of a String variable.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
if ("one".equals(var)) {
  // ...
}
else if ("two".equals(var)) {
 // ...
}
else if ("three".equals(var)) {
 // ...
}
else if ("four".equals(var)) {
 // ...
}
else ...


That's personal preference, of course.


Lol never thought about it. But I guess it could save me some (if var != null)
Offline cylab

JGO Ninja


Medals: 38



« Reply #19 - Posted 2010-10-06 11:50:40 »


OH DEAR GOD MOTHER OF MERCY

I think this has to be

Quote
OH DEAR f**kING GOD MOTHER OF f**kING MERCY  Roll Eyes

or maybe

Quote
OH DEAR GOD f**kING MOTHER OF f**kING MERCY  Shocked

or maybe

Quote




Mathias - I Know What [you] Did Last Summer!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #20 - Posted 2010-10-06 18:53:53 »

A benefit of doing "static string".equals(var) is that var can be null and you don't need to worry about null pointer exceptions because equals() will handle it properly. On the other hand, I do think it looks funny.
This is true, but I would argue that you always want to explicitly check for null so that it's clear that you have handled that situation. Once again, it's about readability.

1  
2  
3  
4  
5  
6  
7  
8  
if (str == null)
{
    throw new Exception("What the crap? str is null! Deal with it!");
}
else if (str.equals("foo"))
{
    //etc.
}


Once again, improved readability (in my opinion, anyway).

But this is off topic, and probably over Thsotus's head. @Thsotus: take a Java class. Smiley

See my work:
OTC Software
Offline Thsotus

Senior Newbie





« Reply #21 - Posted 2010-10-07 05:16:50 »

There is no Java class at my school.  Er...

Hey!  Visit the blog (and current home website) for my studios,
Thsotus Games, http://thsotusgames.wordpress.com/.

My current and most recent project is called "Twin Days."
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #22 - Posted 2010-10-07 16:43:28 »

There is no Java class at my school.  Er...
You can always go for the virtual option.

Google

See my work:
OTC Software
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.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!