Reputation: 9
Java is really confusing me with its brackets and variables and I know with just a little help I can understand what the problem is and get on with it.
if(value1=||
Is where the error is.
Apologize in advance that indenting 8 spaces appears to have not had this pull in correctly either. help with that also appreciated...
import java.util.Scanner;
public class WTF {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
System.out.println("Please enter your Character");
String value1 =input.toString();
if (value1 ="a"||"A"||"e"||"E"||"i"||"I"||"o"||"O"||"u"||"U") {
System.out.print("You entered " +value1);
}
else System.out.print("Consonant");
}
}
Upvotes: 0
Views: 160
Reputation: 136
For Strings you should use the .equals("Your Text here") method. should look like this:
if(value1.equals("a")||value1.equals("A")||value1.equals("e"))
and so on.
Upvotes: 0
Reputation: 1
Boolean operator is used to check the value of two boolean variables and returns a boolean value here you are using the operator to compare two strings which is not compatible. as said earlier you used input.toString method to get the instance of the scanner class but it's not compatilbe instead of that you can use input.Read() or input.ReadLine().
Upvotes: 0
Reputation: 873
Use below code
public static void main(String[] args) {
List<String> vowelsList = Arrays.asList("a","A","e","E","i","I","o","O","u","U" );
Scanner input =new Scanner(System.in);
System.out.println("Please enter your Character");
String value1 = input.next();
if (vowelsList.contains(value1)) {
System.out.print("You entered " +value1);
}
else System.out.print("Consonant");
}
Upvotes: 1
Reputation: 6846
||
Logical OR Operator only used to combines two boolean variables or expressions and returns a result that is true if either or both of its operands are true.
here what you trying to do is using ||
between two character types. so It will raise an compile time error actually.
Right Way to check is ==>
Scanner reader = new Scanner(System.in);
char c = reader.nextChar(); //Correct way to Read Char from Console.
private boolean checkVowel(char c) {
switch(c) {
case 'A':
case 'a'
case 'E':
case 'e'
case 'I':
case 'i'
case 'O':
case 'o':
case 'u':
case 'U':
return true;
default:
return false;
}
}
Upvotes: 1
Reputation: 325
String value1 =input.toString();
it means use get the info about the input -- a instance of Scanner Class ,the target of it is not what you input.
you may use
String value1 = input.next();
or
String value1 = input.nextLine();
Upvotes: 0
Reputation: 2790
You will have to check for every string seperately:
if (value1=="a"||value1=="A"||value1=="e"||value1=="E"||value1=="i"||value1=="I"||value1=="o"||value1=="O"||value1=="u"||value1=="U")
but this will most likely not work since ==
checks for identity on objects and not equality and String
is an object. So you would have to write it like this:
if (value1.equals("a") || value1.equals("A") || value1.equals("e") || value1.equals("E") || value1.equals("i") || value1.equals("I") || value1.equals("o") || value1.equals("O") || value1.equals("u") || value1.equals("U")
This is just getting painfull so you could reduce this by setting value1
to upercase and just check for upercase values:
String value1_tmp = value1.toUperCase();
if (value1_tmp.equals("A") || value1_tmp.equals("E") || value1_tmp.equals("I") || value1_tmp.equals("O") || || value1_tmp.equals("U")
This is still kinda ugly so we can improve it by putting it in a method and using a switch
statement
private static boolean isVowel(String s) {
switch(s.toUperCase()) {
case "A":
case "E":
case "I":
case "O":
case "U":
return true;
default:
return false;
}
}
and then use this method in your if
check:
if (isVowel(value1)) { ...
Or if you are a fan of regular expressions simply do:
if (value1.matches("(?i)[aeiou]")) { ...
So many options, just choose one :)
EDIT: Also fix what Djehenghizz mentioned. Instead of
String value1 = input.toString();
do
String value1 = input.nextLine();
Upvotes: 3
Reputation: 63
Also, change String value1=input.toString();
into String value1=input.nextLine();
because input is already string, you dont have to transform it.
Upvotes: 1
Reputation: 11805
Try
if (value1=="a"||value1=="A"||value1=="e"||value1=="E"||value1=="i"||value1=="I"||value1=="o"||value1=="O"||value1=="u"||value1=="U") //...
Or
if ("aeiouAEIOU".contains(value1))//...
Upvotes: 0