Results 1 to 2 of 2
  1. #1
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

    Default My if statement with multiple || is not doing what I expected

    I'm doing a very simple program that takes a String letter from a user and detects whether it is a vowel.

    Here is the code:
    Java Code:
    package com.sam;
    
    import java.util.Scanner;
    
    public class MainMethodPlayground {
    	
    	public static void main(String[] args) {
    		System.out.println("Now using the main method in the MainMethodPlayground class.");
    		System.out.println("Enter a letter:");
    		Scanner input = new Scanner(System.in);
    		String isVowelChar = input.nextLine();
    		if(isVowelChar == "a" || isVowelChar == "e" || isVowelChar == "i" || isVowelChar == "o" ||
    		   isVowelChar == "u" || isVowelChar == "y") {
    			System.out.println(isVowelChar + " is a vowel.");
    		}
    		else {
    			System.out.println(isVowelChar + " is not a vowel.");
    		}		
    	}
    }
    Sample output:
    Now using the main method in the MainMethodPlayground class.
    Enter a letter:
    a
    a is not a vowel.


    As you can see in the code, the a is in the if statement checking for vowels, so why isn't the output saying, "a is a vowel" instead?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: My if statement with multiple || is not doing what I expected

    Use the equals method when comparing the contents of objects like Strings.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 12
    Last Post: 10-20-2012, 06:55 PM
  2. Replies: 4
    Last Post: 04-27-2012, 09:43 AM
  3. Multiple colors in print-statement
    By schutter07 in forum New To Java
    Replies: 8
    Last Post: 11-20-2010, 11:46 AM
  4. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 04:44 AM
  5. Replies: 2
    Last Post: 07-18-2008, 01:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •