+ 3
Why the second array output is not coming?
19 ответов
+ 3
if( c == firstname[i]){
If you compare two Strings don't use ==.
You have to use equals ()
if(c.equals (firstname [i])
https://www.sololearn.com/post/171078/?ref=app
+ 2
How can improve it
+ 2
Piyush Srivastava think and try for what Jnn suggested , if you dont get then specify where you struck with a reply..
+ 2
Like Denise Roßberg said, you have to use equals() method. Also, (I think) if you put break in else clausule it's going to go out of the loop if the name you search is not the first in the list
+ 2
I'm not sure this is what you wanted, but maybe you can try like this:
P.s. Denise Roßberg or voja or anybody else if you have any suggestions....
https://code.sololearn.com/c1hu30U1ds7I/?ref=app
+ 2
Atila Sabo i think he wants search for the first name and if match, then display corresponding last name.
But he didn't give a reply.. So i let him try first...
+ 2
Yes exactly jaya
+ 2
Ok
+ 2
Maybe For each loop is better for arrays
+ 1
4) you need to put the if else clause out of the for loop body cause it will be executed in every iteration
+ 1
Sry i dont have time to correct your code at the moment
+ 1
Piyush Srivastava you have to specify what you got? What not you getting? What you not understood?
All solution are available here above for your problem, have read and tried it? Did you understand or not those?
Now again:
1).Use equals method always to compare strings .
'==' compares references identification while equals method compares the content.
2) remove break in else
edit:
Piyush Srivastava
because you asked for code improvements, i posting this code. look at this code , you will get some idea..
https://code.sololearn.com/cdH8110C2xgD/?ref=app
+ 1
You can only compare primitive datatypes like int, float or double with "==". If you have objects from a class like "String" (an object is often created with the "new" operator, but every "String" is an object) you are comparing the resources of theses object with "==". Every object has an address in the stack with his attributes but in the object name is only the references to this part in the stack saved. So its possible to have two String objects with the same sequences of chars, but on different addresses in the stack. Thats why you need to use equals() which compares the chars in the string on equality.
+ 1
Well I m not sure what are you trying to achieve the output but the problem is first remove the space from array of firstname in "ram" & "robert" and secondly use else if condition below if condition so you can execute the block if the search reslut not found.
Here is Corrected code
https://code.sololearn.com/ccrG9Mu7R5JU/?ref=app
0
1) by declaring and using int count in the body of the for loop it will have the value -1 and after that 0 (because of ++) in every iteration
0
2) using true in the condition of an if clause will cause that it will always be executed and everything in else will never be accessed
0
3) i think you wanted to use the Boolean contain in the condition of if (would make more sense) .
BUT: to use this boolean outside of the if clause you have to declare it befor that. When using {} (if or for for example) you open a new scope and every variable you define in there cant be accessed out of this scope
0
Your for loop is the problem. The way your code is written you will only get the last name of the first person on the list.
Remove the else and break statements. System.out.println "name not found" needs to come after the loop in an if statement. It should to check if the search found anything.