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;
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
add a comment |
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
add a comment |
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
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
java
asked Mar 24 at 21:49
TimidTomatoTimidTomato
52
52
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you. I got hung up on the wrong places. Thank you for your assistance.
– TimidTomato
Mar 24 at 22:34
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thank you. I got hung up on the wrong places. Thank you for your assistance.
– TimidTomato
Mar 24 at 22:34
add a comment |
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
Thank you. I got hung up on the wrong places. Thank you for your assistance.
– TimidTomato
Mar 24 at 22:34
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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