How to read in multiple characters from a file, multiply the character into a grid and print back to a new fileHow to pretty print XML from Java?How do I get a platform-dependent new line character?How do I create a Java string from the contents of a file?How to read all files in a folder from Java?How to read a large text file line by line using Java?How to remove the last character from a string?JAVA - reading from text file, recognizing new linesHow can i multiply all the numbers by 10 in java file I/OWhat is the best way to read in chars from a text file while ignoring extra spaces from new lines?How to count the occurrence of each letter of the alphabet from a text file in Java?

Make Gimbap cutter

Was planting UN flag on Moon ever discussed?

So a part of my house disappeared... But not because of a chunk resetting

Difference between prepositions in "...killed during/in the war"

Why ambiguous grammars are bad?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

C++ logging library

Multiband vertical antenna not working as expected

Canada travel to US using Global Entry

Do you really need a KDF when you have a PRF?

Is Lambda Calculus purely syntactic?

Cooling tower for nuclear power plants

Command of files and size

What should I be wary of when insurer is taking a lot of time to decide whether car is repairable or a total loss?

Diatonic chords of a pentatonic vs blues scale?

How can one's career as a reviewer be ended?

What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Suppose leased car is totalled: what are financial implications?

How to avoid typing 'git' at the begining of every Git command

What is the Leave No Trace way to dispose of coffee grounds?

How durable are silver inlays on a blade?

Why do radiation hardened IC packages often have long leads?

Should I refuse to be named as co-author of a low quality paper?



How to read in multiple characters from a file, multiply the character into a grid and print back to a new file


How to pretty print XML from Java?How do I get a platform-dependent new line character?How do I create a Java string from the contents of a file?How to read all files in a folder from Java?How to read a large text file line by line using Java?How to remove the last character from a string?JAVA - reading from text file, recognizing new linesHow can i multiply all the numbers by 10 in java file I/OWhat is the best way to read in chars from a text file while ignoring extra spaces from new lines?How to count the occurrence of each letter of the alphabet from a text file in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to read multiple characters from a file multiply each character so that when I print it to a new file they appear in a grid like format.



 int num = 4;
String fileStr = "";

scnrIn.useDelimiter("zzzzzzzzz");

while(scnrIn.hasNextLine())
fileStr = scnrIn.nextLine();

char[] charArray = fileStr.toCharArray();

for(int i = 0; i < charArray.length; ++i)
for(int j = 0; j < Math.sqrt(num); ++j)
writer.write(charArray[i]);
for(int k = 1; k < Math.sqrt(num); ++k)
writer.write(charArray[i]);

writer.newLine();
writer.flush();





}


}



If my txt file contains the characters @#$ and my mulitplier is 4 I expect the new txt file to print:



@@##$$
@@##$$


but instead I get:



@@
@@
##
##
$$
$$


I feel like the issue is tied to writer.newLine(); but if I take it away or comment it out then it won't print it in a grid. I am not sure how to get around the need for a new line to print in a grid.










