我在创建将数据从txt文件导入1d字符串数组和2d双数组的方法时遇到问题。 以下是我到目前为止的情况:
public static void main(String[] args){
String[] productName = new String[100];
double[][] sales = new double[100][5];
initializeArrays(productName, sales);
....}
public static void initializeArrays(String a[], double b[][]){
try
{
String output = "";
File inputFile = new File("c:/temp/salesdata.txt");
if (inputFile.exists())
{
Scanner scanner = new Scanner(inputFile);
while (scanner.hasNext())
{
for (i=1;i<6;i++)
{
b[a.length][i]=scanner.nextDouble();
}
rowCount += 1;
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Error reading file: ");
}
答案 0 :(得分:0)
试试这段代码:
public static void initializeArrays(String a[], double b[][]){
try
{
String output = "";
File inputFile = new File("c:/temp/salesdata.txt");
int counter = 0;
if (inputFile.exists())
{
Scanner scanner = new Scanner(inputFile);
while (scanner.hasNext())
{
for (i=1;i<6;i++)
{
b[counter][i]=scanner.nextDouble();
}
counter++;
rowCount += 1;
}
}
}
catch (FileNotFoundException e)
{
System.out.println("Error reading file: ");
}