Help with strings and if statements.

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/.

if( "y".equalsIgnoreCase(yOrN) ) {
  // todo
}

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

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

which is a shorter way of stating

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.

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

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

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

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

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?

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.

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.

There are too much beginners faults in your code, so go on reading one of the tutorials linked in http://www.java-gaming.org/index.php/topic,23071.msg191130.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.

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).

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… :o :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:


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!

Thanks for taking the time to address me.

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.

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.

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.


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.

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

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

OH DEAR GOD MOTHER OF MERCY

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

I think this has to be

or maybe

or maybe