HI,
as the title say, can somebody explain me why my solution is wrong, please.
First to explain my code:
It takes arbitrarily many strings as input. And checks the ArrayList for the longest. If there is more than 1 or more longest values. It puts them all in a solution ArrayList and at the end in outputs everything in the solution ArrayList.
Best Regards
Steffen
package de.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Längste Zeichenkette
*/
public class Solution {
private static List<String> strings = new ArrayList<String>();
private static List<String> solution = new ArrayList<String>();
public static void main(String[] args) throws Exception {
//schreib hier deinen Code
int max_value = 0;
String tmp = "";
//Input
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true){
tmp = in.readLine();
if(tmp != null){
strings.add(tmp);
//System.out.println("New element read: " + tmp);
}else{
break;
}
}
//Calculation
for(int i = 0; i < 3*strings.size(); i++){
for(int j = 0; j < strings.size(); j++){
tmp = strings.get(j);
if(tmp.length() > max_value){
max_value = tmp.length();
//System.out.println("New max: " + max_value);
solution.add(tmp);
//System.out.println("Added new Element: " + tmp + " new length");
solution.clear();
}
else if((tmp.length() == max_value)&&(!solution.contains(tmp))){
solution.add(tmp);
//solution.remove(j);
//System.out.println("Added new Element: " + tmp + " same length");
}
}
}
//Output
while(true){
tmp = solution.get(0);
System.out.println(tmp);
solution.remove(0);
if(solution.size() == 0){
break;
}
}
}
}