【问题标题】:i get Exception in thread "main" java.util.InputMismatchException when running code运行代码时,我在线程“main”java.util.InputMismatchException 中出现异常
【发布时间】:2016-06-03 09:15:45
【问题描述】:

线程“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;
    }
}

【问题讨论】:

    标签: java arrays object methods ioexception


    【解决方案1】:

    似乎您的 table.txt 文件不仅包含数字,这就是 inputFile.nextInt() 抛出此异常的原因。 来自 JavaDoc:

    由 Scanner 抛出以指示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

    【讨论】:

    • 谢谢!我很感激帮助。现在只是为了获取读取前三个字符 int 值的方法
    【解决方案2】:

    当读取的字符不是它应该找到的字符(在本例中为整数)或找到的标记超出范围时,扫描器会抛出此异常。

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

    因此,很可能例外是因为您的 table.txt 文件中存在整数以外的内容。可能是字符串、字符、“。”等等

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 2019-08-08
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多