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."
|
|
|
divxdede
|
 |
«
Reply #1 - Posted
2010-10-05 12:05:52 » |
|
1 2 3
| if( "y".equalsIgnoreCase(yOrN) ) { } |
|
|
|
|
|
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!
|
|
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."
|
|
|
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?
|
|
|
|
|
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."
|
|
|
Orangy Tang
|
 |
«
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
|
|
|
|
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."
|
|
|
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.
|
|
|
|
|
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!
|
|
cylab
|
 |
«
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...  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!
|
|
|
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."
|
|
|
Eli Delventhal
|
 |
«
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.
|
|
|
|
lhkbob
|
 |
«
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.
|
|
|
|
BoBear2681
|
 |
«
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.
|
|
|
|
|
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."
|
|
|
Riven
|
 |
«
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.
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
cylab
|
 |
«
Reply #19 - Posted
2010-10-06 11:50:40 » |
|
OH DEAR GOD MOTHER OF MERCY
I think this has to be OH DEAR f**kING GOD MOTHER OF f**kING MERCY  or maybe OH DEAR GOD f**kING MOTHER OF f**kING MERCY  or maybe
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Eli Delventhal
|
 |
«
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")) { } |
Once again, improved readability (in my opinion, anyway). But this is off topic, and probably over Thsotus's head. @Thsotus: take a Java class. 
|
|
|
|
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."
|
|
|
Eli Delventhal
|
 |
«
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
|
|
|
|
|