package com.codegym.task.task07.task0712;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Shortest or longest
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0;i < 10;i++){
strings.add(br.readLine());
}
int small = strings.get(0).length(), large = strings.get(0).length();
String x = "d"; int y = 0;
String xx = "d"; int z = 0;
for (int i = 0;i< 10;i++){
if (small > strings.get(i).length()){
small = strings.get(i).length();
x = strings.get(i);
y = i;
}
}
for (int i = 0;i< 10;i++){
if(large < strings.get(i).length()){
large = strings.get(i).length();
xx = strings.get(i);
z = i;
}
}
if (y < z){
System.out.println(x);
} else if (z < y) {
System.out.println(xx);
}
}
}
No passing steps 3 and four in the task,, but when I run the application,, everything is fine??
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Elika
27 July 2019, 05:32
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList <String> strings = new ArrayList <String>();
int size = 0;
int min = 0;
int max = 0;
for(int i=0; i<10; i++) {
strings.add(i, reader.readLine());
}
for(int i=0; i<10; i++) {
if(strings.get(i).length()>size) {
size = strings.get(i).length();
max = i;
}
}
int minSize = strings.get(0).length();
for(int i=0; i<10; i++) {
if(strings.get(i).length()<minSize) {
minSize = strings.get(i).length();
min = i;
}
}
if(min<=max) {
System.out.println(strings.get(min));
}
else {
System.out.println(strings.get(max));
}
}
}
0
Dmitriy Bursa
18 July 2019, 20:28
you have a situation when y = 0 or z = 0, then 'd' is printed
try:
get min and max in the 1st loop.
in 2nd loop compare each line length with them,
0