Stuck on this for a couple hours now. someone please give me some insight on what i am doing wrong
package com.codegym.task.task07.task0718;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Checking the order
*/
public class Solution {
public static void main(String[] args) throws IOException {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> array = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
array.add(reader.readLine());
}
//index represents the next number of array
int index = 1;
//for loop goes through all elements of array
for (int i = 0; i < array.size(); i++) {
//if next length is greater than previous length
if (array.get(index).length() > array.get(i).length()) {
//increment index(go to next number)
index++;
//when index reaches the end of the array, stop loop
if (index == array.size()) {
break;
}
//else if next length is less than or equal to previous length
} else if (array.get(index).length() <= array.get(i).length()) {
System.out.println(array.get(index));
break;
}
}
}
}
larger thenshorter than the length of the previous one. I came on here because i wasnt sure i understood the question asked. As i understand now, every new input should be larger in length than the previous one, so it is ordered correctly if the current one is larger than the previous one. Reading your comment made me quite confused again, as you say it should be shorter to be ordered :) Correct me if i mistunderstand! Thanks, and your answer i read higher up helped me out alot to understand, the commenting is really clear, i need to introduce it to my own code more often as well.