-1

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

Jens
  • 67,715
  • 15
  • 98
  • 113
soepblik
  • 13
  • 5
  • 4
    if your Loop runs with `i=numbers.size()-1` `i+1` is out of bounds. Change to `i < numbers.size()-1; ` – Jens May 10 '17 at 10:46
  • Now sure why people don't give a try on Google before posting here :( – Mehraj Malik May 10 '17 at 10:48
  • In addtion to @Jens, Exceptions gives you the line number of error. So you can see the source of the error. – Yusuf K. May 10 '17 at 10:48
  • Possible duplicate of [Java: Array Index Out of Bounds Exception](http://stackoverflow.com/questions/3951252/java-array-index-out-of-bounds-exception) – Jaroslaw Pawlak May 10 '17 at 11:13
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – AxelH May 10 '17 at 11:14

1 Answers1

0

Since you need both neighbours of an element to be smaller to add an "*", you can skip the first and the last elements (because they are missing one of the neighbours).

Adapting your code with this consideration:

import java.util.ArrayList;
import java.util.Scanner;


public class Tops {

private static final Scanner INPUT_SCANNER = new Scanner(System.in);


public static void calculate() {
    // Initialize numbers
    ArrayList<String> numbers = new ArrayList<>();
    String number = INPUT_SCANNER.next();
    while (!"0".equals(number)) {
        numbers.add(number);
        number = INPUT_SCANNER.next();
    }

    // Check the neighbours
    ArrayList<String> after = new ArrayList<>();

    // Check first element
    if (numbers.get(0) > numbers.get(1)) {
        after.add(numbers.get(0) + "*");
    } else {
        after.add(numbers.get(0));
    }
    // Check other elements
    for (int i = 1; i < numbers.size() - 1; i++) {
        int previous = Integer.parseInt(numbers.get(i - 1));
        int current = Integer.parseInt(numbers.get(i));
        int next = Integer.parseInt(numbers.get(i + 1));

        String newElement = numbers.get(i);
        if (current > next && current > previous) {
            newElement = newElement + "*";
        }

        after.add(newElement);
    }
    // Check last element
    if (numbers.get(numbers.size() - 1) > numbers.get(numbers.size() - 2)) {
        after.add(numbers.get(numbers.size() - 1) + "*");
    } else {
        after.add(numbers.get(numbers.size() - 1));
    }
    after.add(numbers.get(numbers.size() - 1));

    // Show the result
    for (int i = 0; i < after.size(); i++) {
        System.out.print(after.get(i) + " ");
    }
}

public static void main(String[] args) {
    calculate();
}

}

Execution example:

INPUT: 1 2 3 2 1 0
OUTPUT: 1 2 3* 2 1

Notice that:

  • I have made your calculate method static since you do not have any non-static field on the Tops class
  • I have moved the numbers initialization to the begining of the method
  • The final numbers are added on the after list only once so you do not need to initialize it
  • To perform the comparison without converting the current number from string to int twice per loop iteration, I have moved the int formats out of the if comparison.
Cristian Ramon-Cortes
  • 1,838
  • 1
  • 19
  • 32