我有一个计算机程序,我必须从两个文本文件中读取代码,然后使用合并排序合并然后对两个文本文件进行排序。我不能把这两个库存重新放回一个数组并排序!我的代码适用于ArrayLists,因此我将它们注释掉,并在必要时用Array和其他Array元素替换它们,所有更改都发生在Inventory类中。编译时没有语法错误,但我的try catch发现错误。
以下是两个文本文件:
Store1.txt
STORE 1 INVENTORY
ID QTY U/P
362 5 12.98
471 2 6.70
792 13 25.00
901 7 2.98
和Store2.txt
STORE 2 INVENTORY
ID QTY U/P
163 4 7.20
209 5 11.98
471 5 6.70
608 4 5.90
627 2 9.99
792 1 25.00
812 4 6.00
我有多个班级。这是我从
编译程序的测试器类import java.io.*;
public class MergeInventories
{
public static void main()
{
header();
try
{
Inventory store1 = new Inventory(new File("F:\\M359 AP Computer Science\\JAVA\\Merge Inventories\\Store1.txt"));
Inventory store2 = new Inventory(new File("F:\\M359 AP Computer Science\\JAVA\\Merge Inventories\\Store2.txt"));
CombinedInventory mergedInventory = mergeInventories(store1,store2);
store1.print();
store2.print();
mergedInventory.print();
}
catch(Exception e)
{
System.out.print("Error.");
}
}
private static CombinedInventory mergeInventories(Inventory a, Inventory b)
{
CombinedInventory inv = new CombinedInventory();
int i=0,j=0,k=0;
while(i<a.size()&&j<b.size())
{
if(a.getProduct(i).getID() == b.getProduct(j).getID())
{
inv.add(new Product(a.getProduct(i).getID(),a.getProduct(i).getQty()+b.getProduct(j).getQty(),a.getProduct(i).getUP()));
i++;
j++;
}
else if(a.getProduct(i).getID() > b.getProduct(j).getID())
{
inv.add(b.getProduct(j));
j++;
}
else
{
inv.add(a.getProduct(i));
i++;
}
k++;
}
if(j<b.size())
{
for(j=j;j<b.size();j++)
{
inv.add(b.getProduct(j));
}
}
else if(i<a.size())
{
for(i=i;i<a.size();i++)
{
inv.add(a.getProduct(i));
}
}
return inv;
}
private static void header()
{
System.out.println("My Name \n8th Hour \nM359 AP Comp Sci \n");
}
}
库存类:
import java.util.*;
import java.io.*;
public class Inventory
{
protected int increment = -1;
protected int size = 0;
protected Product[] inventory = new Product[20];
//protected ArrayList<Product> inventory = new ArrayList<Product>();
protected String header;
protected static String CATEGORIES = "ID\tQTY\tU\\P";
public Inventory()
{
header = "TOTAL INVENTORY";
}
public Inventory(File file)throws FileNotFoundException
{
Scanner fileScan = new Scanner(file);
header = fileScan.nextLine();
String throwaway = fileScan.nextLine();
while(fileScan.hasNextLine())
{
Scanner lineScan = new Scanner(fileScan.nextLine());
Product p = new Product(lineScan.nextInt(), lineScan.nextInt(), lineScan.nextDouble());
add(p);
}
}
public void add(Product p)
{
inventory[increment++]=p;
//inventory.add(p);
size++;
}
public Product getProduct(int i)
{
//return inventory.get(i);
return inventory[i];
}
public int size()
{
//return inventory.size();
return size;
}
public void print()
{
System.out.println(header);
System.out.println(CATEGORIES);
for(Product p: inventory)
{
System.out.println(p.toString());
}
System.out.println();
}
}
此类合并库存
import java.text.*;
public class CombinedInventory extends Inventory
{
public void print()
{
System.out.println(header);
System.out.println(CATEGORIES + "\tVALUE");
double total = 0;
NumberFormat f = NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
for(Product p: inventory)
{
System.out.println(p.toString() + "\t" + f.format(p.getQty() * p.getUP()));
total += p.getQty() * p.getUP();
}
System.out.println("Total Value of Stock:\t$" + total);
System.out.println();
}
}
最后我们有一个基本的超类
import java.text.*;
public class Product
{
private int idNum;
private int quantity;
private double unitPrice;
public Product()
{
idNum = 0;
quantity = 0;
unitPrice = 0;
}
public Product(int id, int q, double price)
{
idNum = id;
quantity = q;
unitPrice = price;
}
public int getID()
{
return idNum;
}
public int getQty()
{
return quantity;
}
public double getUP()
{
return unitPrice;
}
public String toString()
{
NumberFormat f = NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
return idNum + "\t" + quantity + "\t" + f.format(unitPrice);
}
}
这就是我希望打印的内容
My Name
8th Hour
M359 AP Comp Sci
STORE 1 INVENTORY
ID QTY U\P
362 5 12.98
471 2 6.70
792 13 25.00
901 7 2.98
STORE 2 INVENTORY
ID QTY U\P
163 4 7.20
209 5 11.98
471 5 6.70
608 4 5.90
627 2 9.99
792 1 25.00
812 4 6.00
TOTAL INVENTORY
ID QTY U\P VALUE
163 4 7.20 28.80
209 5 11.98 59.90
362 5 12.98 64.90
471 7 6.70 46.90
608 4 5.90 23.60
627 2 9.99 19.98
792 14 25.00 350.00
812 4 6.00 24.00
901 7 2.98 20.86
Total Value of Stock: $638.94
然而,不是打印所有值&#34;错误&#34;打印出来。我不明白为什么会这样。如何使用Arrays而不是ArrayList来使其工作?
答案 0 :(得分:0)
如果它适用于ArrayList,则不必进行数组操作。如果你真的想要数组,你可以做theArrayList.toArray();
使用ArrayList,它更简单,更面向对象。