2

I want to simply write code that inserts my userItems into the output string stream itemsOSS. Each of my item will be followed by a space. I want the output to reflect the following for example, "red purple yellow Exit":

Output example,

red purple yellow

public class StringStreamOutput {

   public static void main (String [] args) {

      Scanner scnr = new Scanner(System.in);
      String userItem = "";

      StringWriter itemCharStream = new StringWriter();
      PrintWriter itemsOSS = new PrintWriter(itemCharStream);

      System.out.println("Enter items (type Exit to quit):");
      userItem = scnr.next();

      while (!userItem.equals("Exit")) {

         // confused here, 

         userItem = scnr.next();
      }

      userItem = itemCharStream.toString();
      System.out.println(userItem);

      return;
   }
}
3
  • What is "output string stream itemsOSS" ? I don't see anything that reflects something like that in your code. Commented Nov 11, 2017 at 14:49
  • PrintWriter has a print(String) method: docs.oracle.com/javase/7/docs/api/java/io/…. So what's the concrete problem? Commented Nov 11, 2017 at 14:50
  • I found the code that works, I just needed to utilize the print method. itemsOSS.print(userItem + " "); userItem = itemCharStream.toString(); Commented Nov 11, 2017 at 15:15

2 Answers 2

3

I found the code that works, I just needed to utilize the print method.

itemsOSS.print(userItem + " "); 

userItem = itemCharStream.toString();
Sign up to request clarification or add additional context in comments.

Comments

0
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.StringWriter;

public class StringStreamOutput {
   public static void main (String [] args) {
      Scanner scnr = new Scanner(System.in);
      String userItem;

      StringWriter itemCharStream = new StringWriter();
      PrintWriter itemsOSS = new PrintWriter(itemCharStream);

      System.out.println("Enter items (type Exit to quit):");
      userItem = scnr.next();

      while (!userItem.equals("Exit")) {

         /* Your solution goes here  */
         itemsOSS.print(userItem + " ");


         /* this does not to be added as it sits outside the loop
         userItem = itemCharStream.toString();
         */

         userItem = scnr.next();
      }

      userItem = itemCharStream.toString();
      System.out.println(userItem);
   }
}

This works

3 Comments

Can you explain it a bit Robert?
Sure. The only actual thing I added was itemsOSS.print(userItem + " "). This uses the PrintWriter to modify its parameter of ItemCharString. Then userItem is assigned the itemCharString and also calls it toString() method to later be printed as a string. The rest of the code is what the user provided in the original question. And I multi-line commented out the code userItem = itemCharString.toString() because it was already declared outside of the while loop and therefore was just redundant code.
Sorry I can also explain a bit more on the one line I added. So the itemOSS.print(userItem + " ") calls forth the PrintWriter that has a parameter of the StringWriters itemCharString. This will modify the itemCharString with the parameters of userItem and then add a space between each call of the loop. Because of this we can leave the other userItem = itemCharString.toString(); code out of the loop because we are directly modifying itemCharString through the PrintWriters parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.