我有一个 csv,我试图将其读入对象数组。我不断收到以下错误... java.util.InputMismatchException 我认为这是因为它读取的文件由空格而不是逗号分隔。我相信我需要使用 String.split() 方法,但我不确定该怎么做。有什么建议么。这是我到目前为止的代码......
public class Prog7
{
public static void main(String[] args)
{
Part[] parts;
int numParts;
int partNumber;
String description;
double price;
int quantity;
String city;
parts = new Part[100];
numParts = 0;
Scanner inFile = null;
/*
* open file
*/
try
{
inFile = new Scanner( new File( "C:/COSC 210/Assignment#7/parts.txt" ) );
}
catch ( FileNotFoundException e )
{
System.err.println( "Error: file not found" );
}
inFile.useDelimiter(",");
while( inFile.hasNext() )
{
partNumber = inFile.nextInt();
description = inFile.next();
price = inFile.nextDouble();
city = inFile.next();
quantity = inFile.nextInt();
Part p = new Part(partNumber, description, price,
quantity, city);
parts[numParts]= p;
numParts++;
}
inFile.close();
for (int i = 0; i < numParts; i++)
{
System.out.println(parts[i].getPartNumber());
}
}
}