I’m trying to declare a variable that would increment everytime a condition is met since I need the number of time the condition was met for the output.
Variables:
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
-----this part is inside an if-----
String yourname = request.getParameter("yourname").toLowerCase();
String crushname = request.getParameter("crushname").toLowerCase();
yourname = yourname.replace(" ","");
crushname = crushname.replace(" ","");
String[] a_yourname = yourname.split("(?!^)");
String[] a_crushname = crushname.split("(?!^)");
Basically I’m trying to do this PHP code in Java:
if($yourname[$x] == $letters[$y]){
if($yourname[$x] == 'a'){
$y_a++;
}
if($yourname[$x] == 'b'){
$y_b++;
}
if($yourname[$x] == 'c'){
$y_c++;
}
}
This is my Java part:
int y_a=0;
int y_b=0;
for(int x=0;x<a_yourname.length;x++){
for(int y=0;y<letters.length;y++){
if(a_yourname[x] == letters[y]){
if(a_yourname[x] == "a"){
y_a++;
}
if(a_yourname[x] == "b"){
y_b++;
}
Don’t mind the missing closing tags, this will always return 0 whenever I print y_a, well I guess its because I initialize it to hold 0, but how do I make it so that the initialized value wont overwrite the incremented one?
I know this is very simple for some but I’m really a PHP guy and I really don’t know much about Java.
Your basic problem is, that you try to compare Strings using
==.In Java
==, however, just compares the object identity (when used with objects, such as Strings) and not their contents!To compare the contents of an object in Java use
equals()(for equality) orcompareTo()(for ordering). So in your case it should say:Assuming, that you really mean String compare here.
If you mean to compare
charor single characters, turn the"around"a"to'to mark it as such!