Write a java program that reads in 20 strings from the keybo

Write a java program that reads in 20 strings from the keyboard and then displays them in reverse order. Read the strings into an array. Then write a second program like the first, but use an ArrayList instead of an array.

Solution

ReverseDisplay.java

import java.util.Scanner;


public class ReverseDisplay {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       String s[] = new String[20];
       System.out.println(\"Enter 20 strings: \");
       for(int i=0; i<s.length; i++){
           s[i] = scan.nextLine();
       }
       System.out.println(\"Dispaying 20 string in reverse order: \");
       for(int i=s.length-1; i>=0; i--){
           System.out.println(s[i]);
       }
      

   }

}

Output:

Enter 20 strings:
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh
iiii
jjjj
kkkk
llll
mmmmm
nnnn
oooo
ppppp
qqqqq
rrrrr
ssssss
ttttt
Dispaying 20 string in reverse order:
ttttt
ssssss
rrrrr
qqqqq
ppppp
oooo
nnnn
mmmmm
llll
kkkk
jjjj
iiii
hhhh
gggg
ffff
eeee
dddd
cccc
bbbb
aaaa

ReverseDisplay2.java

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


public class ReverseDisplay2 {
  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       ArrayList<String> list = new ArrayList<String>();
       System.out.println(\"Enter 20 strings: \");
       for(int i=0; i<20; i++){
           list.add(scan.nextLine());
       }
       System.out.println(\"Dispaying 20 string in reverse order: \");
       for(int i=list.size()-1; i>=0; i--){
           System.out.println(list.get(i));
       }
      

   }
}

Output:

Enter 20 strings:
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh
iiii
jjjj
kkkk
llll
mmmmm
nnnn
oooo
ppppp
qqqqq
rrrrr
ssssss
ttttt
Dispaying 20 string in reverse order:
ttttt
ssssss
rrrrr
qqqqq
ppppp
oooo
nnnn
mmmmm
llll
kkkk
jjjj
iiii
hhhh
gggg
ffff
eeee
dddd
cccc
bbbb
aaaa

Write a java program that reads in 20 strings from the keyboard and then displays them in reverse order. Read the strings into an array. Then write a second pro
Write a java program that reads in 20 strings from the keyboard and then displays them in reverse order. Read the strings into an array. Then write a second pro
Write a java program that reads in 20 strings from the keyboard and then displays them in reverse order. Read the strings into an array. Then write a second pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site