Converting a Scanner File into a String that contains the entire file output in Java












0














I am working on a MabLibs project that is supposed to iterate through a file, prompt you with anything contained in a <> block and allow you to write over to a new file.



I cannot figure out how to use a Scanner to read a file and turn that into a string so I can use the .length() method to iterate a for loop through the file to find these <> blocks.



I can only use Scanner and can't use array lists, so unfortunately the for loop is the only way I can do this.



Here's the code:



import java.io.*;
import java.util.*;

public class MadLibs {

public static void main(String args)
throws FileNotFoundException {
intro();
madLib();


}

public static void intro() {
System.out.println("Welcome to the game of Mad Libs.");
System.out.println("I will ask you to provide various words");
System.out.println("and phrases to fill in a story.");
System.out.println("The result will be written to an output file.");
System.out.println();
}


public static void madLib() throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String r = input.next();
while (!(r.equalsIgnoreCase("c") || r.equalsIgnoreCase("v")
|| r.equalsIgnoreCase("q"))) {
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
}
if (r.equalsIgnoreCase("v")) {
viewFile();
}
else if (r.equalsIgnoreCase("c")) {
createWord();
}
}


public static void createWord() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
String input2 = input1;
PrintStream output = new PrintStream(new File(toRead));
while (input1.hasNext()) {
String input = input1.next();
for (int i = 0; i < input2.length(); i++) {
if (input.startsWith("<") && input.endsWith(">")) {
String token = input.substring(1, input.length() - 1);
System.out.println("Please input a: " + token);
Scanner scan = new Scanner(System.in);
String replacement = scan.nextLine();
token = token.replace(token, replacement);
output.print(token);
}
}
}
}


public static void viewFile() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
System.out.println();
while (input1.hasNextLine()) {
System.out.println(input1.nextLine());
}
}
}


Any help is greatly appreciated.










share|improve this question




















  • 1




    The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
    – Scary Wombat
    Nov 13 '18 at 1:08










  • Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
    – Scary Wombat
    Nov 13 '18 at 1:09






  • 1




    does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
    – Deadpool
    Nov 13 '18 at 1:18


















0














I am working on a MabLibs project that is supposed to iterate through a file, prompt you with anything contained in a <> block and allow you to write over to a new file.



I cannot figure out how to use a Scanner to read a file and turn that into a string so I can use the .length() method to iterate a for loop through the file to find these <> blocks.



I can only use Scanner and can't use array lists, so unfortunately the for loop is the only way I can do this.



Here's the code:



import java.io.*;
import java.util.*;

public class MadLibs {

public static void main(String args)
throws FileNotFoundException {
intro();
madLib();


}

public static void intro() {
System.out.println("Welcome to the game of Mad Libs.");
System.out.println("I will ask you to provide various words");
System.out.println("and phrases to fill in a story.");
System.out.println("The result will be written to an output file.");
System.out.println();
}


public static void madLib() throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String r = input.next();
while (!(r.equalsIgnoreCase("c") || r.equalsIgnoreCase("v")
|| r.equalsIgnoreCase("q"))) {
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
}
if (r.equalsIgnoreCase("v")) {
viewFile();
}
else if (r.equalsIgnoreCase("c")) {
createWord();
}
}


public static void createWord() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
String input2 = input1;
PrintStream output = new PrintStream(new File(toRead));
while (input1.hasNext()) {
String input = input1.next();
for (int i = 0; i < input2.length(); i++) {
if (input.startsWith("<") && input.endsWith(">")) {
String token = input.substring(1, input.length() - 1);
System.out.println("Please input a: " + token);
Scanner scan = new Scanner(System.in);
String replacement = scan.nextLine();
token = token.replace(token, replacement);
output.print(token);
}
}
}
}


public static void viewFile() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
System.out.println();
while (input1.hasNextLine()) {
System.out.println(input1.nextLine());
}
}
}


Any help is greatly appreciated.










share|improve this question




















  • 1




    The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
    – Scary Wombat
    Nov 13 '18 at 1:08










  • Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
    – Scary Wombat
    Nov 13 '18 at 1:09






  • 1




    does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
    – Deadpool
    Nov 13 '18 at 1:18
















0












0








0







I am working on a MabLibs project that is supposed to iterate through a file, prompt you with anything contained in a <> block and allow you to write over to a new file.



I cannot figure out how to use a Scanner to read a file and turn that into a string so I can use the .length() method to iterate a for loop through the file to find these <> blocks.



I can only use Scanner and can't use array lists, so unfortunately the for loop is the only way I can do this.



Here's the code:



import java.io.*;
import java.util.*;

