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;
}
}
}
}
any idea why this code does not pass the verification ?
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Hendra
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
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.
+2
Tiko hakobyan
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
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
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
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
11 May 2020, 13:56
thank you for your time and help. it was very useful for me
0
Gellert Varga
11 May 2020, 18:35
You're welcome!
0