为什么我得到这个例外?

时间:2012-03-10 14:46:00

标签: java exception

我正在运行我的程序,并尝试从该程序运行的程序启动“帮助”菜单(如果这有意义!)。但是我得到了一个“NoSuchElement”异常,在我的try {} catch()中甚至都没有在这两个程序中调用! 我正在做的是; 运行程序。键入“create”以启动commandCreate类。然后我输入“帮助”以启动帮助菜单。但我得到了NoSuchElement异常。如果有人能够帮助我,我的两个程序在下面。谢谢。

main.java

// main.java
import java.io.*;

public class Main extends API {
      boolean _active = true;
     String _username = System.getProperty("user.name").toLowerCase();
     String _os = System.getProperty("os.name").trim().toLowerCase();

    public Main() {
         try {
            while(_active) {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                print(_username + "@" + _os + ":~$ ");
                String command = br.readLine();
                    if(command.equalsIgnoreCase("create")) {
                        new commandCreate();
                    /*} else if(command.equals("compile")) {
                        new commandCompile();*/
                    } else if(command.equalsIgnoreCase("help")) {
                        println("Commands");
                        println(" create              - Creates .java files, does not compile.");
                        //println(" compile             - Creates .java files, compiles on creation.");
                        println(" exit                - Exits program");
                        println(" help                - Shows help documentation.");
                    } else if(command.equalsIgnoreCase("exit")) {
                        print("Are you sure you want to exit? (Y/N) ");
                        String exit = br.readLine();
                        if(exit.equalsIgnoreCase("y")) {
                        exit();
                        } else {
                        println("Cancelled!");
                        }
                    } else if(command.isEmpty()) {

                    } else {
                        println("\"" + command + "\" does not exist. Please review the \"help\" menu");
                    }
            }
        } catch(Exception ex) {
            println("There was a problem: " + ex);
            }
   }

    public static void main(String[] args) {
     new Main();
    }
}

commandCreate.java

// commandCreate.java
import java.util.*;
import java.io.*;

public class commandCreate {
    boolean _active = true;
   String _username = System.getProperty("user.name").toLowerCase();
   String _os = System.getProperty("os.name").trim().toLowerCase();
   String fileName, create, option;

    public commandCreate() {
        try {
            System.out.print(_username + "@" + _os + ":~/create$ ");
            Scanner kbd = new Scanner(System.in);
                String userLine = kbd.nextLine();

            Scanner read = new Scanner(userLine);
                option = read.next();
                fileName = read.next();

            FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java"));

            if(userLine.equals(option + " " + fileName)) {
                if(option.equals("-a")) {
                    // Option = -a, creates standard file with main class.
                    create.write("public class " + fileName + " {\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      System.out.println(\"Welcome to your new program!\");\n");
                    create.write("  }\n");
                    create.write("}");
                } else if(option.equals("-c")) {
                    // Option = -c , creates standard file with overloaded constructor & main class.
                    create.write("public class " + fileName + " {\n");
                    create.write("  public " + fileName + "() {\n");
                    create.write("      System.out.println(\"Welcome to your new program!\");\n");
                    create.write("  }\n");
                    create.write("\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      new " + fileName + "();\n");
                    create.write("  }\n");
                    create.write("}");
                } else if(option.equals("-j")) {
                    // Option = -j, creates GUI within constructor w/ single JLabel.
                    create.write("import javax.swing.*;\n");
                    create.write("import java.awt.*;\n");
                    create.write("import java.awt.event.*;\n");
                    create.write("\n");
                    create.write("public class " + fileName + " extends JFrame {\n");
                    create.write("  private static final int HEIGHT = 50;\n");
                    create.write("  private static final int WIDTH = 400;\n");
                    create.write("\n");
                    create.write("  private JLabel welcomeJ;\n");
                    create.write("\n");
                    create.write("  public " + fileName + "() {\n");
                    create.write("    super(\"Welcome to your program - " + fileName + "\");\n");
                    create.write("      Container pane = getContentPane();\n");
                    create.write("    setLayout(new FlowLayout());\n");
                    create.write("\n");
                    create.write("      welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n");
                    create.write("\n");
                    create.write("      pane.add(welcomeJ);\n");
                    create.write("\n");
                    create.write("     setSize(WIDTH, HEIGHT);\n");
                    create.write("     setVisible(true);\n");
                    create.write("     setResizable(false);\n");
                    create.write("     setDefaultCloseOperation(EXIT_ON_CLOSE);\n");
                    create.write("  }\n");
                    create.write("\n");
                    create.write("  public static void main(String[] args) {\n");
                    create.write("      new " + fileName + "();\n");
                    create.write("  }\n");
                    create.write("}");
                }
            } else if(userLine.equalsIgnoreCase("help")) {
                System.out.println("Commands");
                System.out.println("  Syntax: [-option] [filename]");
                System.out.println("      -a [filename]      [Program: main class]");
                System.out.println("      -c [filename]      [Program: overloaded constructor, main class]");
                System.out.println("      -j [filename]      [Program: GUI: overloaded constructor, main class]");
            } else {
                System.out.println("Error in syntax. Please review the \"help\" menu");
            }
            create.close();
        } catch(IOException e) {
            System.out.println("There was an error: " + e);
        } catch(InputMismatchException ex) {
            System.out.println("There was an error: " + ex);
        }
    }

    public static void main(String[] args) {
        new commandCreate();
    }
}

2 个答案:

答案 0 :(得分:2)

根据您的堆栈跟踪,问题在于:

            Scanner kbd = new Scanner(System.in);
                String userLine = kbd.nextLine();

            Scanner read = new Scanner(userLine);
                option = read.next();
                fileName = read.next();             // <--- exception here

这段代码的作用是:

  • 它从标准输入中读取一行,并将其保存在userLine
  • 它从userLine读取两个以空格分隔的标记,并将其保存为optionfilename

所以问题是来自标准输入的行实际上没有两个以空格分隔的标记。它需要看起来像-j file.txt,但可能只是看起来像-j,或者像file.txt

答案 1 :(得分:1)

您可能在名为API的超类中遇到问题。看那里。