-3

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at PeriodicTable.elementsInt(PeriodicTable.java:54) at PeriodicTable.findElement(PeriodicTable.java:107) at PeriodicTable.main(PeriodicTable.java:82)

is the error i get when running this code. can anyone tell me where i went wrong? for further information, i'm trying to create a code where it pulls from the periodic table and gives you the element information according to your atomic or abbreviation.

this is the code so far:

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;
    }
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122

2 Answers2

1

Seems that your table.txt file contains not only numbers, that's why inputFile.nextInt() throws this exception. From JavaDoc:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

ninja
  • 2,908
  • 4
  • 23
  • 30
0

This Exception is thrown by a Scanner when either the read character isn't what its supposed to find(in this case an integer) or the found token is out of range.

Since the largest predicted Atomic number is 172(correct me if i am wrong), it is well within the range...

Thus, most probably the exception is because you have something other than integers in your table.txt file.Maybe a string, a character, "." etc.

why don't you put it in a try block and run a counter to find the line number where this error happens and check your file.

Kushan
  • 5,855
  • 3
  • 31
  • 45