always getting an error how to fix this (( if (Choice == 'Borrow' || Choice == 'borrow') ))
Asked
Active
Viewed 831 times
-5
-
Please post your code instead of an image. – Yassin Hajaj Oct 25 '15 at 21:41
-
1`charAt` returns character. You are comparing to a string. Also, in Java, single quotes are used for chars, and double for string. Use `equals()` for comparing – sam Oct 25 '15 at 21:42
-
sorry here is the code Choice = zam.next().charAt(0); if (Choice == 'Borrow' || Choice == 'borrow'){ – Zam Oct 25 '15 at 21:42
-
You use apostrophes for characters, not strings. Plus youre trying to compare à character to a string and doing it in a wrong way – Yassin Hajaj Oct 25 '15 at 21:44
-
to fix the problem and make it recognized by java a word not just a character what should i use sorry a beginner programmer – Zam Oct 25 '15 at 21:48
3 Answers
1
In Java, Strings should be quoted like "this"
, not like 'this'
. The second syntax is used for individual characters.
Since you're using charAt(0)
, you are already getting the first character of the string, so you should be comparing to the individual characters 'b'
and 'B'
.

luiscubal
- 24,773
- 9
- 57
- 83
-
what can i use to make it the whole word not just the first character sorry a beginner at java – Zam Oct 25 '15 at 21:46
-
`zam.next()`. No need for anything after that. But note that your string comparisons won't work because you should use `.equals` instead of `==`. – Andy Turner Oct 25 '15 at 21:48
-
am.next( ) ; i get an error and i used .equals also the same problem error – Zam Oct 25 '15 at 21:53
0
luiscubal is right and if you want to compare the string instead of the first carachter, you can use Choice = zam.next();
wich returns a String
and compare it using Choice.equals("Borrow")
.
You get an error when you on Choice = zam.next()
because zam.next()
returns a String
and you probably declared Choice
as a char
.

Marcello Davi
- 433
- 4
- 16
-1
Scanner sc = new Scanner(System.in);
System.out.println("Veuillez saisir un mot :");
String str = sc.nextLine();
System.out.println("Vous avez saisi : " + str);
if (src == "Borrow"|| Choice == "borrow"){
System.out.println("You are borrow ");
}
Le mieux est d'éviter de faire des tests inutiles:
// supprime les espaces avant et apres le mot entré sur l'ui
String sansEspace = src. trim();
// remplace les majuscule par des minuscule
String formated = sansEspace.toLowerCase();
// faire qu'un seul test
if (formated == "borrow"){
System.out.println("You are borrow ");
}

Mada Aflak
- 1
- 1
-
2Two things: please write your text in english; and `formated == "borrow"` is still wrong. – Tom Oct 25 '15 at 21:55
-
use double quote " and not ' . If you declare the String in the same bloc, it works – Mada Aflak Oct 25 '15 at 21:59
-
i made choice string worked fine but i get error on if (Choice == "Borrow" || Choice == "borrow" ) – Zam Oct 25 '15 at 21:59
-
-
-
@Zam You should only accept the answer which solved your problem. This doesn't match to this answer, because it is flawed. So eiher you write or own answer with the fixes you've made to solve the question or you wait with accepting this answer after Mada updated this answer. Please mind that this page is supposed to be a Q&A page where people find answers to there problems (in programming). So if someone has a similar question like you, then he would think that the code in this answer is correct, but instead the solution is "hidden" in your comment. – Tom Oct 25 '15 at 22:20