简体   繁体   English

JAVA-主要方法中的逻辑错误

[英]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. 我的作业要求我用JAVA用10种不同的方法(包括主要方法)编写程序。 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. 这是一个入门课程,我们被告知要使用hasNext方法。 THe second file where the data is retrieved from has 10 rows and 5 columns, each column representing something different. 从中检索数据的第二个文件有10行和5列,每列表示不同的内容。 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. 我使用sc1.nextInt()是因为我们的教授警告我们程序会读取每条数据,而我们还没有学会如何仅从一列中提取数据。 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)" java.util.Scanner.next(Scanner.java:1485)的java.util.Scanner.throwFor(Scanner.java:864)处的线程“ main”中的java.util.InputMismatchException(java.util.Scanner.nextInt(在homework4.HomeWork4.main(HomeWork4.java:26)的java.util.Scanner.nextInt(Scanner.java:2076)在homework4.HomeWork4.checkNumber(HomeWork4.java:47)处的Scanner.java:2117)/ Users / xiomarahenriquez / Library / Caches / NetBeans / 8.2 / executor-snippets / run.xml:53:Java返回:1个构建失败(总时间:0秒)”

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. 在while循环中,您运行两次checkNumber方法。 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)? ++ i和i ++之间也有细微差别,因此请检查此链接: for循环(Java)中的i ++和++ i有什么区别?

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 线程“主”中的异常java.util.InputMismatchException

Here's a link to the documentation for the InputMismatchException . 这是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 . 我不确定,因为我不知道您的输入文件是什么样子,但是我很确定当您调用nextInt() ,下一个读取的标记不是可以转换为int My guess is that the Scanner is encountering some text or something else preventing it from returning an int . 我的猜测是扫描仪遇到某些文本或其他阻止其返回int文本。 To figure out which token is causing the problem, I'd try wrapping your invocations of nextInt() in try/catch blocks. 为了弄清楚是哪个令牌导致了问题,我将尝试将您对nextInt()调用包装在try / catch块中。 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. 当扫描程序引发InputMismatchException ,它将不会传递导致异常的令牌,因此在引发异常之后,您可以获取令牌的值(如next()方法一样)或完全跳过令牌。 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): 这是一个示例(我目前无法访问IDE,因此尚未经过测试,但希望您能理解):

//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. 顺便说一句,如果您不确定自己要获取的令牌是您认为将要使用的类型(在您的情况下为int ),则将代码的这一部分包装在一个try / catch块,以便您可以处理令牌不是您认为的那样的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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