public class MadLibs {

public static void main(String args)
throws FileNotFoundException {
intro();
madLib();


}

public static void intro() {
System.out.println("Welcome to the game of Mad Libs.");
System.out.println("I will ask you to provide various words");
System.out.println("and phrases to fill in a story.");
System.out.println("The result will be written to an output file.");
System.out.println();
}


public static void madLib() throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String r = input.next();
while (!(r.equalsIgnoreCase("c") || r.equalsIgnoreCase("v")
|| r.equalsIgnoreCase("q"))) {
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
}
if (r.equalsIgnoreCase("v")) {
viewFile();
}
else if (r.equalsIgnoreCase("c")) {
createWord();
}
}


public static void createWord() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
String input2 = input1;
PrintStream output = new PrintStream(new File(toRead));
while (input1.hasNext()) {
String input = input1.next();
for (int i = 0; i < input2.length(); i++) {
if (input.startsWith("<") && input.endsWith(">")) {
String token = input.substring(1, input.length() - 1);
System.out.println("Please input a: " + token);
Scanner scan = new Scanner(System.in);
String replacement = scan.nextLine();
token = token.replace(token, replacement);
output.print(token);
}
}
}
}


public static void viewFile() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
System.out.println();
while (input1.hasNextLine()) {
System.out.println(input1.nextLine());
}
}
}


Any help is greatly appreciated.










share|improve this question















I am working on a MabLibs project that is supposed to iterate through a file, prompt you with anything contained in a <> block and allow you to write over to a new file.



I cannot figure out how to use a Scanner to read a file and turn that into a string so I can use the .length() method to iterate a for loop through the file to find these <> blocks.



I can only use Scanner and can't use array lists, so unfortunately the for loop is the only way I can do this.



Here's the code:



import java.io.*;
import java.util.*;

public class MadLibs {

public static void main(String args)
throws FileNotFoundException {
intro();
madLib();


}

public static void intro() {
System.out.println("Welcome to the game of Mad Libs.");
System.out.println("I will ask you to provide various words");
System.out.println("and phrases to fill in a story.");
System.out.println("The result will be written to an output file.");
System.out.println();
}


public static void madLib() throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String r = input.next();
while (!(r.equalsIgnoreCase("c") || r.equalsIgnoreCase("v")
|| r.equalsIgnoreCase("q"))) {
System.out.println("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
}
if (r.equalsIgnoreCase("v")) {
viewFile();
}
else if (r.equalsIgnoreCase("c")) {
createWord();
}
}


public static void createWord() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
String input2 = input1;
PrintStream output = new PrintStream(new File(toRead));
while (input1.hasNext()) {
String input = input1.next();
for (int i = 0; i < input2.length(); i++) {
if (input.startsWith("<") && input.endsWith(">")) {
String token = input.substring(1, input.length() - 1);
System.out.println("Please input a: " + token);
Scanner scan = new Scanner(System.in);
String replacement = scan.nextLine();
token = token.replace(token, replacement);
output.print(token);
}
}
}
}


public static void viewFile() throws FileNotFoundException {
System.out.println("Input file name: ");
Scanner viewFile = new Scanner(System.in);
String toRead = viewFile.nextLine();
File f = new File(toRead);
while (!f.exists()) {
System.out.println("File Not Found. Try again: ");
toRead = viewFile.nextLine();
f = new File(toRead);
}
Scanner input1 = new Scanner(new File(toRead));
System.out.println();
while (input1.hasNextLine()) {
System.out.println(input1.nextLine());
}
}
}


Any help is greatly appreciated.







java string loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 1:10









Deadpool

4,4692326




4,4692326










asked Nov 13 '18 at 1:03









N.SpiessN.Spiess

212




212








  • 1




    The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
    – Scary Wombat
    Nov 13 '18 at 1:08










  • Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
    – Scary Wombat
    Nov 13 '18 at 1:09






  • 1




    does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
    – Deadpool
    Nov 13 '18 at 1:18
















  • 1




    The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
    – Scary Wombat
    Nov 13 '18 at 1:08










  • Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
    – Scary Wombat
    Nov 13 '18 at 1:09






  • 1




    does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
    – Deadpool
    Nov 13 '18 at 1:18










1




1




The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
– Scary Wombat
Nov 13 '18 at 1:08




The standard way to to open fileA loop read line change line write to fileB end loop rename fileB to fileA
– Scary Wombat
Nov 13 '18 at 1:08












Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
– Scary Wombat
Nov 13 '18 at 1:09




Maybe the whole line is not quoted with <...> so use some regex to replace <....> with your new content
– Scary Wombat
Nov 13 '18 at 1:09




1




1




does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
– Deadpool
Nov 13 '18 at 1:18






does this compile first, i believe you should get compilation error Scanner input1 = new Scanner(new File(toRead)); String input2 = input1;
– Deadpool
Nov 13 '18 at 1:18














0






active

oldest

votes











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%2f53272285%2fconverting-a-scanner-file-into-a-string-that-contains-the-entire-file-output-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53272285%2fconverting-a-scanner-file-into-a-string-that-contains-the-entire-file-output-in%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







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong