gpt4 book ai didi

java - 在Java中逐行读取和替换特定单词的问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:56 24 4
gpt4 key购买 nike

我尝试读取的文本文件的一个示例是tarzan.txt
enter image description here

我的程序实质上允许用户玩Mad-Lib游戏,在该游戏中,他将用所请求的描述(形容词,复数名词等)替换文本文件中的占位符。

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

public class MadLibs {
public static void main(String[] args) throws FileNotFoundException {
intro();
System.out.println();
Scanner console = new Scanner(System.in);

boolean continueGame = true;

while (continueGame == true) {
continueGame = gameMenu(console);
}
}

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.");
}

public static boolean gameMenu(Scanner console) throws FileNotFoundException {
System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
String userChoice = console.next();

if (userChoice.equalsIgnoreCase("c")) {
createMadLib(console);
return true;
} else if (userChoice.equalsIgnoreCase("v")) {
viewMadLib(console);
return true;
} else if (userChoice.equalsIgnoreCase("q")) {
return false;
} else {
return true; //keep continuing even if user input is irrelevant
}
}

public static void createMadLib(Scanner console) throws FileNotFoundException {
System.out.print("Input file name: ");
String fileName = console.next();
File textFile = new File(fileName);

while (!textFile.exists()) {
System.out.print("File not found. Try again: ");
fileName = console.next();
textFile = new File(fileName);
}
System.out.print("Output file name: ");
String output = console.next();
PrintStream outputFile = new PrintStream(output);

Scanner fileRead = new Scanner(textFile);

while (fileRead.hasNextLine()) {
String word = fileRead.next();

if (word.startsWith("<") && word.endsWith(">")) {
char vowel = word.charAt(1);

String beforeVowel = "";
if (vowel == 'a' || vowel == 'A' ||
vowel == 'e' || vowel == 'E' ||
vowel == 'i' || vowel == 'I' ||
vowel == 'o' || vowel == 'O' ||
vowel == 'u' || vowel == 'U') {
beforeVowel = " an";
} else {
beforeVowel = " a";
}
word = word.replace("<", " ");
word = word.replace(">", " ");
word = word.replace("-", " ");
System.out.print("Please type" + beforeVowel + word + ": ");
String inputWord = console.next();
outputFile.print(" " + inputWord + " ");
} else {
outputFile.print(" " + word + " ");
}
}
System.out.println("Your mad-lib has been created!");
}


我目前遇到的问题是文本文件中有多个占位符的行,例如第五行。当程序读取到该特定行时,它提示用户同时键入复数名词和名词,而不是分别要求用户输入单词

1

这是程序问题的当前输出,我的预期输出是通过这种方式提示用户:


  请输入一个复数名词:bees


一旦用户输入了一个单词,程序便会在文本文件的同一行分别询问一个名词


  请输入一个复数名词:bees
  
  请输入一个名词:

最佳答案

console.next()替换为console.nextLine()

使用next()将仅返回定界符之前的内容(默认为空白)。返回当前行后,nextLine()会自动将扫描仪向下移动。

关于java - 在Java中逐行读取和替换特定单词的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58851205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com