私は現在、単純なファイルからテキストを取り込んで別のファイルに書き込む小さなプログラムを書いています。入力ファイルは、「madlibs」のような取引のようなもので、「」や「」などのトークンを配置する必要があります。
文章:
> One of the most <adjective> characters in fiction is named
"Tarzan of the <plural-noun>." Tarzan was raised by a/an <noun>
and lives in the <adjective> jungle in the heart of darkest
<place>.
コード:
import java.io.*;
import java.util.*;
public class Story {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.print("Input file name? ");
String infileName = sc.next();
Scanner input = new Scanner(new File(infileName));
System.out.print("Output file name? ");
String outfileName = sc.next();
PrintStream out = new PrintStream(new File(outfileName));
while (input.hasNext()){
String current = input.next();
System.out.println(current);
int openIndex = current.indexOf("<");
int closeIndex = current.indexOf(">");
current = current.substring(openIndex + 1, closeIndex - openIndex);
System.out.println(current);
if (current.equals("adjective")){
System.out.print("Please enter an adjective: ");
current = sc.next();
out.print(current);
out.print(" ");
} else if (current.equals("plural-noun")){
System.out.print("Please enter a plural noun: ");
current = sc.next();
out.print(current);
out.print(" ");
} else if (current.equals("noun")){
System.out.print("Please enter a noun: ");
current = sc.next();
out.print(current);
out.print(" ");
} else if (current.equals("place")){
System.out.print("Please enter a place: ");
current = sc.next();
out.print(current);
out.print(" ");
} else {
out.print(current);
out.print(" ");
}
}
}
}
これが私が抱えている問題のようです。ユーザーが入力した単語だけが「out1.txt」などに印刷されており、各単語の間にいくつかのスペースがあります。
どんな助けでもありがたいです、みんなと女の子に感謝します!