代码可以编译,但是当我执行它时,错误提示
“ java.util.NoSuchElementException”并突出显示湿度= input.next();
示例输出如下:
K-Nearest Neighbor Prediction Program:
The values you entered are: sunny cool high true
Comparring the values: sunny hot high false dist = 2 and play = no
Comparring the values: sunny hot high true dist = 1 and play = no
Comparring the values: overcast hot high false dist = 3 and play = yes
Comparring the values: rainy mild high false dist = 3 and play = yes
Comparring the values: rainy cool normal false dist = 3 and play = yes
这是我的代码:
import java.util.Scanner;
public class lab2
{
public static void main (String [] args)
{
Scanner input = new Scanner ("data.txt");
System.out.println("The values you entered are : ");
String queryOutlook = "sunny";
String queryHumidity = "high";
String queryTemp = "cool";
String queryVerd = "true";
String outlook = null;
String humidity = null;
String temp = null;
String verd = null;
String play= null;
int distance = 0;
while(input.hasNext())
{
outlook = input.next();
***humidity = input.next();***
temp = input.next();
verd = input.next();
if (!outlook.equalsIgnoreCase(queryOutlook) )
distance++;
if (!humidity.equalsIgnoreCase(queryHumidity) )
distance++;
if (!temp.equalsIgnoreCase(queryTemp) )
distance++;
if (!verd.equalsIgnoreCase(queryVerd) )
distance++;
System.out.println(outlook + humidity + temp + verd + "\t" + play + distance);
同样,该程序与K邻居最近的预测有关,这是自上一堂课以来我从未听说过的。任何帮助,将不胜感激!!!
最佳答案
while(input.hasNext()) {
outlook = input.next();
humidity = input.next();
temp = input.next();
verd = input.next();
您正在检查输入是否有一个下一个元素,如果有,则得到四个下一个元素。在获取每一个之前,您应该检查一下。
关于java - 我的while循环中JAVA程序出现运行时错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21539550/