为什么我的 MadLib txt 文件程序在右括号“]”之后没有在每个生命上打印任何内容
Why is my MadLib txt file program not printing anything on each life after the right bracket "]"
我正在尝试创建一个程序,允许用户导入他们通过文本创建的 MadLib,导入文件,并更改他们输入的括号中的单词。
这个程序所做的是用替换词更改 txt 文件的输出。但是,被替换单词之后的所有内容都会从每一行的其余部分消失。
我将添加我的代码并包括对我所做的评论,这样您就可以看到问题可能发生在何处。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class BlanksStory {
ArrayList<String> story1 =new ArrayList<>();
public BlanksStory()
{
File file=null;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
String fileName= null;
try {
fileName= file.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Your file could not be read.");
}
FileReader file1;
try {
String line;
file1 = new FileReader(file);
BufferedReader bRead= new BufferedReader(file1);
while((line=bRead.readLine())!=null){
story1.add(line); //Adds text file contents to the ArrayList
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File Not Found: Please choose another file.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
ArrayList<String> edited= new ArrayList<>();
Scanner input= new Scanner(System.in);
for(String x: story1){ //Reformats ArrayList to delete the brackets and
//commas, while putting each element on a new line to appear the same as
//in txt file.
StringBuffer buffer1= new StringBuffer();
int startIndex=0;
String z;
for(int i=0; i<x.length();i++){
if(x.charAt(i)=='['){
int firstChar=i;
while(x.charAt(i)!=']')
i++;
int lastIndex=i;
String word=x.substring(firstChar+1, lastIndex);
String replaceWord= x.substring(firstChar, lastIndex+1);
char firstLetter= word.charAt(0);
if(firstLetter=='a'|| firstLetter== 'e'|| firstLetter=='i'|| firstLetter=='o'||firstLetter=='u'){
System.out.println("Enter an "+word+": ");
}
else{
System.out.println("Enter a "+word+": ");
} //Determines if vowel or consonant to choose a or an.
//replaces the word with a new word entered by user
String newWord= input.next();
z= x.replace(replaceWord, newWord);
buffer1.append(z.substring(startIndex, z.indexOf(newWord)+newWord.length()));
startIndex=z.indexOf(newWord)+replaceWord.length()+1;
}
}
edited.add(buffer1.toString());
}
for(String x:edited){
System.out.println(x);
}
}
public static void main(String [] args){
BlanksStory madLib= new BlanksStory();
madLib.play();
}
}
我用来测试此程序的 txt 文件包含以下文本...
This is a [adjective] practice test.
This test will [verb] if it works.
当询问形容词和动词时,它按预期工作。当我输入形容词 simple 和动词 运行 时,这就是我得到的输出....
This is a simple
This test will run
应该读....
This is a simple test.
The test will run if it works.
如您所见,它会切断右括号后的所有内容。我最初认为也许最后一个索引设置不正确,但是当我告诉用户选择该类型的单词时,它说整个单词都有正确的索引并且每行没有额外的索引,所以它确实结束了括号中的替换词不会延续到句子的其余部分,并用替换词将其删除。
我能想到的唯一另一件事是开始索引 运行 循环再次从单词的结尾开始,所以它没有缓冲文本的其余部分。但是,我尝试在循环结束时在 story1 元素的最后一个索引处启动索引,但我没有运气,因为它给出了相同的输出。
我一直在那里尝试一些不同的东西,但它根本没有改变代码。我在循环的最后一行一起注释掉了 startIndex,它根本没有改变程序,所以我想知道是否有任何理由甚至使用它。
我发现的第一个问题是 StringBuffer 不接受参数。我发现的第二个问题是这些方法是从字符串 x 而不是从 StringBuffer buffer1 中获取的。
我实际上通过为 buffer1 调用 StringBuffer 替换方法解决了这个问题,将第一个索引和最后一个索引 +1 放入 int 参数,然后将 newWord 放入替换字符串。这是我想出的代码并且有效...
public void play(){
ArrayList<String> edited= new ArrayList<>();
Scanner input= new Scanner(System.in);
for(String x: story1){
// ^Takes the ArrayList of the story and reformats into a new string.
// This takes the ArrayList story1, and removes the commas and brackets in between lines.
StringBuffer buffer1= new StringBuffer(x);
for(int i=0; i<buffer1.length();i++){
if(buffer1.charAt(i)=='['){
int firstChar=i;
while(buffer1.charAt(i)!=']')
//^Searches for characters in the string that match brackets. The loop is supposed to stop at the last bracket, replace the word, and then continue.
i++;
int lastIndex=i;
String word=buffer1.substring(firstChar+1, lastIndex);
char firstLetter= word.charAt(0);
/**String word is not used for replacement, but simply to read the word inside the brackets[].
* If word in brackets begins with a vowel, such as adjective, the prompt is...
* Enter an adjective. If the word begins with a consonant, such as verb, the prompt is...
* Enter a verb.
* The word entered by the user is put through the buffer, and it replaces the word inside the brackets[].
*/
if(firstLetter=='a'|| firstLetter== 'e'|| firstLetter=='i'|| firstLetter=='o'||firstLetter=='u'){
System.out.println("Enter an "+word+": ");
}
else{
System.out.println("Enter a "+word+": ");
}
String newWord= input.nextLine();
buffer1.replace(firstChar, lastIndex+1, newWord);
}
}
//buffer1 is formatted into a toString(), and put into an ArrayList edited.
edited.add(buffer1.toString());
}
//ArrayList edited it put into a String format, called x, and then printed.
for(String x:edited){
System.out.println(x);
}
我正在尝试创建一个程序,允许用户导入他们通过文本创建的 MadLib,导入文件,并更改他们输入的括号中的单词。
这个程序所做的是用替换词更改 txt 文件的输出。但是,被替换单词之后的所有内容都会从每一行的其余部分消失。
我将添加我的代码并包括对我所做的评论,这样您就可以看到问题可能发生在何处。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class BlanksStory {
ArrayList<String> story1 =new ArrayList<>();
public BlanksStory()
{
File file=null;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
String fileName= null;
try {
fileName= file.getCanonicalPath();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Your file could not be read.");
}
FileReader file1;
try {
String line;
file1 = new FileReader(file);
BufferedReader bRead= new BufferedReader(file1);
while((line=bRead.readLine())!=null){
story1.add(line); //Adds text file contents to the ArrayList
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File Not Found: Please choose another file.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
ArrayList<String> edited= new ArrayList<>();
Scanner input= new Scanner(System.in);
for(String x: story1){ //Reformats ArrayList to delete the brackets and
//commas, while putting each element on a new line to appear the same as
//in txt file.
StringBuffer buffer1= new StringBuffer();
int startIndex=0;
String z;
for(int i=0; i<x.length();i++){
if(x.charAt(i)=='['){
int firstChar=i;
while(x.charAt(i)!=']')
i++;
int lastIndex=i;
String word=x.substring(firstChar+1, lastIndex);
String replaceWord= x.substring(firstChar, lastIndex+1);
char firstLetter= word.charAt(0);
if(firstLetter=='a'|| firstLetter== 'e'|| firstLetter=='i'|| firstLetter=='o'||firstLetter=='u'){
System.out.println("Enter an "+word+": ");
}
else{
System.out.println("Enter a "+word+": ");
} //Determines if vowel or consonant to choose a or an.
//replaces the word with a new word entered by user
String newWord= input.next();
z= x.replace(replaceWord, newWord);
buffer1.append(z.substring(startIndex, z.indexOf(newWord)+newWord.length()));
startIndex=z.indexOf(newWord)+replaceWord.length()+1;
}
}
edited.add(buffer1.toString());
}
for(String x:edited){
System.out.println(x);
}
}
public static void main(String [] args){
BlanksStory madLib= new BlanksStory();
madLib.play();
}
}
我用来测试此程序的 txt 文件包含以下文本...
This is a [adjective] practice test.
This test will [verb] if it works.
当询问形容词和动词时,它按预期工作。当我输入形容词 simple 和动词 运行 时,这就是我得到的输出....
This is a simple
This test will run
应该读....
This is a simple test.
The test will run if it works.
如您所见,它会切断右括号后的所有内容。我最初认为也许最后一个索引设置不正确,但是当我告诉用户选择该类型的单词时,它说整个单词都有正确的索引并且每行没有额外的索引,所以它确实结束了括号中的替换词不会延续到句子的其余部分,并用替换词将其删除。
我能想到的唯一另一件事是开始索引 运行 循环再次从单词的结尾开始,所以它没有缓冲文本的其余部分。但是,我尝试在循环结束时在 story1 元素的最后一个索引处启动索引,但我没有运气,因为它给出了相同的输出。
我一直在那里尝试一些不同的东西,但它根本没有改变代码。我在循环的最后一行一起注释掉了 startIndex,它根本没有改变程序,所以我想知道是否有任何理由甚至使用它。
我发现的第一个问题是 StringBuffer 不接受参数。我发现的第二个问题是这些方法是从字符串 x 而不是从 StringBuffer buffer1 中获取的。
我实际上通过为 buffer1 调用 StringBuffer 替换方法解决了这个问题,将第一个索引和最后一个索引 +1 放入 int 参数,然后将 newWord 放入替换字符串。这是我想出的代码并且有效...
public void play(){
ArrayList<String> edited= new ArrayList<>();
Scanner input= new Scanner(System.in);
for(String x: story1){
// ^Takes the ArrayList of the story and reformats into a new string.
// This takes the ArrayList story1, and removes the commas and brackets in between lines.
StringBuffer buffer1= new StringBuffer(x);
for(int i=0; i<buffer1.length();i++){
if(buffer1.charAt(i)=='['){
int firstChar=i;
while(buffer1.charAt(i)!=']')
//^Searches for characters in the string that match brackets. The loop is supposed to stop at the last bracket, replace the word, and then continue.
i++;
int lastIndex=i;
String word=buffer1.substring(firstChar+1, lastIndex);
char firstLetter= word.charAt(0);
/**String word is not used for replacement, but simply to read the word inside the brackets[].
* If word in brackets begins with a vowel, such as adjective, the prompt is...
* Enter an adjective. If the word begins with a consonant, such as verb, the prompt is...
* Enter a verb.
* The word entered by the user is put through the buffer, and it replaces the word inside the brackets[].
*/
if(firstLetter=='a'|| firstLetter== 'e'|| firstLetter=='i'|| firstLetter=='o'||firstLetter=='u'){
System.out.println("Enter an "+word+": ");
}
else{
System.out.println("Enter a "+word+": ");
}
String newWord= input.nextLine();
buffer1.replace(firstChar, lastIndex+1, newWord);
}
}
//buffer1 is formatted into a toString(), and put into an ArrayList edited.
edited.add(buffer1.toString());
}
//ArrayList edited it put into a String format, called x, and then printed.
for(String x:edited){
System.out.println(x);
}