简体   繁体   中英

JAVA - logic error in main method

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

This is my first answer. Hope it helps. In your while loop you are running the checkNumber method twice. That's unnecessary. Do it just once like below. Also there is a slight difference between ++i and i++ so check this link: what is the difference between i++ & ++i in for loop (Java)?

while (sc1.hasNext()) {
    atomicNumber = sc1.nextInt();

    if(checkNumber(atomicNumber)== true){
        valid++;
    } else {
        invalid++;
    }
    totalProcessed++;
}

I think that the cause of your problem is in the first line of your exception stack trace:

Exception in thread "main" java.util.InputMismatchException

Here's a link to the documentation for the InputMismatchException . I can't say for sure since I don't know what your input files look like but I'm pretty sure that when you're calling nextInt() , the next token read isn't something that can be cast to an int . My guess is that the Scanner is encountering some text or something else preventing it from returning an int . To figure out which token is causing the problem, I'd try wrapping your invocations of nextInt() in try/catch blocks. When the Scanner throws an InputMismatchException , it will not pass the token that caused the exception so that after the exception is thrown you can get the value of the token (like with the next() method) or skip the token altogether. Here's an example (I don't have access to an IDE right now so this isn't tested but hopefully you can get the idea):

//Some initialization code here...
Scanner myScanner = new Scanner(new File("myFile.txt"));
while(myScanner.hasNext()) {
    try {
        int myIntVariable = myScanner.nextInt();
    } catch (InputMismatchException ex) {
        System.out.println("Here's the token that caused the problem: " + myScanner.next());
    }
}
//The rest of your code here...

By the way, if you're not absolutely sure that the token that you're retrieving is the type that you think it's going to be (in your case an int ), it's probably a good idea to wrap that portion of the code in a try/catch block so that you can handle cases where the token isn't what you think it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM