java NoSuchElementException 错误
importjava.io.File;importjava.util.Scanner;importjava.io.PrintStream;importtype.lib.S...
import java.io.File;
import java.util.Scanner;
import java.io.PrintStream;
import type.lib.Stock;
import java.text.DecimalFormat;
public class Check05B
{
public static void main(String[] args)throws java.io.IOException {
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
output.print("Enter filename ... ");
Scanner fileInput = new Scanner(new File(input.next()));
Stock stock = new Stock(fileInput.next());
int a = 00000;
double value = stock.getPrice() * fileInput.nextInt();
do{
output.print (new DecimalFormat ("00000 ").format(a));
output.println (stock.getPrice());
stock = new Stock(fileInput.next());
a = a + 2;
value = value + stock.getPrice() * fileInput.nextInt();
//value = value + stock.getPrice() * shares;
//int shares = fileInput.nextInt();
//value = value + stock.getPrice() * fileInput.nextInt();
}
while (fileInput.hasNextLine());
output.print(new DecimalFormat ("00000 ").format(a));
output.println(stock.getPrice());
output.printf ("Total value = %.2f",value);
fileInput.close();
}
}
代码如上,运行时出现NoSuchELementException的错误,请问如何解决
txt文件内容如下
mox
15
.ve
16
.fy
8
mox
34
运行结果如下
Enter filename ... lab5data.txt
00000 0.18
00002 20.64
00004 49.38
00006 0.18
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Check05B.main(Check05B.java:24)
初步分析是程序在搜索到TXT文件倒数第二行的时候,hasNextLine = true,所以程序再次搜索TXT文件中的下一个非整数行的内容,所以返回错误,但是不知道如何更改,期待大神 展开
import java.util.Scanner;
import java.io.PrintStream;
import type.lib.Stock;
import java.text.DecimalFormat;
public class Check05B
{
public static void main(String[] args)throws java.io.IOException {
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
output.print("Enter filename ... ");
Scanner fileInput = new Scanner(new File(input.next()));
Stock stock = new Stock(fileInput.next());
int a = 00000;
double value = stock.getPrice() * fileInput.nextInt();
do{
output.print (new DecimalFormat ("00000 ").format(a));
output.println (stock.getPrice());
stock = new Stock(fileInput.next());
a = a + 2;
value = value + stock.getPrice() * fileInput.nextInt();
//value = value + stock.getPrice() * shares;
//int shares = fileInput.nextInt();
//value = value + stock.getPrice() * fileInput.nextInt();
}
while (fileInput.hasNextLine());
output.print(new DecimalFormat ("00000 ").format(a));
output.println(stock.getPrice());
output.printf ("Total value = %.2f",value);
fileInput.close();
}
}
代码如上,运行时出现NoSuchELementException的错误,请问如何解决
txt文件内容如下
mox
15
.ve
16
.fy
8
mox
34
运行结果如下
Enter filename ... lab5data.txt
00000 0.18
00002 20.64
00004 49.38
00006 0.18
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Check05B.main(Check05B.java:24)
初步分析是程序在搜索到TXT文件倒数第二行的时候,hasNextLine = true,所以程序再次搜索TXT文件中的下一个非整数行的内容,所以返回错误,但是不知道如何更改,期待大神 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询