+ 3

Why the second array output is not coming?

https://code.sololearn.com/c1jskkm1cyd6/?ref=app

27th Nov 2019, 11:03 AM
Piyush Srivastava
Piyush Srivastava - avatar
19 Respuestas
+ 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
27th Nov 2019, 6:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
How can improve it
27th Nov 2019, 11:31 AM
Piyush Srivastava
Piyush Srivastava - avatar
+ 2
Piyush Srivastava think and try for what Jnn suggested , if you dont get then specify where you struck with a reply..
27th Nov 2019, 12:48 PM
Jayakrishna 🇮🇳
+ 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
27th Nov 2019, 10:16 PM
Atila Sabo
Atila Sabo - avatar
+ 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
27th Nov 2019, 10:27 PM
Atila Sabo
Atila Sabo - avatar
+ 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...
28th Nov 2019, 9:06 AM
Jayakrishna 🇮🇳
+ 2
Yes exactly jaya
28th Nov 2019, 10:07 AM
Piyush Srivastava
Piyush Srivastava - avatar
+ 2
Ok
28th Nov 2019, 10:51 AM
Piyush Srivastava
Piyush Srivastava - avatar
+ 2
Maybe For each loop is better for arrays
29th Nov 2019, 3:12 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
29th Nov 2019, 8:08 AM
Programmer Raja
Programmer Raja - avatar
+ 1
4) you need to put the if else clause out of the for loop body cause it will be executed in every iteration
27th Nov 2019, 11:34 AM
Jnn
Jnn - avatar
+ 1
Sry i dont have time to correct your code at the moment
27th Nov 2019, 11:42 AM
Jnn
Jnn - avatar
+ 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
28th Nov 2019, 10:35 AM
Jayakrishna 🇮🇳
+ 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.
28th Nov 2019, 11:16 AM
Jnn
Jnn - avatar
+ 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
29th Nov 2019, 6:55 AM
Naved
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
27th Nov 2019, 11:26 AM
Jnn
Jnn - avatar
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
27th Nov 2019, 11:28 AM
Jnn
Jnn - avatar
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
27th Nov 2019, 11:32 AM
Jnn
Jnn - avatar
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.
29th Nov 2019, 5:29 AM
Bernie Booth Jr
Bernie Booth Jr - avatar