Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. Yeah you can do it but you need to write a bit complex logic for that. You can use String class's substring() method to get last names and then you can make a new array of last names and then you can sort that array writing logic
Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. Yeah you can do it but you need to write a bit complex logic for that. You can use String class's substring() method to get last names and then you can make a new array of last names and then you can sort that array writing logic
That is quite mistaken, I am afraid. You do not need a Map, and the logic for sorting is quite simple.Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. . . .
Campbell Ritchie wrote:
That is quite mistaken, I am afraid. You do not need a Map, and the logic for sorting is quite simple.Sankalp Bhagat wrote:You need to use HashMap concept for that. you can't do sorting with last names in your code. . . .
That is incorrect. You can use Scanner. You only need to learn how to use nextLine, which you can find from the link I posted yesterday. You also do not have to worry about checked Exceptions.Sankalp Bhagat wrote:. . .
you need to use BufferedReader class instead of Scanner class.
That is bad design, using parallel arrays. Very non‑object‑loriented code, and prone to errors if you sort things.
Completely unnecessary local variable.
Both those variables are redundant. You can write the code without either.
That is incorrect use of the for‑each loop. You should use a different sort of loop there. A for‑each loop is not intended for altering the contents of an array, which is why you have that awkward syntax with k.
Campbell Ritchie wrote:
That is incorrect. You can use Scanner. You only need to learn how to use nextLine, which you can find from the link I posted yesterday. You also do not have to worry about checked Exceptions.Sankalp Bhagat wrote:. . .
you need to use BufferedReader class instead of Scanner class.That is bad design, using parallel arrays. Very non‑object‑loriented code, and prone to errors if you sort things.Completely unnecessary local variable.Both those variables are redundant. You can write the code without either.That is incorrect use of the for‑each loop. You should use a different sort of loop there. A for‑each loop is not intended for altering the contents of an array, which is why you have that awkward syntax with k.
You are also writing very unoptimised code because you have two loops when only one loop is needed. You also don't have correct code for finding the last name.
We would like to see you writing correct code first. If I am paying money into the bank I do not mind whether it take 10 seconds or 20 seconds to get in. I do worry whether it gets to the right account.Sankalp Bhagat wrote:. . .
Thank you Sir. I will try to correct my coding style and will try to write optimized code.