Receiving multi-line input and concatenating it into a single string to do something with in a Java console appSplit Java String by New LineStringBuilder vs String concatenation in toString() in JavaPrinting a string of text into multiple lines? JavaReceiving input from command line in JavaSpecial characters not working with StringBuilder.append(String)Console default values when using Scanner(System.in)StringBuilder() vs StringBuilder(null) vs StringBuilder(“”)How to print a line and type input in the same lineProgram runs infinitely when inputting a multi-line String in JAVA (BlueJ) using Scanner

Getting an entry level IT position later in life

Print only the last three columns from file

Colleagues speaking another language and it impacts work

If someone else uploads my GPL'd code to Github without my permission, is that a copyright violation?

Unexpected route on a flight from USA to Europe

Repeated! Factorials!

Where in ש״ס who one find the adage, “He who suggests the idea should carry it out”?

Should I self-publish my novella on Amazon or try my luck getting publishers?

Does the Voyager team use a wrapper (Fortran(77?) to Python) to transmit current commands?

"In charge of" vs "Responsible for"

Probably terminated or laid off soon; confront or not?

Does this smartphone photo show Mars just below the Sun?

The actual purview of Her Majesty The Queen's Perogative?

Are children a reason to be rejected for a job?

Where to pee in London?

Is it a bad idea to offer variants of a final exam based on the type of allowed calculators?

Is there a difference between 「目を覚ます」 and 「目覚める」

Will a paper be retracted if a flaw in released software code invalidates its central idea?

Does the length of a password for Wi-Fi affect speed?

Why don't the open notes matter in guitar chords?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

Onenote - Reducing Storage Footprint on PC

How to realistically deal with a shield user?

What city skyline is this picture of?



Receiving multi-line input and concatenating it into a single string to do something with in a Java console app


Split Java String by New LineStringBuilder vs String concatenation in toString() in JavaPrinting a string of text into multiple lines? JavaReceiving input from command line in JavaSpecial characters not working with StringBuilder.append(String)Console default values when using Scanner(System.in)StringBuilder() vs StringBuilder(null) vs StringBuilder(“”)How to print a line and type input in the same lineProgram runs infinitely when inputting a multi-line String in JAVA (BlueJ) using Scanner






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















The ultimate goal is to take a multi-line user input and combine it all into one single string that can be used later in my code. When I use the code below every sinle line is printed perfectly as output. However, when I use the commented code my end result is not correct.



System.out.println("Enter a string: ");

StringBuilder sb1 = new StringBuilder();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext())
System.out.println(scanner.nextLine());
// String line = scanner.nextLine();
// sb1.append(line);


// System.out.println(sb1.toString());









