Longest string

  • 10
  • Locked
Let's measure strings! And we'll complete a programming task at the same time: create a string list, read 5 strings from the keyboard, and add them to the list. Then use a loop to find the longest string (or longest strings, if there is more than one). A country should know its heroes: the longest strings will be displayed.
You can't complete this task, because you're not signed in.
Comments (21)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Level 8 , New Delhi, India
18 May, 12:09
Can anyone tell why test cases are failing when my program is printing both the largest strings?
Level 41
21 May, 05:56
Please post your question with the attached solution in the section Help.
Level 8 , Jacksonville, United States
23 December 2020, 03:08
So I have to wonder, am I the only one whose getting slightly annoyed they are using advanced techniques in a lot of these answers before they teach them? I clearly remember last level they had a whole problem in one that actually couldn't be solved without an array before we learned them in this level. and this particular problem uses a technique I had to google to decipher the (for each loop) as in the colon. I mean i get that its a way to solve the problem but I think its more beneficial for people to see the current possible answers rather than the future ones. (anyway just a small thought/rant.)
Guadalupe Gagnon
Level 37 , Tampa, United States
30 December 2020, 16:37
It doesn't say anywhere to use a foreach loop in this task, all it says is to use a loop. Loops were discussed in level 4. You should be glad that you personally found a solution that solves this task using a valid loop on your own. As a professional programmer, a large portion of your work day is going to be researching online how to accomplish whatever task your company needs you to solve. Think about it, they aren't going to hire you to solve problems that have already been solved. Also, tech advances to better techniques and new concepts on a weekly basis.
Aisuluu Zhumabaeva SDET at Capgemini
16 June, 15:55
I feel the same way. They are too advanced.
Level 15 , United Kingdom
5 September 2020, 20:57
I do not like how I need to use 3 for loops to achieve this. Any ideas for improvement?
Level 25 , Bexleyheath, United Kingdom
10 October 2020, 20:47
Yes, you can do it in two. One to find out the max length, another to print any strings that have that length.
for (String x : strings) {//use x.length to find the max}
for (String x : strings) {//if x.length == the maximum, print it}
Level 7 , Yuma, United States
22 February, 22:05
This is much easier than my solution, which was to create a separate integer array list and clearing the intArray list if a new max is found, then add the index of the max to it. This way the inArray changes size if you have many identical maxes
Karas Java Developer
4 September 2020, 23:47
Consider that the strings in the input can be changed, swap them arround and run your code , the result should be the same, if you compare the length and then print, what if there is a longer line, the prior already printed and would not be the longest one, think with that.
Marek Pasierbek
Level 22 , Poznań, Poland
8 July 2020, 03:51
not easy, very stupid, get more satisfaction
Qi Wang
Level 10 , Perth, Australia
26 June 2020, 07:58
String temp1=strings.get(j); strings.set(j,strings.get(j+1)); strings.set(j+1, temp1); // String temp1=strings.get(j); // strings.remove(j); // strings.add(j,strings.get(j+1)); // strings.remove(j+1); // strings.add(j+1,temp1); why the code in "//" doesnt work for swap two elements in a list ?
Level 41 , Solar System
27 June 2020, 09:54
Code after // is seen by Java as a comment and will not be used during compilation.
Level 9
12 June 2020, 12:41
A way to complete the last requirement is to create another loop like what you used to find the longest string but instead compare (==) every string length with the max length string and print them if they're the same.
12 February 2020, 06:16
You know what is funny about this one: I got it working so i only have a green pass mark just for "The program should display the longest string." the rest is red. If you remember that kid who did go to karate and says : (You need to hit me like i learn)
Level 7 , Arlington, United States
28 January 2020, 21:44
The code runs fine...but the requirements are not met...Someone plz help.
Level 41
31 January 2020, 08:10
Please refer to Help section at our course.
Param M
Level 8 , Mumbai, India
10 July 2019, 02:33
Where am i going wrong. Someone please help