I have an assignment that requires me to write a program in JAVA with 10 different methods including the main. It obtains the input from a file in order to extract data from a second file through the various methods. Lastly it prints the results to a third file. THis is an intro class and we were insturcted to use the hasNext method. THe second file where the data is retrieved from has 10 rows and 5 columns, each column representing something different. I used sc1.nextInt() since our professor warned us that the programs will read every piece of data and we havent learned how to extract data from just one column. I am stuck on an error I keep receiving. I have included a snippet of my code if anyone can help me. Thank you.
this is the error I keep receiving:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at homework4.HomeWork4.checkNumber(HomeWork4.java:47) at homework4.HomeWork4.main(HomeWork4.java:26) /Users/xiomarahenriquez/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)"
public static PrintStream ps;
public static void main(String[] args) throws Exception {
ps = new PrintStream("elementsResults.txt");
Scanner sc1 = new Scanner(new File("input.txt"));
int atomicNumber, valid=0, invalid=0, totalProcessed=0;
while (sc1.hasNext()) {
atomicNumber = sc1.nextInt();
checkNumber(atomicNumber);
if(checkNumber(atomicNumber)== true){
++valid;
} else {
++invalid;
}
++totalProcessed;
}
}
public static boolean checkNumber (int atomicNumber) throws Exception {
Scanner sc2 = new Scanner (new File("PeriodicTable.txt"));
int columnA = sc2.nextInt();
String columnB;
int columnC,columnD,columnE;
while (sc2.hasNext() && (columnA > -1 || columnA < 118)) {
columnA=sc2.nextInt();
columnB=sc2.next();
columnC=sc2.nextInt();
columnD=sc2.nextInt();
columnE=sc2.nextInt();
if (atomicNumber==columnA) {
return true;
}
}
sc2.close();
return false;
}