For the given problem, first read the input from console using scanner class. Then print the input into the print writer and scan the next word using the scanner. This will continues until we did not get ‘Exit’ word. The loop will terminate when user enter exit. Now, append the text and print the string.
Further Explanation:
Code: The below Java code that inserts the user items into output stream items until the user enters “exit”.
// Scanner class to take input from the keyboards
import java.util.Scanner;
//Enables to find the characters written to a writer as a string
import java.io.StringWriter;
// Used to print the objects in text-output stream
import java.io.PrintWriter;
//class definition
public class UserInputsOutputs
{
public static void main (String [] args)
{
//Scanner class to read the input
Scanner scan = new Scanner(System.in);
String userItem = "";
// printwriter to print items on the console.
StringWriter itemCharStream = new StringWriter();
PrintWriter stringItems = new PrintWriter(itemCharStream);
System.out.println("Enter items (type Exit to quit):");
userItem = scan.next();
// Loop terminates when user enter Exit
while (!userItem.equals("Exit"))
{
stringItems.append(userItem+" ");
userItem = scan.next();
}
userItem = itemCharStream.toString();
// Display the string
System.out.println(userItem);
return;
}
}
Learn more:
1. A company that allows you to license software monthly to use online is an example of ? brainly.com/question/10410011
2. Prediction accuracy of a neural network depends on _______________ and ______________. brainly.com/question/10599832
3. The shape of our galaxy was determined ‘on the inside looking out' by surveying the milky way using ____________ telescopes. brainly.com/question/7866623
Answer details:
Grade: College Engineering
Subject: Computer Science and Engineering
Chapter: Java Programming
Keyword:
Java, programming, console, user, items, string, append, string, writer, scanner, input, output, red, purple, yellow, stream, print, next, loop, while, for, if else statement, println