share|improve this question
































    0















    The ultimate goal is to take a multi-line user input and combine it all into one single string that can be used later in my code. When I use the code below every sinle line is printed perfectly as output. However, when I use the commented code my end result is not correct.



    System.out.println("Enter a string: ");

    StringBuilder sb1 = new StringBuilder();
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext())
    System.out.println(scanner.nextLine());
    // String line = scanner.nextLine();
    // sb1.append(line);


    // System.out.println(sb1.toString());









    share|improve this question




























      0












      0








      0








      The ultimate goal is to take a multi-line user input and combine it all into one single string that can be used later in my code. When I use the code below every sinle line is printed perfectly as output. However, when I use the commented code my end result is not correct.



      System.out.println("Enter a string: ");

      StringBuilder sb1 = new StringBuilder();
      Scanner scanner = new Scanner(System.in);
      while (scanner.hasNext())
      System.out.println(scanner.nextLine());
      // String line = scanner.nextLine();
      // sb1.append(line);


      // System.out.println(sb1.toString());









      share|improve this question
















      The ultimate goal is to take a multi-line user input and combine it all into one single string that can be used later in my code. When I use the code below every sinle line is printed perfectly as output. However, when I use the commented code my end result is not correct.



      System.out.println("Enter a string: ");

      StringBuilder sb1 = new StringBuilder();
      Scanner scanner = new Scanner(System.in);
      while (scanner.hasNext())
      System.out.println(scanner.nextLine());
      // String line = scanner.nextLine();
      // sb1.append(line);


      // System.out.println(sb1.toString());






      java console-application stringbuilder






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 4:30







      Blake Rivell

















      asked Mar 27 at 4:18









      Blake RivellBlake Rivell

      4,52916 gold badges54 silver badges129 bronze badges




      4,52916 gold badges54 silver badges129 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          2














          I'm assuming what you want is for a new line after every input so after



          sb1.append(line);



          add



          sb1.append(System.getProperty("line.separator"));



          Edit: (because i dont know how to post code in comments)
          This will basically keep asking for input until user types in "exit" which it then will break from while loop and print out the string. You can append commas or spaces to separate them after appending line.



          I'm not sure if your original problem was because you weren't breaking from loop or because you called nextLine twice in print and assigning it to line



           public static void main(String[] args) 
          System.out.println("Enter a string: ");
          StringBuilder sb = new StringBuilder();
          try (Scanner scanner = new Scanner(System.in))
          String input;
          while (!(input = scanner.nextLine()).equals("exit"))
          sb.append(input);


          System.out.println(sb.toString());






          share|improve this answer



























          • My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

            – Blake Rivell
            Mar 27 at 4:30












          • @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

            – chris
            Mar 27 at 4:32












          • I don't want to keep the lines. I want it to be a single line string.

            – Blake Rivell
            Mar 27 at 4:36











          • @BlakeRivell Well not exactly sure what format you want but look at main post again

            – chris
            Mar 27 at 4:42












          • your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

            – Blake Rivell
            Mar 27 at 4:48



















          0














          You can try this.



          private static String readFile(String pathname) throws IOException 

          File file = new File(pathname);
          StringBuilder fileContents = new StringBuilder((int)file.length());

          try (Scanner scanner = new Scanner(file))
          while(scanner.hasNextLine())
          fileContents.append(scanner.nextLine() );

          return fileContents.toString();




          This code will read all lines from a text file and give us as a single string as return object.






          share|improve this answer



























          • I am not reading from a file here. I am reading user input in a console app.

            – Blake Rivell
            Mar 27 at 4:49


















          0














          The following is the pom.xml file for your reference:



          <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>json_example</groupId>
          <artifactId>json_example</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <dependencies>
          <dependency>
          <groupId>javax.json</groupId>
          <artifactId>javax.json-api</artifactId>
          <version>1.1.4</version>
          </dependency>
          <dependency>
          <groupId>org.glassfish</groupId>
          <artifactId>javax.json</artifactId>
          <version>1.1.4</version>
          </dependency>
          </dependencies>
          </project>


          Then add maven dependent jar files to your classpath.



          import javax.json.Json;
          import javax.json.JsonObject;
          import javax.json.JsonReader;

          public static void main(String[] args)
          System.out.println("Enter a valid JSON string: ");

          try (// Create JsonReader from Json.
          JsonReader reader = Json.createReader(System.in))
          // Get the JsonObject structure from JsonReader.
          JsonObject jsonObj = reader.readObject();
          System.out.println(jsonObj.toString());
          catch (Exception e)
          e.printStackTrace();








          share|improve this answer



























          • I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

            – Blake Rivell
            Mar 27 at 5:28











          • Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

            – Blake Rivell
            Mar 27 at 13:19












          • I think inputting Json in a console app would be just for testing and debugging purpose.

            – Miller Cy Chan
            Mar 28 at 4:25













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369747%2freceiving-multi-line-input-and-concatenating-it-into-a-single-string-to-do-somet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I'm assuming what you want is for a new line after every input so after



          sb1.append(line);



          add



          sb1.append(System.getProperty("line.separator"));



          Edit: (because i dont know how to post code in comments)
          This will basically keep asking for input until user types in "exit" which it then will break from while loop and print out the string. You can append commas or spaces to separate them after appending line.



          I'm not sure if your original problem was because you weren't breaking from loop or because you called nextLine twice in print and assigning it to line



           public static void main(String[] args) 
          System.out.println("Enter a string: ");
          StringBuilder sb = new StringBuilder();
          try (Scanner scanner = new Scanner(System.in))
          String input;
          while (!(input = scanner.nextLine()).equals("exit"))
          sb.append(input);


          System.out.println(sb.toString());






          share|improve this answer



























          • My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

            – Blake Rivell
            Mar 27 at 4:30












          • @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

            – chris
            Mar 27 at 4:32












          • I don't want to keep the lines. I want it to be a single line string.

            – Blake Rivell
            Mar 27 at 4:36











          • @BlakeRivell Well not exactly sure what format you want but look at main post again

            – chris
            Mar 27 at 4:42












          • your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

            – Blake Rivell
            Mar 27 at 4:48
















          2














          I'm assuming what you want is for a new line after every input so after



          sb1.append(line);



          add



          sb1.append(System.getProperty("line.separator"));



          Edit: (because i dont know how to post code in comments)
          This will basically keep asking for input until user types in "exit" which it then will break from while loop and print out the string. You can append commas or spaces to separate them after appending line.



          I'm not sure if your original problem was because you weren't breaking from loop or because you called nextLine twice in print and assigning it to line



           public static void main(String[] args) 
          System.out.println("Enter a string: ");
          StringBuilder sb = new StringBuilder();
          try (Scanner scanner = new Scanner(System.in))
          String input;
          while (!(input = scanner.nextLine()).equals("exit"))
          sb.append(input);


          System.out.println(sb.toString());






          share|improve this answer



























          • My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

            – Blake Rivell
            Mar 27 at 4:30












          • @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

            – chris
            Mar 27 at 4:32












          • I don't want to keep the lines. I want it to be a single line string.

            – Blake Rivell
            Mar 27 at 4:36











          • @BlakeRivell Well not exactly sure what format you want but look at main post again

            – chris
            Mar 27 at 4:42












          • your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

            – Blake Rivell
            Mar 27 at 4:48














          2












          2








          2







          I'm assuming what you want is for a new line after every input so after



          sb1.append(line);



          add



          sb1.append(System.getProperty("line.separator"));



          Edit: (because i dont know how to post code in comments)
          This will basically keep asking for input until user types in "exit" which it then will break from while loop and print out the string. You can append commas or spaces to separate them after appending line.



          I'm not sure if your original problem was because you weren't breaking from loop or because you called nextLine twice in print and assigning it to line



           public static void main(String[] args) 
          System.out.println("Enter a string: ");
          StringBuilder sb = new StringBuilder();
          try (Scanner scanner = new Scanner(System.in))
          String input;
          while (!(input = scanner.nextLine()).equals("exit"))
          sb.append(input);


          System.out.println(sb.toString());






          share|improve this answer















          I'm assuming what you want is for a new line after every input so after



          sb1.append(line);



          add



          sb1.append(System.getProperty("line.separator"));



          Edit: (because i dont know how to post code in comments)
          This will basically keep asking for input until user types in "exit" which it then will break from while loop and print out the string. You can append commas or spaces to separate them after appending line.



          I'm not sure if your original problem was because you weren't breaking from loop or because you called nextLine twice in print and assigning it to line



           public static void main(String[] args) 
          System.out.println("Enter a string: ");
          StringBuilder sb = new StringBuilder();
          try (Scanner scanner = new Scanner(System.in))
          String input;
          while (!(input = scanner.nextLine()).equals("exit"))
          sb.append(input);


          System.out.println(sb.toString());







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 5:23

























          answered Mar 27 at 4:28









          chrischris

          514 bronze badges




          514 bronze badges















          • My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

            – Blake Rivell
            Mar 27 at 4:30












          • @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

            – chris
            Mar 27 at 4:32












          • I don't want to keep the lines. I want it to be a single line string.

            – Blake Rivell
            Mar 27 at 4:36











          • @BlakeRivell Well not exactly sure what format you want but look at main post again

            – chris
            Mar 27 at 4:42












          • your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

            – Blake Rivell
            Mar 27 at 4:48


















          • My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

            – Blake Rivell
            Mar 27 at 4:30












          • @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

            – chris
            Mar 27 at 4:32












          • I don't want to keep the lines. I want it to be a single line string.

            – Blake Rivell
            Mar 27 at 4:36











          • @BlakeRivell Well not exactly sure what format you want but look at main post again

            – chris
            Mar 27 at 4:42












          • your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

            – Blake Rivell
            Mar 27 at 4:48

















          My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

          – Blake Rivell
          Mar 27 at 4:30






          My goal is to accept all of the lines and concatenate them into a single string. Which will be used elsewhere in my code.

          – Blake Rivell
          Mar 27 at 4:30














          @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

          – chris
          Mar 27 at 4:32






          @BlakeRivell Hmm what I posted with what you have combined should give you a String with the input + a new line after every input which from what I understand is what you want?

          – chris
          Mar 27 at 4:32














          I don't want to keep the lines. I want it to be a single line string.

          – Blake Rivell
          Mar 27 at 4:36





          I don't want to keep the lines. I want it to be a single line string.

          – Blake Rivell
          Mar 27 at 4:36













          @BlakeRivell Well not exactly sure what format you want but look at main post again

          – chris
          Mar 27 at 4:42






          @BlakeRivell Well not exactly sure what format you want but look at main post again

          – chris
          Mar 27 at 4:42














          your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

          – Blake Rivell
          Mar 27 at 4:48






          your answer is exactly what I was trying in my post. If you run the code it doesn't even accept the user input it just goes dead. Which is the issue I am facing. Maybe it is because I am using Java 12... I don't know.

          – Blake Rivell
          Mar 27 at 4:48














          0














          You can try this.



          private static String readFile(String pathname) throws IOException 

          File file = new File(pathname);
          StringBuilder fileContents = new StringBuilder((int)file.length());

          try (Scanner scanner = new Scanner(file))
          while(scanner.hasNextLine())
          fileContents.append(scanner.nextLine() );

          return fileContents.toString();




          This code will read all lines from a text file and give us as a single string as return object.






          share|improve this answer



























          • I am not reading from a file here. I am reading user input in a console app.

            – Blake Rivell
            Mar 27 at 4:49















          0














          You can try this.



          private static String readFile(String pathname) throws IOException 

          File file = new File(pathname);
          StringBuilder fileContents = new StringBuilder((int)file.length());

          try (Scanner scanner = new Scanner(file))
          while(scanner.hasNextLine())
          fileContents.append(scanner.nextLine() );

          return fileContents.toString();




          This code will read all lines from a text file and give us as a single string as return object.






          share|improve this answer



























          • I am not reading from a file here. I am reading user input in a console app.

            – Blake Rivell
            Mar 27 at 4:49













          0












          0








          0







          You can try this.



          private static String readFile(String pathname) throws IOException 

          File file = new File(pathname);
          StringBuilder fileContents = new StringBuilder((int)file.length());

          try (Scanner scanner = new Scanner(file))
          while(scanner.hasNextLine())
          fileContents.append(scanner.nextLine() );

          return fileContents.toString();




          This code will read all lines from a text file and give us as a single string as return object.






          share|improve this answer















          You can try this.



          private static String readFile(String pathname) throws IOException 

          File file = new File(pathname);
          StringBuilder fileContents = new StringBuilder((int)file.length());

          try (Scanner scanner = new Scanner(file))
          while(scanner.hasNextLine())
          fileContents.append(scanner.nextLine() );

          return fileContents.toString();




          This code will read all lines from a text file and give us as a single string as return object.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 5:27









          Suraj Kumar

          2,8095 gold badges11 silver badges26 bronze badges




          2,8095 gold badges11 silver badges26 bronze badges










          answered Mar 27 at 4:47









          sree rajasree raja

          1326 bronze badges




          1326 bronze badges















          • I am not reading from a file here. I am reading user input in a console app.

            – Blake Rivell
            Mar 27 at 4:49

















          • I am not reading from a file here. I am reading user input in a console app.

            – Blake Rivell
            Mar 27 at 4:49
















          I am not reading from a file here. I am reading user input in a console app.

          – Blake Rivell
          Mar 27 at 4:49





          I am not reading from a file here. I am reading user input in a console app.

          – Blake Rivell
          Mar 27 at 4:49











          0














          The following is the pom.xml file for your reference:



          <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>json_example</groupId>
          <artifactId>json_example</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <dependencies>
          <dependency>
          <groupId>javax.json</groupId>
          <artifactId>javax.json-api</artifactId>
          <version>1.1.4</version>
          </dependency>
          <dependency>
          <groupId>org.glassfish</groupId>
          <artifactId>javax.json</artifactId>
          <version>1.1.4</version>
          </dependency>
          </dependencies>
          </project>


          Then add maven dependent jar files to your classpath.



          import javax.json.Json;
          import javax.json.JsonObject;
          import javax.json.JsonReader;

          public static void main(String[] args)
          System.out.println("Enter a valid JSON string: ");

          try (// Create JsonReader from Json.
          JsonReader reader = Json.createReader(System.in))
          // Get the JsonObject structure from JsonReader.
          JsonObject jsonObj = reader.readObject();
          System.out.println(jsonObj.toString());
          catch (Exception e)
          e.printStackTrace();








          share|improve this answer



























          • I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

            – Blake Rivell
            Mar 27 at 5:28











          • Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

            – Blake Rivell
            Mar 27 at 13:19












          • I think inputting Json in a console app would be just for testing and debugging purpose.

            – Miller Cy Chan
            Mar 28 at 4:25















          0














          The following is the pom.xml file for your reference:



          <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>json_example</groupId>
          <artifactId>json_example</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <dependencies>
          <dependency>
          <groupId>javax.json</groupId>
          <artifactId>javax.json-api</artifactId>
          <version>1.1.4</version>
          </dependency>
          <dependency>
          <groupId>org.glassfish</groupId>
          <artifactId>javax.json</artifactId>
          <version>1.1.4</version>
          </dependency>
          </dependencies>
          </project>


          Then add maven dependent jar files to your classpath.



          import javax.json.Json;
          import javax.json.JsonObject;
          import javax.json.JsonReader;

          public static void main(String[] args)
          System.out.println("Enter a valid JSON string: ");

          try (// Create JsonReader from Json.
          JsonReader reader = Json.createReader(System.in))
          // Get the JsonObject structure from JsonReader.
          JsonObject jsonObj = reader.readObject();
          System.out.println(jsonObj.toString());
          catch (Exception e)
          e.printStackTrace();








          share|improve this answer



























          • I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

            – Blake Rivell
            Mar 27 at 5:28











          • Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

            – Blake Rivell
            Mar 27 at 13:19












          • I think inputting Json in a console app would be just for testing and debugging purpose.

            – Miller Cy Chan
            Mar 28 at 4:25













          0












          0








          0







          The following is the pom.xml file for your reference:



          <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>json_example</groupId>
          <artifactId>json_example</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <dependencies>
          <dependency>
          <groupId>javax.json</groupId>
          <artifactId>javax.json-api</artifactId>
          <version>1.1.4</version>
          </dependency>
          <dependency>
          <groupId>org.glassfish</groupId>
          <artifactId>javax.json</artifactId>
          <version>1.1.4</version>
          </dependency>
          </dependencies>
          </project>


          Then add maven dependent jar files to your classpath.



          import javax.json.Json;
          import javax.json.JsonObject;
          import javax.json.JsonReader;

          public static void main(String[] args)
          System.out.println("Enter a valid JSON string: ");

          try (// Create JsonReader from Json.
          JsonReader reader = Json.createReader(System.in))
          // Get the JsonObject structure from JsonReader.
          JsonObject jsonObj = reader.readObject();
          System.out.println(jsonObj.toString());
          catch (Exception e)
          e.printStackTrace();








          share|improve this answer















          The following is the pom.xml file for your reference:



          <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>json_example</groupId>
          <artifactId>json_example</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <dependencies>
          <dependency>
          <groupId>javax.json</groupId>
          <artifactId>javax.json-api</artifactId>
          <version>1.1.4</version>
          </dependency>
          <dependency>
          <groupId>org.glassfish</groupId>
          <artifactId>javax.json</artifactId>
          <version>1.1.4</version>
          </dependency>
          </dependencies>
          </project>


          Then add maven dependent jar files to your classpath.



          import javax.json.Json;
          import javax.json.JsonObject;
          import javax.json.JsonReader;

          public static void main(String[] args)
          System.out.println("Enter a valid JSON string: ");

          try (// Create JsonReader from Json.
          JsonReader reader = Json.createReader(System.in))
          // Get the JsonObject structure from JsonReader.
          JsonObject jsonObj = reader.readObject();
          System.out.println(jsonObj.toString());
          catch (Exception e)
          e.printStackTrace();









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 27 at 8:40

























          answered Mar 27 at 4:56









          Miller Cy ChanMiller Cy Chan

          5255 silver badges11 bronze badges




          5255 silver badges11 bronze badges















          • I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

            – Blake Rivell
            Mar 27 at 5:28











          • Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

            – Blake Rivell
            Mar 27 at 13:19












          • I think inputting Json in a console app would be just for testing and debugging purpose.

            – Miller Cy Chan
            Mar 28 at 4:25

















          • I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

            – Blake Rivell
            Mar 27 at 5:28











          • Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

            – Blake Rivell
            Mar 27 at 13:19












          • I think inputting Json in a console app would be just for testing and debugging purpose.

            – Miller Cy Chan
            Mar 28 at 4:25
















          I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

          – Blake Rivell
          Mar 27 at 5:28





          I run the debug in IntelliJ and paste a multi line json string then press enter and no matter how many times a press enter nothing happens. I will give your code a try tomorrow though.

          – Blake Rivell
          Mar 27 at 5:28













          Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

          – Blake Rivell
          Mar 27 at 13:19






          Maybe it isn't a good idea to have the user paste formatted Json in a console app and I should be doing this a different way like pointing to a file or something. But it should be possible right? My ultimate goal is to pass a user-entered formatted json to final JsonParser parser = Json.createParser(new StringReader(userJsonStr)); Prior to passing it here I need to strip all white spaces and remove all line breaks. Otherwise the parser will error.

          – Blake Rivell
          Mar 27 at 13:19














          I think inputting Json in a console app would be just for testing and debugging purpose.

          – Miller Cy Chan
          Mar 28 at 4:25





          I think inputting Json in a console app would be just for testing and debugging purpose.

          – Miller Cy Chan
          Mar 28 at 4:25

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369747%2freceiving-multi-line-input-and-concatenating-it-into-a-single-string-to-do-somet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript