我在线程" main"中得到了Exception。运行代码

时间:2016-06-03 09:15:45

标签: java arrays object methods ioexception

  

线程中的异常" main" java.util.InputMismatchException       在java.util.Scanner.throwFor(未知来源)       在java.util.Scanner.next(未知来源)       在java.util.Scanner.nextInt(未知来源)       在java.util.Scanner.nextInt(未知来源)       在PeriodicTable.elementsInt(PeriodicTable.java:54)       在PeriodicTable.findElement(PeriodicTable.java:107)       在PeriodicTable.main(PeriodicTable.java:82)

是运行此代码时出现的错误。谁能告诉我哪里出错了?有关更多信息,我试图创建一个从周期表中提取的代码,并根据您的原子或缩写为您提供元素信息。

到目前为止这是代码:

public class PeriodicTable {

    static class PeriodicElement{
        int[] atomicNumber = new int[200];
        double[] atomicMass = new double[200];
        String[][] abbreviation = new String[200][200];
        String[] theTable = new String[200];

        public String[] toString(String[] arr){
            String[] s = Arrays.toString(arr).split(" ");
            return s;
        }

        public PeriodicElement(String[] pElement) {
            toString(pElement);

        }
    }
    public PeriodicTable(String[] theTable) throws IOException{
        Scanner inputFile = new Scanner(new File("/Users/eddie/workspace/PeriodicTable/src/table.txt"));
        while (inputFile.hasNextLine()){
            int i = 0;
            theTable[i] = inputFile.nextLine();
            i++;
        }
        inputFile.close();// close the file when done
    }

    public static String[] readTable(String[] table)throws IOException{
        PeriodicTable inputFile = new PeriodicTable(table);
        return table;
    }

    public static int elementsInt(int found)throws IOException{
        Scanner inputFile = new Scanner(new File("/Users/eddie/workspace/PeriodicTable/src/table.txt"));
        while (inputFile.hasNextLine()){
            int[] table = new int[200];
            int i = 0;
            table[i] = inputFile.nextInt();
            if (found == table[i]){
                System.out.println("found your number!");
                return table[i];
            }
            else
                i++;
        }
        inputFile.close();// close the file when done.
        return found;
    }

    public static void main(String[] args)throws IOException { // Main Method
        final int NUMBER_ELEMENTS = 0;
        Scanner keyboard = new Scanner(System.in);
        String yourName = "your Name";
        System.out.println("Periodic Table by " + yourName);
        System.out.println(" ");
        System.out.println("Number of elements: " + getNumberOfElements(NUMBER_ELEMENTS));
        System.out.println("1. Search atomic number ");
        System.out.println("2. Search abbreviation  ");
        System.out.println("3. Print table ");
        System.out.println("4. Exit ");
        int choice = keyboard.nextInt();
        switch (choice) {
            case 1: System.out.print("Enter an atomic number: ");
                int aNumber = keyboard.nextInt();
                findElement(aNumber);
                System.out.println("your atomic number is: " + findElement(aNumber) );
                break;

            case 2: System.out.print("Enter an abbreviation");
                String abbreviation = keyboard.next();
                break;

            case 3: String[] everything = new String[200];
                PeriodicElement print = new PeriodicElement(printTable(everything));
                for(int i=0; i<everything.length ;i++){
                    System.out.println(print);
                }
                break;

            case 4: break;
        }
    }

    public static int getNumberOfElements(int num){
        return num = 118;
    }

    public static int findElement(int e1)throws IOException {
        return elementsInt(e1);
    }

    public static String[] printTable(String[] display)throws IOException{
        PeriodicElement printAll = new PeriodicElement(printTable(display));
        for(int i=0; i<display.length ;i++){
            System.out.println(printAll);
        }
        return display;
    }
}

2 个答案:

答案 0 :(得分:1)

似乎你的table.txt文件不仅包含数字,而且还包含inputFile.nextInt()抛出此异常的原因。 来自JavaDoc:

  

由扫描程序抛出,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

答案 1 :(得分:0)

当读取字符不是它应该找到的字符(在这种情况下是整数)或者找到的标记超出范围时,扫描程序抛出此异常。

由于最大的预测原子数是172(如果我错了,请纠正我),它完全在...范围内...

因此,很可能异常是因为你的table.txt文件中有除整数以外的东西。也许是一个字符串,一个字符,“。”等

为什么不把它放在try块中并运行计数器来查找发生此错误的行号并检查您的文件。