Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中创建mad lib游戏并接收NoTouchElementException方法_Java - Fatal编程技术网

在java中创建mad lib游戏并接收NoTouchElementException方法

在java中创建mad lib游戏并接收NoTouchElementException方法,java,Java,我正在尝试创建一个java程序,它接收一个.txt文件并玩游戏,然后将其全部打印到一个新文件中(由用户命名)。我已经到了所有的单词都被选中的地步,但是在那之后我收到了一条NosTouchElementException消息。我对java有相当基础的知识,完全不知道如何进行。有人有什么建议吗 import java.io.*; import java.util.*; public class MadLibs { public static void main(String[] args) thr

我正在尝试创建一个java程序,它接收一个.txt文件并玩游戏,然后将其全部打印到一个新文件中(由用户命名)。我已经到了所有的单词都被选中的地步,但是在那之后我收到了一条NosTouchElementException消息。我对java有相当基础的知识,完全不知道如何进行。有人有什么建议吗

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

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

  intro();

  //in order to create the output file first prompts user to decide 
  //whether they want to create a mad-lib, view their mad-lib or quit
  //if 'c' is selected then while loop is exited
  String action = "c";
  String fileName = "fileName";
  while (action.equals("c")) {
     System.out.print("(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
     action = console.nextLine();
     action = action.toLowerCase();
     File file = new File(fileName);
     System.out.print("Input file name: ");
     while (!file.exists()) {
        fileName = console.nextLine();
        file = new File(fileName);
        if (!file.exists()) {
           System.out.print("File not found. Try again: ");
        }
     }


     //asks for a file to read from for the mad-lib game
     //and creates file (named by user) to input the information
     System.out.print("Output file name: ");
     String outputName = console.nextLine();
     System.out.println();
     File outputFile = new File(outputName);
     PrintStream output = new PrintStream(outputFile);

     Scanner tokens = new Scanner(file);
     while (tokens.hasNext()) {
        String token = tokens.next();

        //calls the returned placeHolder
        String placeHolder = placeHolder(console, tokens, token);
        String newWord = madLib(console, token, placeHolder);


        //copies each token and pastes into new output file


     }
  }

  while (action.equals("v")) {       
     System.out.print("Input file name: ");
     fileName = console.nextLine();
     File outputFile = new File(fileName);
     if (!outputFile.exists()) { 
        System.out.print("File not found. Try again: ");
        fileName = console.nextLine();
     } else {
        PrintStream output = new PrintStream(outputFile);
        output = System.out;
     }
  }

  while (action.equals("q")) {
  } 

}

public static String madLib(Scanner console, String token, String        
placeHolder) throws FileNotFoundException{
  String word = placeHolder.replace("<", "").replace(">", ": ").replace("-",  
" ");
  String startsWith = String.valueOf(word.charAt(0));
  if (startsWith.equalsIgnoreCase("a") || startsWith.equalsIgnoreCase("e") 
 || 
      startsWith.equalsIgnoreCase("i") || startsWith.equalsIgnoreCase("o") 
 ||
      startsWith.equalsIgnoreCase("u")) {
     String article = "an ";
     System.out.print("Please type " + article + word);
     String newWord = console.next();
     return newWord;
  } else {
     String article = "a ";
     System.out.print("Please type " + article + word);
     String newWord = console.next();
     return newWord;
  }
}

public static String placeHolder(Scanner console, Scanner tokens, String  
 token) throws FileNotFoundException {
   while(!(token.startsWith("<") && token.endsWith(">"))) { 
      //not a placeholder! 
      //continue reading file
      token = tokens.next();
   }
   //outside of this while loop = found a placeholder!!
   String placeHolder = token;
   //returns placeholder to main 
   return placeHolder;
}



//method prints out the introduction to the game
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();
 }
}

我运行了你的代码,得到了一个
NoTouchElementException
而不是
NoSuchFileException
。要避免此异常,您需要检查在方法
占位符中是否还有其他令牌。否则,在输入每个占位符后,尽管没有
next()
,您仍会搜索下一个占位符标记

将代码更改为:

while(tokens.hasNext() && !(token.startsWith("<") && token.endsWith(">"))) { 
     //not a placeholder! 
     //continue reading file
     System.out.println(token);
     token = tokens.next();
}
while(tokens.hasNext()&&!(token.startsWith(“”)){
//不是占位符!
//继续读取文件
System.out.println(令牌);
token=tokens.next();
}

请发布整个异常输出,包括完整堆栈跟踪。哎哟,忘了这一点。。。
while(tokens.hasNext() && !(token.startsWith("<") && token.endsWith(">"))) { 
     //not a placeholder! 
     //continue reading file
     System.out.println(token);
     token = tokens.next();
}