如何调用从文件中读取数据的方法?
How to call a method which reads data from a file?
问题是
Write a method readStock(String[] sAr, double[] pAr, int[] qAr)
to read the following details from a file named “stock.txt”.
所以我创建了包含
的库存文件
pillow 14.50 30
Sheet 43 40
Quilt 52.50 40
Set 100 200
我用这个方法
public static void readStock(String[] sAr, double[] pAr, int[] qAr) throws FileNotFoundException
{
Scanner input = new Scanner(new FileReader("stock.txt")) ;
int i = 0;
while (input.hasNext())
{
sAr[i]= input.next();
pAr[i] = input.nextDouble();
qAr[i] =input.nextInt();
i++;
}
input.close();
System.out.print("ITEM"+" "+"Price"+" "+"Quantity");
for (i=0;i<qAr.length;i++)
{
System.out.println(sAr[i]+" "+pAr[i]+" "+qAr[i]+"");
}
}
但是不知道怎么调用?
我做到了
public static void main(String[] args)
{
readStock();
}
}
但是有错误。
您的方法签名需要 3 个参数,3 个数组,因此您需要将它们传递给方法。您可以这样做: readStock(new String[4], new double[4], new int[4]);
然而,对于这种设计,您需要知道您的文件包含多少行(因为您需要创建具有适当大小的数组)。要使其适用于任何文件长度,只需将数组替换为列表:
public static void readStock(List<String> names, List<Double> prices, List<Integer> quantity) throws FileNotFoundException
{
Scanner input = new Scanner(new FileReader("stock.txt")) ;
while (input.hasNext())
{
names.add(input.next());
prices.add(input.nextDouble());
quantity.add(input.nextInt());
}
input.close();
System.out.print("ITEM"+" "+"Price"+" "+"Quantity");
for (int i=0; i<names.size(); i++)
{
System.out.println(names.get(i)+" "+prices.get(i)+" "+quantity.get(i)+"");
}
}
列表可以在您添加元素时扩展它们的大小,而没有足够的 space 供他使用。要创建新列表,您可以使用 new ArrayList<String>()
你应该传入3个相应的参数,在你的例子中是String[] sAr, double[] pAr, int[] qAr
。因此你应该做类似 readStock(sAr, pAr, qAr);
问题是
Write a method
readStock(String[] sAr, double[] pAr, int[] qAr)
to read the following details from a file named “stock.txt”.
所以我创建了包含
的库存文件pillow 14.50 30
Sheet 43 40
Quilt 52.50 40
Set 100 200
我用这个方法
public static void readStock(String[] sAr, double[] pAr, int[] qAr) throws FileNotFoundException
{
Scanner input = new Scanner(new FileReader("stock.txt")) ;
int i = 0;
while (input.hasNext())
{
sAr[i]= input.next();
pAr[i] = input.nextDouble();
qAr[i] =input.nextInt();
i++;
}
input.close();
System.out.print("ITEM"+" "+"Price"+" "+"Quantity");
for (i=0;i<qAr.length;i++)
{
System.out.println(sAr[i]+" "+pAr[i]+" "+qAr[i]+"");
}
}
但是不知道怎么调用?
我做到了
public static void main(String[] args)
{
readStock();
}
}
但是有错误。
您的方法签名需要 3 个参数,3 个数组,因此您需要将它们传递给方法。您可以这样做: readStock(new String[4], new double[4], new int[4]);
然而,对于这种设计,您需要知道您的文件包含多少行(因为您需要创建具有适当大小的数组)。要使其适用于任何文件长度,只需将数组替换为列表:
public static void readStock(List<String> names, List<Double> prices, List<Integer> quantity) throws FileNotFoundException { Scanner input = new Scanner(new FileReader("stock.txt")) ; while (input.hasNext()) { names.add(input.next()); prices.add(input.nextDouble()); quantity.add(input.nextInt()); } input.close(); System.out.print("ITEM"+" "+"Price"+" "+"Quantity"); for (int i=0; i<names.size(); i++) { System.out.println(names.get(i)+" "+prices.get(i)+" "+quantity.get(i)+""); } }
列表可以在您添加元素时扩展它们的大小,而没有足够的 space 供他使用。要创建新列表,您可以使用 new ArrayList<String>()
你应该传入3个相应的参数,在你的例子中是String[] sAr, double[] pAr, int[] qAr
。因此你应该做类似 readStock(sAr, pAr, qAr);