Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

I need help with a very simple problem


  • Please log in to reply
4 replies to this topic

#1 idrivebears

idrivebears

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 02 July 2011 - 07:54 PM

Hello, im new to java programming and i ran into what seems to be(and most likely is) a very simple problem. Im testing a string variable with an if statement and its not working how i want it to be:


import java.util.Scanner;
import java.util.Random;

public class Program {
	public static void main(String[] args){
		Scanner keyboard = new Scanner(System.in);  //scanner variable to get user input
		
		String option;
		
		System.out.println("Welcome!");
		System.out.print("Do you want to play the game?");
		option = keyboard.nextLine();  //storing input.
		
		
		if(option == "Yes"||option=="yes"){
			System.out.println("Ok then!!! Lets play!");
		}
		else{
			System.out.println("\n");
			System.out.println("Alright! bye!");
			
		}
	}
}


when i run this, i input "yes" into the command line and it goes straight to the "else" statement instead of running the code in the if statement. Can anyone help me and tell me what im doing wrong? Thanks a lot in advance! :)
  • 0

#2 Xdawn90

Xdawn90

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts
  • Learning:C, Java, C++, PHP, JavaScript, Visual Basic .NET

Posted 02 July 2011 - 09:37 PM

Use the equal() method instead of "==" operator. "==" operator compares object references and not the value of the string variable.

import java.util.Scanner;
import java.util.Random;

public class Program {
    public static void main(String[] args){
        Scanner keyboard = new Scanner(System.in);  //scanner variable to get user input
        
        String option;
        
        System.out.println("Welcome!");
        System.out.print("Do you want to play the game?");
        option = keyboard.nextLine();  //storing input.
        
        if(option.equals("Yes") || option.equals("yes")){
            System.out.println("Ok then!!! Lets play!");
        }
        else{
            System.out.println("\n");
            System.out.println("Alright! bye!");
            
        }
    }
}
Java String comparison. The difference between == and equals().
  • 0

#3 idrivebears

idrivebears

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 03 July 2011 - 09:38 AM

Thank you so much!
  • 0

#4 johannes

johannes

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 05 July 2011 - 12:54 AM

if(option.equals("Yes") || option.equals("yes")){


If possible you should try to use equals operator on the constants, because an object like 'option' can be null. Also, if your intention with checking both Yes and yes is to not care about the case, you can use the equalsIgnoreCase() method

if("Yes".equalsIgnoreCase(option)){

  • 0

#5 idrivebears

idrivebears

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 23 July 2011 - 09:24 AM

Thats brilliant, thanks a lot!
  • 0