Please see below help would be appreciated thank you! Intro to JAVA Programming
ID: 3700843 • Letter: P
Question
Please see below help would be appreciated thank you! Intro to JAVA Programming
________________________________________________________________________________________________________________________________
Write a program that reads in words and prints them out in reverse order. Complete this code.
import java.util.ArrayList;
import java.util.Scanner;
public class ReverseInput
{
public static void main(String[] args)
{
ArrayList<String> words = new ArrayList<String>();
Scanner in = new Scanner(System.in);
// Read input into words
while (in.hasNext())
{
. . .
}
// Reverse input
for (. . .)
{
System.out.print(words.get(i) + " ");
}
}
}
Explanation / Answer
ReverseInput.java
import java.util.ArrayList;
import java.util.Scanner;
public class ReverseInput {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<String>();
Scanner in = new Scanner(System.in);
while(in.hasNext()){
words.add(in.next());
}
for(int i=words.size()-1; i>=0; i--){
System.out.print(words.get(i)+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.