yoges nsamy
yoges nsamy

Reputation: 1435

Java: Possible to use a single for-loop instead of two?

My code is as below. It first takes input from user and prints it in reverse. I'm new to Java. I achieve this by using two 'for loops' to first iterate through the input and another for-loop to print the numbers in reverse. My question is if there's any way to improve my code - by using just a single loop perhaps? Any suggestion is appreciated. Thank you.

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

    for (int arr_i = 0; arr_i < n; arr_i++) {
        arr[arr_i] = in.nextInt();
    }

    for (int reverse_i = n-1; reverse_i >= 0; reverse_i--) {
        System.out.print(arr[reverse_i]);

        if (reverse_i != 0) {
            System.out.print(" ");
        }
    }
}

An example input:

4
1 2 3 4

Expected output:

4 3 2 1

Upvotes: 0

Views: 726

Answers (5)

Ms. Zia
Ms. Zia

Reputation: 489

The simplest approach I found is to use String Builder here:

Scanner in = new Scanner(System.in);
StringBuilder stringBuilder = new StringBuilder();
while (in.hasNext()) {
   stringBuilder.append(in.next());
   if(in.hasNext()) {
     stringBuilder.append(" ");
   }
}

System.out.print(stringBuilder.reverse());

Upvotes: 2

Solace
Solace

Reputation: 2189

I am not sure about the requirements of your request, but you do not necessarily have to use an array for this. You can concatenate the inputs into a String in reverse order like so.

public static void main(String[] args) {
    System in = new Scanner(System.in);
    int n = in.nextInt();
    String allNumbers = "";

    for (int i = 0; i < n; i++) {
        int current = in.nextInt();
        allNumbers = current + " " + allNumbers;
    }

    if(allNumbers != ""){
      allNumbers = allNumbers.substring(0,allNumbers.length()-1);
    }

    System.out.println(allNumbers);
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201477

First, you might reverse your elements as you insert them in the array. Then, assuming you are using Java 8+, you could use an IntStream instead of a loop and print with a basic Collector. Like,

Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
IntStream.range(0, n).forEachOrdered(i -> arr[n - i - 1] = in.nextInt());
System.out.println(IntStream.of(arr).mapToObj(String::valueOf)
        .collect(Collectors.joining(" ")));

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44854

Use a StringBuilder and always insert at 0 index.

See: Oracle » JavaDocs » 1.7 » java.lang.StringBuilder.insert(int, int)

StringBuilder bld = new StringBuilder();

for (int arr_i = 0; arr_i < n; arr_i++) {
    int i = in.nextInt();
    bld.insert(0, i);
}

System.out.println(bld.toString());

Upvotes: 4

Rohan Rayarikar
Rohan Rayarikar

Reputation: 240

Java has inbuilt String method for the same. If your input is a String, you can use the below-

String s1 = new String("new");
String s2 = s1.reverse();
System.out.println(s2) // wen

Upvotes: -1

Related Questions