使用文件编号计算 GCM - NoSuchElementException
Calculating GCM Using File Numbers - NoSuchElementException
我正在尝试将这些数字存入文件并相应地计算 GCM。但是我一直收到 NoSuchElementException。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog280a {
public static void main(String[] args) {
Scanner in = null;
int greater = 1;
int lesser = 1;
int a = 1;
int b = 1;
int originalA = 0;
int originalB = 0;
try {
in = new Scanner(new File(("C:\Users\####\Desktop\####\Eclipse\#######\#######2\src\Prog280a.dat")));
} catch (FileNotFoundException e) {
System.out.println("File is not found try again.");
System.exit(0);
}
while (in.hasNextInt()) {
while (a != 0) {
greater = in.nextInt();
lesser = in.nextInt();
if (greater > lesser) {a = greater; b = lesser;} else {b = greater; a = lesser;}
originalA = a;
originalB = b;
a = a - b;
}
System.out.println("The GCD of " + originalA + " & " + originalB + " is " + b);
a = 1;
b = 1;
}
}
}
好的,在我说的那一行。 'greater = in.nextInt();'系统说没有这样的元素异常。为什么要这样做?请帮忙
内容如下:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Prog280a.main(Prog280a.java:25)
顺便说一下,这些是文件中的数字:
25 10
81 41
255 138
847 624
84 420
182 637
620 420
36 24
75 125
1 17
我的假设是否正确,您不一定会在调用第一个 greater = in.nextInt()
时出错?
因为我认为你的问题出在循环上。
在你的外循环中,你正确地询问了 in.hasNext()
- 但是,在你的内循环中,你似乎在循环 a != 0
- 所以,你可以继续调用 greater = in.nextInt(); lesser = in.nextInt();
而没有检查你的枚举是否有更多元素。
将你内心的 while 更改为 if。第一个 while 正在创建一个连续循环,直到到达文件末尾。
Scanner in = null;
int greater = 1;
int lesser = 1;
int a = 1;
int b = 1;
int originalA = 0;
int originalB = 0;
try {
in = new Scanner(new File(("C:\Users\####\Desktop\####\Eclipse\#######\#######2\src\Prog280a.dat")));
} catch (FileNotFoundException e) {
System.out.println("File is not found try again.");
System.exit(0);
}
while (in.hasNextInt()) {
if (a != 0) {
greater = in.nextInt();
lesser = in.nextInt();
if (greater > lesser) {a = greater; b = lesser;} else {b = greater; a = lesser;}
originalA = a;
originalB = b;
a = a - b;
}
System.out.println("The GCD of " + originalA + " & " + originalB + " is " + b);
a = 1;
b = 1;
}
我正在尝试将这些数字存入文件并相应地计算 GCM。但是我一直收到 NoSuchElementException。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog280a {
public static void main(String[] args) {
Scanner in = null;
int greater = 1;
int lesser = 1;
int a = 1;
int b = 1;
int originalA = 0;
int originalB = 0;
try {
in = new Scanner(new File(("C:\Users\####\Desktop\####\Eclipse\#######\#######2\src\Prog280a.dat")));
} catch (FileNotFoundException e) {
System.out.println("File is not found try again.");
System.exit(0);
}
while (in.hasNextInt()) {
while (a != 0) {
greater = in.nextInt();
lesser = in.nextInt();
if (greater > lesser) {a = greater; b = lesser;} else {b = greater; a = lesser;}
originalA = a;
originalB = b;
a = a - b;
}
System.out.println("The GCD of " + originalA + " & " + originalB + " is " + b);
a = 1;
b = 1;
}
}
}
好的,在我说的那一行。 'greater = in.nextInt();'系统说没有这样的元素异常。为什么要这样做?请帮忙
内容如下:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Prog280a.main(Prog280a.java:25)
顺便说一下,这些是文件中的数字: 25 10 81 41 255 138 847 624 84 420 182 637 620 420 36 24 75 125 1 17
我的假设是否正确,您不一定会在调用第一个 greater = in.nextInt()
时出错?
因为我认为你的问题出在循环上。
在你的外循环中,你正确地询问了 in.hasNext()
- 但是,在你的内循环中,你似乎在循环 a != 0
- 所以,你可以继续调用 greater = in.nextInt(); lesser = in.nextInt();
而没有检查你的枚举是否有更多元素。
将你内心的 while 更改为 if。第一个 while 正在创建一个连续循环,直到到达文件末尾。
Scanner in = null;
int greater = 1;
int lesser = 1;
int a = 1;
int b = 1;
int originalA = 0;
int originalB = 0;
try {
in = new Scanner(new File(("C:\Users\####\Desktop\####\Eclipse\#######\#######2\src\Prog280a.dat")));
} catch (FileNotFoundException e) {
System.out.println("File is not found try again.");
System.exit(0);
}
while (in.hasNextInt()) {
if (a != 0) {
greater = in.nextInt();
lesser = in.nextInt();
if (greater > lesser) {a = greater; b = lesser;} else {b = greater; a = lesser;}
originalA = a;
originalB = b;
a = a - b;
}
System.out.println("The GCD of " + originalA + " & " + originalB + " is " + b);
a = 1;
b = 1;
}