返回

如何克服java程序中的“NoTouchElementException”

发布时间:2022-03-18 13:53:22 475
# 移动端
import java.io.*;
import java.util.*;

public class JTMadLibs {

    public static void main(String[] args)throws FileNotFoundException {
        // TODO Auto-generated method stub
        boolean playGame = true;
        Scanner console = new Scanner(System.in);
        

        System.out.println("Welcome to the game of MadLibs.");
        System.out.println("I will ask you to provide various words ");
        System.out.println("and phrases to fill a story.");
        System.out.println("The result will be written to an output file.");
        
        while(playGame == true){
            playGame = menu(console);       
        }
        
        System.out.println("Thanks for playing!");
        
    }

    public static boolean menu(Scanner in) throws FileNotFoundException{
        
        System.out.print("\n(C)reate mad-lib, (V)iew mad-lib, (Q)uit? ");
        String answer = in.nextLine();
        if(answer.charAt(0) == 'c' || answer.charAt(0) == 'C'){
            create(in);
            return true;
        }
        if(answer.charAt(0) == 'v' || answer.charAt(0) == 'V' ){
            view(in);
            return true;
        }
        if(answer.charAt(0) == 'q' || answer.charAt(0) == 'Q'){
            return false;
        }
        else{
            System.out.println("Invalid answer. Please try again.");
             return true;
        }
    }
    public static void create(Scanner console)throws FileNotFoundException{
        
        System.out.print("Input file name: ");
        String nameIn = console.nextLine();
        File f1 = new File(nameIn);
        while(!f1.exists()){
            System.out.print("File not found. Try again: ");
            nameIn = console.nextLine();
            //f1 = new File(nameIn);
        }
        System.out.print("Output file name: ");
        String nameOut = console.nextLine();
        File out = new File(nameOut);
        PrintStream output = new PrintStream(out);
        
        Scanner input = new Scanner(f1);
        
        while(input.hasNextLine()){
            String text = input.next();
            
            //System.out.print(text);
            //processLine(console, f1, nameOut);
            if(text.startsWith("<") && text.endsWith(">")){
                //String x = input.next();
                char a = text.charAt(1);
                String anora = aOrAn(a);
                text = text.replace('<', ' ');
                text = text.replace('>', ' ');
                text = text.replace('-', ' ');
                System.out.println("Please type" + anora + text);
                String in = console.next();
                output.print(" " + in + " ");
            }
            else{
                output.print(" " + text + " ");
            }
        }
        
    }
    
    
    public static void view(Scanner console)throws FileNotFoundException{
        Scanner input = new Scanner(new File("clothes.txt"));
        while(input.hasNextLine()){
            String text = input.nextLine();
         
            
            //System.out.println(text);
            //processLine(text);
            
        }
        System.out.println();
      System.out.println("Your mad-lib has been created!");
    }
    /*public static void processLine(Scanner console, File inName, String outName)
    throws FileNotFoundException{
        Scanner madL = new Scanner(new File(inName.getPath()));
        //int length = madL.next().length();
        PrintStream output = new PrintStream(new File(outName));
        String tx = madL.next();
        System.out.println(tx);
        
        if(madL.nextLine().startsWith("<") && madL.nextLine().endsWith(">")){
            String x = madL.next();
            char a = x.charAt(1);
            String anora = aOrAn(a);
            x.replace('<', ' ');
            x.replace('>', ' ');
            x.replace('-', ' ');
            System.out.println("Please type" + anora + x);
            String in = console.nextLine();
            output.print(" " + in + " ");
            
        }
        else{
            String y = madL.next();
            output.print(" " + y + " ");
        }
        
    }*/
    
    public static String aOrAn(char check){
        String a;
        if(check == 'a' || check == 'A' || check == 'i' || check == 'I' 
            || check == 'i' || check == 'e' || check == 'E' ||
            check == 'o' || check == 'O' || check == 'u' || check == 'U'){
            a = " an";
        }
        else{
            a = " a";
        }
        return a;
    }
    
}
Console:

Welcome to the game of MadLibs.
I will ask you to provide various words 
and phrases to fill a story.
The result will be written to an output file.

(C)reate mad-lib, (V)iew mad-lib, (Q)uit? c
Input file name: simple.txt
Output file name: madlib1
Please type a job 
car salesman
Please type an adjective 
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at madLibs.JT.create(JTMadLibs.java:64)
    at madLibs.JTMadLibs.menu(JTMadLibs.java:31)
    at madLibs.JTMadLibs.main(JTMadLibs.java:19)

我正在为我的CS课做一个疯狂的库实验室,我不知道这里发生了什么。一旦;“用户”;输入mad lib文本提示后,它将显示上述错误,而不是打印“;你的疯狂自由已经被创造出来了&引用;作业中说要使用;“下一行”;而不是;“下一步”;出于所有控制台的目的,但当我更改它们时,它会给我带来更多错误。

非常感谢你们在这方面提供的任何帮助或建议。

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像