Hi, the program works fine, no errors, however, the output is wrong and I can't figure out why exactly.
However, I have noticed two strange things when testing.
1 - If I input b, c, a and other longer strings, the results will display "a" as being the first shortest, even though it is the third.
2 - When I use the suggested input, grandfather, grandmother etc.. the results will display "grandmother" as being the first longest, even though it is the second.
What am I missing?
Thank you!
Andrei.
public class Solution {
private static String shortestString = null;
private static String longestString = null;
private static int indexOfShortest = 0;
private static int indexOfLongest = 0;
public static void main(String[] args) throws Exception {
//write your code here
ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int a = 0; a < 10; a++) list.add(reader.readLine());
reader.close();
int shortest = list.get(0).length();
for (int a = 0; a < 10; a++){
if (shortest > list.get(a).length()) shortest = list.get(a).length();
}
// System.out.println(shortest); ok till here
for (int a = 0; a < 10; a++){
if (shortest == list.get(a).length()) {
shortestString = list.get(a);
}
indexOfShortest = list.indexOf(shortestString);
}
/*
System.out.println(shortestString);
System.out.println(indexOfShortest); // ok until here but if there are more strings that are the same size
// ex. b,c,a, the shortest string will be considered a, why is that?
*/
int longest = list.get(0).length();
for (int a = 0; a < 10; a++){
if (longest < list.get(a).length()) longest = list.get(a).length();
}
for (int a = 0; a < 10; a++){
if (longest == list.get(a).length()) {
longestString = list.get(a);
}
indexOfLongest = list.indexOf(longestString);
}
if (indexOfShortest < indexOfLongest) {
System.out.println(list.get(indexOfShortest));
}
else if (indexOfShortest > indexOfLongest){
System.out.println(list.get(indexOfLongest));
}
else if (indexOfLongest == indexOfShortest) System.out.println("no bueno");
}
}
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 {
private static String shortestString = null;
private static String longestString = null;
private static int indexOfShortest = 0;
private static int indexOfLongest = 0;
public static void main(String[] args) throws Exception {
//write your code here
ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int a = 0; a < 10; a++) list.add(reader.readLine());
int shortest = list.get(0).length();
for (int a = 0; a < 10; a++){
if (shortest > list.get(a).length()) shortest = list.get(a).length();
}
// System.out.println(shortest); e ok pana aici
for (int a = 0; a < 10; a++){
if (shortest == list.get(a).length()) {
shortestString = list.get(a);
}
indexOfShortest = list.indexOf(shortestString);
}
/*
System.out.println(shortestString);
System.out.println(indexOfShortest); // e ok pana aici doar ca daca sunt mai multe string-uri de aceeasi dimensiune
// ex , b ,c, a, il va considera de a cel mai mic
*/
int longest = list.get(0).length();
for (int a = 0; a < 10; a++){
if (longest < list.get(a).length()) longest = list.get(a).length();
}
for (int a = 0; a < 10; a++){
if (longest == list.get(a).length()) {
longestString = list.get(a);
}
indexOfLongest = list.indexOf(longestString);
}
/*
System.out.println(longestString);
System.out.println(indexOfLongest);
*/
if (indexOfShortest < indexOfLongest) {
System.out.println(list.get(indexOfShortest));
}
else if (indexOfShortest > indexOfLongest){
System.out.println(list.get(indexOfLongest));
}
/*else if (indexOfLongest == indexOfShortest) System.out.println("suge-o");*/
}
}