gpt4 book ai didi

java InputMismatchException 从文件中读取

转载 作者:行者123 更新时间:2023-12-02 13:11:11 26 4
gpt4 key购买 nike

我正在实现一个驱动程序来测试决策树,但遇到输入不匹配异常。我可以做什么来解决这个问题?

这是我的代码:

import java.io.FileNotFoundException;

/**
* LoanApprovalAnaylyzer demonstrates the use of a binary decision tree to
* decide the approval of a loan.
*/
public class LoanApprovalAnalyzer
{
/**
* Asks questions of the user to get their credit worthiness.
*/
public static void main (String[] args) throws FileNotFoundException
{
System.out.println ("So, you need a loan.");

DecisionTree expert = new DecisionTree("input2.txt");
expert.evaluate();
}
}

这是我的输入文件的内容:

13
Is your income above $100,000?
Do you have more than 3 dependants?
Do you have more than 6 dependants?
Do you own real estate worth less than $200,000?
Do you own real estate worth more than $200,000?
Are you above the age of 60?
Are you above the age of 45?
Your loan is not approved.
Your loan is approved.
Your loan is not approved.
Your loan is approved
Your loan is not approved.
Your loan is approved
Your loan is not approved.
Your loan is approved
3 7 8
4 9 10
5 11 12
1 3 4
2 5 6
0 1 2

这是我的决策树代码:

import java.util.*;
import java.io.*;
/**
* The DecisionTree class uses the LinkedBinaryTree class to implement
* a binary decision tree. Tree elements are read from a given file and
* then the decision tree can be evaluated based on user input using the
* evaluate method.
*
* @author Java Foundations
* @version 4.0
*/
public class DecisionTree
{
private LinkedBinaryTree<String> tree;

/**
* Builds the decision tree based on the contents of the given file
*
* @param filename the name of the input file
* @throws FileNotFoundException if the input file is not found
*/
public DecisionTree(String filename) throws FileNotFoundException
{
File inputFile = new File(filename);
Scanner scan = new Scanner(inputFile);
int numberNodes = scan.nextInt();
scan.nextLine();
int root = 0, left, right;

List<LinkedBinaryTree<String>> nodes = new java.util.ArrayList<LinkedBinaryTree<String>>();
for (int i = 0; i < numberNodes; i++)
nodes.add(i,new LinkedBinaryTree<String>(scan.nextLine()));

while (scan.hasNext())
{
root = scan.nextInt();
left = scan.nextInt();
right = scan.nextInt();
scan.nextLine();

nodes.set(root, new LinkedBinaryTree<String>((nodes.get(root)).getRootElement(),
nodes.get(left), nodes.get(right)));
}
tree = nodes.get(root);
}

/**
* Follows the decision tree based on user responses.
*/
public void evaluate()
{
LinkedBinaryTree<String> current = tree;
Scanner scan = new Scanner(System.in);

while (current.size() > 1)
{
System.out.println (current.getRootElement());
if (scan.nextLine().equalsIgnoreCase("N"))
current = current.getLeft();
else
current = current.getRight();
}

System.out.println (current.getRootElement());
}
}

最佳答案

问题是这样的:

在您的输入文件中,第一个数字是 13,因此您的代码会跳过 13 行,并在下一行中执行 EXPECTS 和整数 (nextInt())。但有 15 行需要跳过,以便您可以在下一行获取整数。

解决方案:在输入文件中,将第一行的13改为15。

关于java InputMismatchException 从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com