I am trying to use a for loop to examine every character in a single dimensional string array and print “Character [a] is uppercase” or “Character [b] is lowercase” or “Character [c] is a space.” I also have to print the index number and the character in that index.
I thought I was supposed to create separate strings with uppercase letters and lowercase letters but turns out I was wrong. I also don’t know if I’m supposed to convert the string array into a char array?
I think I’m supposed to use charAt() method but I don’t know how.
This is what I had done:
for(x = 0; x < fullName.length(); x++)
{
if(fullName[0] == nameUpperCase[2]);
{
System.out.print("character [0] located at position [0] is lowercase");
}
if(fullName[1] == nameLowerCase[4]);
{
System.out.print("character [b] located at position [1] is uppercase");
}
}
As you can see, I couldn’t even figure out how to use the loop to print ‘a’ or ‘b’ and so I had to manually insert each one along with the position they’re in…
If you’re comparing two Strings (ie the arrays are
String[]) then your code is comparing the memory reference of each string, which will be different.In order to compare
Stringfor equality, you need to use theequalsmethod.I would also recommend you take some of the ideas from the other answers.
Convert the original
Stringto a char array instead…Or you could simply get the char at the given index…
Updated example
Now that we’ve established that we can’t compare
Stringvalues with==, here’s a working example of what I think you want achieve…Which outputs