I am working on a small program for school where i need to input an array. Every element in the array must be checked if the neighbours are smaller than the Element. If the neighbours are smaller --> This elements get an extra *
But i got problems with the boundaries, for example the first one and the last one. The first one has only one neighbour.
public class Tops {
Scanner sc = new Scanner(System.in);
public void calculate(){
String number;
ArrayList<String> numbers; //def’s, decl’s ArrayList
ArrayList<String> after;
numbers = new ArrayList<>(); //creates numbers
after = new ArrayList<>();
number = sc.next();
while (!"0".equals(number)) {
numbers.add(number); //initializes each ArrayList element
number = sc.next();
}
for (int i = 0; i < numbers.size(); i++) {
if (Integer.parseInt(numbers.get(i)) > Integer.parseInt(numbers.get(i+1)) && Integer.parseInt(numbers.get(i)) > Integer.parseInt(numbers.get(i-1)) ){
String replace = numbers.get(i)+"*";
after.add(replace);
} else {
after.add(numbers.get(i));
}
}
for(int i=0;i<after.size();i++){
System.out.print(after.get(i)+ " ");
}
}
public static void main(String[] args) {
new Tops().calculate();
}
}
i hope you can help me