我尝试创建程序将允许用户导入他们通过文本创建的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,它根本没有改变程序,所以我想知道是否有任何理由甚至使用它。
答案 0 :(得分:0)
我发现的第一个问题是StringBuffer不接受参数。我发现的第二个问题是方法取自String x而不是StringBuffer buffer1。
我实际上是通过调用buffer1的StringBuffer替换方法,放入第一个索引,最后一个索引+1 in为int参数,然后是newWord替换String来解决这个问题。这是我提出的代码,它有效......
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);
}