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;
}
}