0

I would like this array to print without the brackets when it is called by the main method.

public static void main(String[] args) {    
    int [] temps = {15, 43, 23, 20}; 
    System.out.print(Arrays.toString(arrayIncrease(temps)));
}

public static int[] arrayIncrease (int[] array) {

    int [] tempsUp = new int[array.length];    
    for (int i = 0; i < array.length; i++)     
        tempsUp[i] = array[i] + 10; 

    return tempsUp; 
}

Current output

[25, 53, 33, 30]
azro
  • 53,056
  • 7
  • 34
  • 70

3 Answers3

0

You could use substring to extract the part inside the brackets in the string:

String output = Arrays.toString(arrayIncrease(temps);
System.out.print(output.substring(1, output.length() - 1);
Victor P
  • 1,496
  • 17
  • 29
0

In general case, it's not correct to remove [] from the string (it could be changed). It's better to control output on your own:

public static void main(String[] args) throws IOException {
    int[] temps = { 15, 43, 23, 20 };
    arrayIncrease(temps, 10);
    System.out.println(toString(arrayIncrease(temps, 10)));
}

public static int[] arrayIncrease(int[] arr, int inc) {
    return Arrays.stream(arr)
                 .map(val -> val + inc)
                 .toArray();
}

public static String toString(int[] arr) {
    return Arrays.stream(arr)
                 .mapToObj(Integer::toString)
                 .collect(Collectors.joining(", "));
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

You can do so using a Java stream

Arrays.stream(temps).forEach(System.out::println);
thomasters
  • 183
  • 1
  • 15