Tomas
Tomas

Reputation: 305

Printing two lines in reverse order - java

Programmers,

I am new to Java and for a certain tutorial I have to write a program which first gives the possibility to type two lines and consequently prints those two lines in reversed order. This is what I got so far, but at this point the program first gives the possibility to type line 1, then prints line 1 and then let's me write line 2 and print line 2. I need to make clear that the program has to work in some specific order but I do not know what commands to use. Who helps me out?

import java.util.Scanner;

public class DoubleEchoLine {

    public static void main(String[] args) {
        Scanner myScanner1 = new Scanner(System.in);
        Scanner myScanner2 = new Scanner(System.in);

        System.out.println(myScanner2.nextLine());
        System.out.println(myScanner1.nextLine());
    }

}

Upvotes: 0

Views: 591

Answers (2)

Nambi
Nambi

Reputation: 12042

you are printing the value at the same time.store it in a variable and then print.

String a=myScanner2.nextLine();
String b=myScanner2.nextLine();
System.out.println(b);
System.out.println(a);

Upvotes: 0

Migol
Migol

Reputation: 8447

myScanner1.nextLine() aparently is the moment you load text.

Try this:

import java.util.Scanner;

public class DoubleEchoLine {

    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        String line1 = myScanner.nextLine();
        String line2 = myScanner.nextLine();

        System.out.println(line2);
        System.out.println(line1);
    }

}

Upvotes: 1

Related Questions