Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: My string is not activating my if statment.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default My string is not activating my if statment.

    I have recently started to learn java and wanted skills to test my skills so far. I started making a small text-based game and have hit an early roadblock.

    I have two classes so far, one for the menu system and the core processes, another for the character's stats and information.
    While testing the two classes interacting, I was able to successfully print the stats, call it in an if by setting two variables equal (operation and stats).

    The problem came when I wanted to use scanner to allow the user to type "stats" and show the stats. I have tried both if and while with the variables equal, so I know that the problem has something to do with scanner.

    Here are the classes.

    GameD01.java

    import java.util.Scanner;
     
    public class GameD01 {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		characterStats charStats = new characterStats();
     
    		String operation, stats = null;
    		System.out.println("What would you like to see?");
    		operation = input.nextLine();
    		if(stats){
    			System.out.println("Your stats are: ");
    			System.out.println("HP: "+ charStats.health);
    			System.out.println("MP: "+ charStats.magic);
    			System.out.println("Str: "+ charStats.strength);
    			System.out.println("Def: "+ charStats.defense);
    			operation = null;
    		}else{
    			System.out.println("Please work...");
    		}
    	}
    }

    characterStats.java
     
    public class characterStats {
    	public int health, magic, strength, defense; {
    		health = 100;
    		magic = 20;
    		strength = 50;
    		defense = 50;
    	}
    }
    Last edited by ZeroLRS; July 8th, 2011 at 06:22 PM. Reason: Removed code that is no longer there.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: My string is not activating my if statment.

    Try debugging your code by adding a println before the if statement to show the values of the Strings being compared. Something like this:
    System.out.println("op=" +operation + "< stats=" + stats + "<");

    The output should help you see what is the problem.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My string is not activating my if statment.

    It prints:

    op=stats< stats=null<

    which tells me that the if should go off, but it still doesn't.
    NOTE: In the code of the original post, I changed the choice if to a regular if, the choice was a suggestion from another site.

    More NOTE: If at all possible, instead of more strings, I would like to just have user's input tested against the operation.
    Last edited by ZeroLRS; July 8th, 2011 at 06:30 PM. Reason: Added More NOTE:

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: My string is not activating my if statment.

    if should go off, but it still doesn't.
    By "going off" I assume you mean that the condition tested should be true.

    The original code tested the contents of operation("stat")
    to the contents of stat(null);
    Those two contents are different, so the if condition will be false.

    Does the new version of the code compile?
    String operation, stats = null;
    ...
    if(stats){

    The variable: stats is not a boolean.

    I would like to just have user's input tested against the operation.
    Not sure what that means. The variable: operation is the user's input.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My string is not activating my if statment.

    I'm an idiot when it comes to editing my own code....
    In eclipse I changed the choice if to:
    if(operation == stats){

    but in my post, I changed it to:
    if(stats){

    How do you suppose I make it so that if the user types "stats" into the prompt, it will display them.

    I'm about to try setting stats = "stats" to see if that works.
    Will edit post if it does, assume that it wont if you see this beforehand.

    Nope, as expected, it didn't.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: My string is not activating my if statment.

    if(operation == stats){
    Use the equals() method to compare Strings. The == operator is for primitives.

    You could use a String literal in your if statement:
    if(operation.equals("stats"){

  7. The Following User Says Thank You to Norm For This Useful Post:

    ZeroLRS (July 8th, 2011)

  8. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My string is not activating my if statment.

    Thank you so much!
    In the videos I'm watching, I haven't gotten to equals yet, this worked perfectly.

    When I finish this in however many months it will take me, I'm gonna put you in the credits :)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: My string is not activating my if statment.

    Wow. I'll be famous.
    Keep plugging along. It'll get easier and make more sense as you get more experience and knowledge.

  10. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My string is not activating my if statment.

    Looking at my code now, I've already started most of the battle system's core.

    I'm amazed that I wrote all of this stuff. Even more so that I started java yesterday.

Similar Threads

  1. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  2. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  3. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 06:11 PM
  4. Replies: 0
    Last Post: February 23rd, 2010, 02:12 PM
  5. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM