Hi
I get the error message that posMin and posMax might not have been initialized... What am I doing wrong?
Thank you for your help!
package de.codegym.task.task07.task0712;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
/*
Kürzeste oder längste
*/
public class Solution {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < 10; i++){
list.add(i, s.nextLine());
}
int min = list.get(0).length();
for(int i = 0; i < list.size(); i++){
if(list.get(i).length() <= min){
min = list.get(i).length();
}
}
int max = list.get(0).length();
for(int i = 0; i < list.size(); i++){
if(list.get(i).length() >= max){
max = list.get(i).length();
}
}
int posMin;
for(int i = 0; i < list.size(); i++){
if(list.get(i).length() == min){
posMin = list.indexOf(i);
}
}
int posMax;
for(int i = 0; i < list.size(); i++){
if(list.get(i).length() == max){
posMax = list.indexOf(i);
}
}
if(posMin < posMax){
System.out.println(list.get(posMin));
} else {
System.out.println(list.get(posMax));
}
}
}