share|improve this question




























    0















    I am trying to read multiple characters from a file multiply each character so that when I print it to a new file they appear in a grid like format.



     int num = 4;
    String fileStr = "";

    scnrIn.useDelimiter("zzzzzzzzz");

    while(scnrIn.hasNextLine())
    fileStr = scnrIn.nextLine();

    char[] charArray = fileStr.toCharArray();

    for(int i = 0; i < charArray.length; ++i)
    for(int j = 0; j < Math.sqrt(num); ++j)
    writer.write(charArray[i]);
    for(int k = 1; k < Math.sqrt(num); ++k)
    writer.write(charArray[i]);

    writer.newLine();
    writer.flush();





    }


    }



    If my txt file contains the characters @#$ and my mulitplier is 4 I expect the new txt file to print:



    @@##$$
    @@##$$


    but instead I get:



    @@
    @@
    ##
    ##
    $$
    $$


    I feel like the issue is tied to writer.newLine(); but if I take it away or comment it out then it won't print it in a grid. I am not sure how to get around the need for a new line to print in a grid.










    share|improve this question
























      0












      0








      0








      I am trying to read multiple characters from a file multiply each character so that when I print it to a new file they appear in a grid like format.



       int num = 4;
      String fileStr = "";

      scnrIn.useDelimiter("zzzzzzzzz");

      while(scnrIn.hasNextLine())
      fileStr = scnrIn.nextLine();

      char[] charArray = fileStr.toCharArray();

      for(int i = 0; i < charArray.length; ++i)
      for(int j = 0; j < Math.sqrt(num); ++j)
      writer.write(charArray[i]);
      for(int k = 1; k < Math.sqrt(num); ++k)
      writer.write(charArray[i]);

      writer.newLine();
      writer.flush();





      }


      }



      If my txt file contains the characters @#$ and my mulitplier is 4 I expect the new txt file to print:



      @@##$$
      @@##$$


      but instead I get:



      @@
      @@
      ##
      ##
      $$
      $$


      I feel like the issue is tied to writer.newLine(); but if I take it away or comment it out then it won't print it in a grid. I am not sure how to get around the need for a new line to print in a grid.










      share|improve this question














      I am trying to read multiple characters from a file multiply each character so that when I print it to a new file they appear in a grid like format.



       int num = 4;
      String fileStr = "";

      scnrIn.useDelimiter("zzzzzzzzz");

      while(scnrIn.hasNextLine())
      fileStr = scnrIn.nextLine();

      char[] charArray = fileStr.toCharArray();

      for(int i = 0; i < charArray.length; ++i)
      for(int j = 0; j < Math.sqrt(num); ++j)
      writer.write(charArray[i]);
      for(int k = 1; k < Math.sqrt(num); ++k)
      writer.write(charArray[i]);

      writer.newLine();
      writer.flush();





      }


      }



      If my txt file contains the characters @#$ and my mulitplier is 4 I expect the new txt file to print:



      @@##$$
      @@##$$


      but instead I get:



      @@
      @@
      ##
      ##
      $$
      $$


      I feel like the issue is tied to writer.newLine(); but if I take it away or comment it out then it won't print it in a grid. I am not sure how to get around the need for a new line to print in a grid.







      java






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 21:49









      TimidTomatoTimidTomato

      52




      52






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Your problem is related to the nesting of your for loops. If you follow what your code is doing, you are writing out the one current character at i in a grid and then going on to the next character in the charArray.



          Instead what you should be doing is going by the rows and printing out the current character's number of columns for each character before going to a new line. This mainly requires switching the two most outer for loops and then fixing how often you call write.



          Here is the code working as expected:



          int num = 4; 
          int cols = (int) Math.sqrt(num); //easier to read the code when stored in a variable
          int rows = cols;

          String fileStr = "";

          scnrIn.useDelimiter("zzzzzzzzz");

          while(scnrIn.hasNextLine())
          fileStr = scnrIn.nextLine();
          char[] charArray = fileStr.toCharArray();

          for (int i = 0; i < rows; i++)
          for (int j = 0; j < charArray.length; j++)
          char curChar = charArray[j]; //this is the current character we want to write out for the number of columns times

          for (int k = 0; k < cols; k++)
          writer.write(curChar);


          writer.newLine(); //create a new line after every row

          writer.flush(); //better to only flush output when fully done






          share|improve this answer























          • Thank you. I got hung up on the wrong places. Thank you for your assistance.

            – TimidTomato
            Mar 24 at 22:34











          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%2f55328914%2fhow-to-read-in-multiple-characters-from-a-file-multiply-the-character-into-a-gr%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your problem is related to the nesting of your for loops. If you follow what your code is doing, you are writing out the one current character at i in a grid and then going on to the next character in the charArray.



          Instead what you should be doing is going by the rows and printing out the current character's number of columns for each character before going to a new line. This mainly requires switching the two most outer for loops and then fixing how often you call write.



          Here is the code working as expected:



          int num = 4; 
          int cols = (int) Math.sqrt(num); //easier to read the code when stored in a variable
          int rows = cols;

          String fileStr = "";

          scnrIn.useDelimiter("zzzzzzzzz");

          while(scnrIn.hasNextLine())
          fileStr = scnrIn.nextLine();
          char[] charArray = fileStr.toCharArray();

          for (int i = 0; i < rows; i++)
          for (int j = 0; j < charArray.length; j++)
          char curChar = charArray[j]; //this is the current character we want to write out for the number of columns times

          for (int k = 0; k < cols; k++)
          writer.write(curChar);


          writer.newLine(); //create a new line after every row

          writer.flush(); //better to only flush output when fully done






          share|improve this answer























          • Thank you. I got hung up on the wrong places. Thank you for your assistance.

            – TimidTomato
            Mar 24 at 22:34















          0














          Your problem is related to the nesting of your for loops. If you follow what your code is doing, you are writing out the one current character at i in a grid and then going on to the next character in the charArray.



          Instead what you should be doing is going by the rows and printing out the current character's number of columns for each character before going to a new line. This mainly requires switching the two most outer for loops and then fixing how often you call write.



          Here is the code working as expected:



          int num = 4; 
          int cols = (int) Math.sqrt(num); //easier to read the code when stored in a variable
          int rows = cols;

          String fileStr = "";

          scnrIn.useDelimiter("zzzzzzzzz");

          while(scnrIn.hasNextLine())
          fileStr = scnrIn.nextLine();
          char[] charArray = fileStr.toCharArray();

          for (int i = 0; i < rows; i++)
          for (int j = 0; j < charArray.length; j++)
          char curChar = charArray[j]; //this is the current character we want to write out for the number of columns times

          for (int k = 0; k < cols; k++)
          writer.write(curChar);


          writer.newLine(); //create a new line after every row

          writer.flush(); //better to only flush output when fully done






          share|improve this answer























          • Thank you. I got hung up on the wrong places. Thank you for your assistance.

            – TimidTomato
            Mar 24 at 22:34













          0












          0








          0







          Your problem is related to the nesting of your for loops. If you follow what your code is doing, you are writing out the one current character at i in a grid and then going on to the next character in the charArray.



          Instead what you should be doing is going by the rows and printing out the current character's number of columns for each character before going to a new line. This mainly requires switching the two most outer for loops and then fixing how often you call write.



          Here is the code working as expected:



          int num = 4; 
          int cols = (int) Math.sqrt(num); //easier to read the code when stored in a variable
          int rows = cols;

          String fileStr = "";

          scnrIn.useDelimiter("zzzzzzzzz");

          while(scnrIn.hasNextLine())
          fileStr = scnrIn.nextLine();
          char[] charArray = fileStr.toCharArray();

          for (int i = 0; i < rows; i++)
          for (int j = 0; j < charArray.length; j++)
          char curChar = charArray[j]; //this is the current character we want to write out for the number of columns times

          for (int k = 0; k < cols; k++)
          writer.write(curChar);


          writer.newLine(); //create a new line after every row

          writer.flush(); //better to only flush output when fully done






          share|improve this answer













          Your problem is related to the nesting of your for loops. If you follow what your code is doing, you are writing out the one current character at i in a grid and then going on to the next character in the charArray.



          Instead what you should be doing is going by the rows and printing out the current character's number of columns for each character before going to a new line. This mainly requires switching the two most outer for loops and then fixing how often you call write.



          Here is the code working as expected:



          int num = 4; 
          int cols = (int) Math.sqrt(num); //easier to read the code when stored in a variable
          int rows = cols;

          String fileStr = "";

          scnrIn.useDelimiter("zzzzzzzzz");

          while(scnrIn.hasNextLine())
          fileStr = scnrIn.nextLine();
          char[] charArray = fileStr.toCharArray();

          for (int i = 0; i < rows; i++)
          for (int j = 0; j < charArray.length; j++)
          char curChar = charArray[j]; //this is the current character we want to write out for the number of columns times

          for (int k = 0; k < cols; k++)
          writer.write(curChar);


          writer.newLine(); //create a new line after every row

          writer.flush(); //better to only flush output when fully done







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 22:16









          KarlKarl

          1,3152717




          1,3152717












          • Thank you. I got hung up on the wrong places. Thank you for your assistance.

            – TimidTomato
            Mar 24 at 22:34

















          • Thank you. I got hung up on the wrong places. Thank you for your assistance.

            – TimidTomato
            Mar 24 at 22:34
















          Thank you. I got hung up on the wrong places. Thank you for your assistance.

          – TimidTomato
          Mar 24 at 22:34





          Thank you. I got hung up on the wrong places. Thank you for your assistance.

          – TimidTomato
          Mar 24 at 22:34



















          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%2f55328914%2fhow-to-read-in-multiple-characters-from-a-file-multiply-the-character-into-a-gr%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