CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Tiko hakobyan
Level 20
Yerevan
  • 10.05.2020
  • 332views
  • 8comments

any idea why this code does not pass the verification ?

Question about the task Shortest or longest
Java Syntax,  Level 7,  Lesson 6
Under discussion

1. Create a list of strings.
2. Add 10 strings from the keyboard.
3. Find out which string occurs earlier in the list: the shortest or the longest.
If several strings are shortest or longest, then consider the very first such string encountered.
4. Display the string described in Step 3. One string should be displayed.

Requirements:
  • Declare a string list variable and immediately initialize it.
  • The program should read 10 strings from the keyboard and add them to the list.
  • The program should display the shortest string if it comes before the longest.
  • The program should display the longest string if it comes before the shortest.
package com.codegym.task.task07.task0712; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Scanner; /* Shortest or longest */ public class Solution { public static void main(String[] args) throws Exception { //write your code here Scanner scanner = new Scanner(System.in); ArrayList<String> list = new ArrayList<>(); int shortest = 0; int longest = 0; //using for lood to read 10 strings from keybord and asighning them to ArrayList for (int i = 0; i < 10; i++) { list.add(i,scanner.nextLine()); //assigning to shortest and longest value of first input string shortest =list.get(0).length(); longest =list.get(0).length(); //with each new input checking if its shortar than shortest and longer than longeest //if yes assigning new value to shortest or longest if (shortest>list.get(i).length()){ shortest = list.get(i).length(); }else if(longest<list.get(i).length()){ longest = list.get(i).length(); } } // checking weather valuse of shortest or longest was input first and displaying the string for (int i = 0; i <list.size() ; i++) { if (shortest == list.get(i).length()){ System.out.println(list.get(i)); break; }else if (longest == list.get(i).length()){ System.out.println(list.get(i)); break; } } } }
0
Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Hendra
Level 8 , Medan, Indonesia
14 May 2020, 12:36
so complex ... 1.using for loop for input 10 string... 2. using for loop to check the length of string .,find the shortest value and the longest value. 3.using for loop to find the index of the shortest 4.using for loop to find the index of the longest 5.then compare the index ,which index is small 6.then display the result
0
Thomas
Level 31 , Bayreuth, Germany
14 May 2020, 13:15useful
I consider the code to be not bad at all. Instead of having thousands of loops you do what is possible in one. Despite the little mistake in lines 26,27 (that needs just an if clause or set outside the loop with the Integer constans Integer.MAX_VALUE and Integer.MIN_VALUE) this is a solid attempt to solve the task. Saving max and min in the first loop and checking which one is first in the second is advanced coding. Especially that the second loop usually doesn't need to finish ;) When tidied up a little bit, the code will look clean and simple.
public static void main(String[] args) throws Exception {

    ArrayList<String> list = new ArrayList<>();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int maxLength = 0;
    int minLength = Integer.MAX_VALUE;

    for (int i = 0; i < 10; i++) {
        list.add(br.readLine());
        if (list.get(i).length() > maxLength) {
            maxLength = list.get(i).length();
        } else if (list.get(i).length() < minLength) {
            minLength = list.get(i).length();
        }
    }

    for (int i = 0; i < list.size(); i++) {
        if(list.get(i).length() == maxLength || list.get(i).length() == minLength) {
            System.out.println(list.get(i));
            break;
        }
    }
}
+2
Tiko hakobyan
Level 20 , Yerevan, Armenia
14 May 2020, 14:45
thanks for your reply . below is shown how i ended up solving the issue. now, after having read your comment and gained some more experience , i'll make sure to use a shorter and more simple code next time. for (int i = 0; i < 10; i++) { list.add(i,scanner.nextLine()); } int shortest =list.get(0).length(); int longest =list.get(0).length(); int index = 0; int index1 = 0; for (int i = 0; i < list.size() ; i++) { if(shortest>list.get(i).length()) { index = i; shortest = list.get(i).length(); } for (int j = 0; j <list.size() ; j++) { if(longest<list.get(i).length()){ index1 = j; longest = list.get(j).length(); } }
0
Gellert Varga
Level 23 , Szekesfehervar, Hungary
10 May 2020, 16:23
Some suggestions. 1) A complex task should be broken down into several smaller simpler ones. So, try to seperate the problems to be solved. This way: - one cycle: read the data. - second cycle: finding the smallest element. - third cycle: finding the largest element. - fourth task: a comparision, the largest or the smallest is the first? - fifth task: display this element. This way things are surely not mixed up!! 2) Check/test your program often. With different input data. Then you will see when what happens in the program.
0
Tiko hakobyan
Level 20 , Yerevan, Armenia
10 May 2020, 18:42
1) all 5 steps you mentioned are included in just 2 loops. the less your code the better . 2) checked dozen of times before posting in here have you looked through the code ? which part is not correct ? or which which condition(s) is not meet ?
0
Gellert Varga
Level 23 , Szekesfehervar, Hungary
10 May 2020, 20:09useful
1) "the less your code the better ." - but only if it works! If not, then you have to break down the program into separate steps. For purpose: to be able to see what was mixed up. First: Primary to make the program work. Second: if it works well, after it you can compress it. ((But it is just my logic.)) 2) Currently, your program outputs the very first string. Each time. No matter it is longer or shorter. 3) Analyzing Line 26 and Line 27: Maybe the second string was the shortest, but you set back the variable 'shortest' to the longer first element here (get(0).length). Every time. So, no matter which string is the shortest. So, you always compare all elements only to this first element, in every circle. 4) Line 37....Line43: I don't understand this logic exactly. Maybe this is confused. Maybe not. I don't know. But i would not use this. Instead of this, i would do this: When you are searching the shortest and longest, put the index of the found elements into a variable! In other words: Save the index of the found shortest item! shortest= list.get(i).length(); shortestindex = i; And after the searcher cycle, you just print the get(shortestindex).
+1
Tiko hakobyan
Level 20 , Yerevan, Armenia
11 May 2020, 13:56
thank you for your time and help. it was very useful for me
0
Gellert Varga
Level 23 , Szekesfehervar, Hungary
11 May 2020, 18:35
You're welcome!
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.