简体   繁体   中英

How to insert string into the output string stream

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;
   }
}

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

itemsOSS.print(userItem + " "); 

userItem = itemCharStream.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM