0

how can i print the result without brackets

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i < n; i++){
            arr[i] = in.nextInt();

        }

        int[] reverse =new int[n];
       for (int i = 0; i < reverse.length; i++) {
        reverse[i]=arr[arr.length-1-i];
    }
             in.close();
    }
}
2
  • 1
    That's like asking how can I print [] without printing []. Don't use Arrays.toString if you don't want the [] Commented Apr 10, 2016 at 8:26
  • Use String#substring for removing surrounding brackets from your string. Call str = str.substring(1, str.length() - 1); Commented Apr 10, 2016 at 8:32

2 Answers 2

1

In java8, you can conveniently do any kind of String output using join, there you will have to do some manual reversing though:

String output = String.join(", ", arr);
Sign up to request clarification or add additional context in comments.

Comments

1

You can't change the implementation of Arrays.toString() so you can't print your array like that.

Although, you can assign it to an string variable and then manipulate that string as you desire.

String str = Arrays.toString(reverse);
str = str.substring(1, str.length() - 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.