import java.io.*; //Importing to read from file.
import java.util.Scanner; //Importing Scanner class.
import java.text.DecimalFormat; //Importing DecimalFormat class.
public class StockAssessment
{
public static void main(String[] args) throws IOException
{
System.out.println("Joe Williams");
// Setting up DecimalFormat.
DecimalFormat money = new DecimalFormat("$###.##");
// Setting up the scanner.
Scanner keyboard = new Scanner(System.in);
/*
Prompting for the file name and initalizing
it to fileName.
*/
System.out.print("Name of the file? ");
String fileName = keyboard.nextLine();
//Opening the file.
File theFile = new File(fileName);
Scanner inputFile = new Scanner(theFile);
//Making sure the file exists.
if (!theFile.exists())
{
System.out.println("========================");
System.out.println("No transactions processed.");
System.exit(0);
}
//Getting data from stock file.
while (inputFile.hasNext())
{
String str = inputFile.nextLine();
int shareAmount = inputFile.nextInt();
double purchasePrice = inputFile.nextDouble();
double purchCommissRate = inputFile.nextDouble();
double salePrice = inputFile.nextDouble();
double saleComiss = inputFile.nextDouble();
}
inputFile.close();
}
}
我不明白为什么这个代码会找到一个文件而不是另一个文件,如果它们都在源代码所在的文件夹中。代码不完全在那里,我有一个等待放入代码但需要我能够打开Java当前无法找到的文件的方法。如果有人能让我知道如何解决这个问题,那将非常感激。
Name of the file? StockData.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at StockAssessment.main(StockAssessment.java:46)
Press any key to continue . . .
一个名为EmptyFile.txt,另一个名为StockData.txt。它识别空文件但不识别库存数据
DELL: Dell Inc
125 25.567 0.025 28.735 0.025
MSFT: Microsoft
34.1 -15.75 0.012 15.90 0.013
GOOG: Google
56.5 58.125 0.032 67.975 0.030
IBM: IBM Corp
87.3 8.875 0.015 7.500 0.020
答案 0 :(得分:1)
如果找不到该文件,那么它应该给你
java.io.FileNotFoundException: StockData (The system cannot find the file specified).
在你的情况下,问题出在某处。
String str = inputFile.nextLine();
int shareAmount = inputFile.nextInt();
double purchasePrice = inputFile.nextDouble();
double purchCommissRate = inputFile.nextDouble();
double salePrice = inputFile.nextDouble();
double saleComiss = inputFile.nextDouble();
如果请求的值与此结构不匹配,则抛出InputMismatchException。
<强>更新强>
要使代码生效,您需要更改在StockData中编写数据的方式。
DELL: Dell Inc
125
25.567
0.025
28.735
0.025
答案 1 :(得分:1)
在 StockData.txt 中,shareAmount
不是所有公司的int
。例如,
MSFT: Microsoft
34.1 ...
GOOG: Google
56.5 ...
因此,要么将金额列为整数,要么使用nextDouble()
来阅读它们。
String str = inputFile.nextLine();
// cast to int; 34.1 becomes 34
int shareAmount = (int) inputFile.nextDouble();
答案 2 :(得分:0)
您必须添加另一个nextLine才能完成阅读带数字的行。
while (inputFile.hasNext()) {
String str = inputFile.nextLine();
int shareAmount = inputFile.nextInt();
double purchasePrice= inputFile.nextDouble();
double purchCommissRate = inputFile.nextDouble();
double salePrice = inputFile.nextDouble();
double saleComiss = inputFile.nextDouble();
inputFile.nextLine();
}
或者,将所有行都读为String
while( inputFile.hasNextLine() ){
String name = inputFile.nextLine();
String data = inputFile.nextLine();
Scanner ds = new Scanner( data );
int shareAmount = ds.nextInt();
double purchasePrice= ds.nextDouble();
double purchCommissRate = ds.nextDouble();
double salePrice = ds.nextDouble();
double saleComiss = ds.nextDouble();
}