从不同文件添加项目并使用数组列表将它们存储在另一个文件中
Posted
技术标签:
【中文标题】从不同文件添加项目并使用数组列表将它们存储在另一个文件中【英文标题】:Adding items from a different file and storing them in another using an array list 【发布时间】:2016-08-14 02:31:22 【问题描述】:嘿,所以对于这个程序,我总共有 3 个文件,我想要实现的是从一个文件中获取用户输入的内容,然后将其存储在另一个文件的数组中,这样我就可以调用稍后再回来。
第一个程序是用户输入的位。
import java.util.Scanner;
导入 java.util.*; 导入 java.util.ArrayList;
公共课 Inv
public static void main(String args[])
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;
//Product product = new Product();
System.out.println(" INVENTORY MANAGEMENT SYSTEM");
System.out.println("===============================================");
System.out.println("1. ADD PRODUCT DATA");
System.out.println("2. VIEW PRODUCT DATA");
System.out.println("3. VIEW REPRLENISHMENT STRATEGY");
System.out.println("===============================================");
System.out.println("4. EXIT PROGRAM");
while(n!=4)
System.out.print("\n Please enter option 1-4 to continue...: ");
n = Integer.parseInt(System.console().readLine());
if (n>4||n<1)
System.out.print("Invalid input, please try again...");
continue;
if (n==1)
str="y";
while(str.equals("y")||str.equals("Y"))
Inv.addItem();
System.out.print("Would you like to enter another product ? (Y or N) : ");
str = console.next();
continue;
if (n==2)
str="y";
while(str.equals("y")||str.equals("Y"))
Inv.prodData();
System.out.println("\n***************************************************\n");
System.out.print("Stay viewing this page? (Y or N) ");
str = console.next();
continue;
else
if (n==3)
System.out.print("View Replenishment Strategy.");
continue;
System.out.print("\nThank you for using this inventory management software.\n");
System.out.print("Developed by Xavier Edwards");
System.out.println("\n***************************************************\n");
public static Product product;
public static Store store;
public static void addItem ()
Scanner console = new Scanner(System.in);
product = new Product();
String desc, id, str="";
double price = 0, sUpPrice = 0, unitCost = 0, inventoryCost = 0;
int stock = 0, demand = 0;
if (product == null) //If product 1 is empty
System.out.print("Please enter product description between 3 to 10 characters...: ");
desc = console.next();
desc = desc.toLowerCase();
product.setName(desc);
if ((desc.length() < 3 || desc.length() > 10))
System.out.println("\nThis Input is incorrect. Please make description between 3 to 10 characters.\n");
System.out.println("Try again with different input. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter price in $ : ");
price = console.nextDouble();
product.setPrice(price);
if (price < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter set up price. $ : ");
sUpPrice = console.nextDouble();
product.setsUpPrice(sUpPrice);
if (sUpPrice < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter unit- cost. $ : ");
unitCost = console.nextDouble();
product.setunitCost(unitCost);
if (unitCost < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter the inventory cost. $ : ");
inventoryCost = console.nextDouble();
product.setinvCost(inventoryCost);
if (inventoryCost < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter the amount in stock : ");
stock = console.nextInt();
product.setstock(stock);
if (stock < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.print("Please enter the demand of the product : ");
demand = console.nextInt();
product.setdRate(demand);
if (demand < 0)
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
System.out.println("\n*****************************************\n");
System.out.print(desc +" Product was added successfully ");
System.out.println("\n*****************************************\n");
store.add(product);
public static void prodData()
Scanner console = new Scanner(System.in);
String pOption, str;
System.out.print("\nEnter product description to view the data...\n");
pOption = console.next();
if (product == null)
System.out.println("\nThere is no information on this product.\n");
System.out.println("\nWould you like to try again? (Y or N) \n");
str = console.next();
Inv.prodData();
System.out.println("The information for the product is..... ");
System.out.println("\n*****************************************\n");
System.out.println("Product description : "+product.getName());
System.out.println("Price : $ "+product.getPrice());
System.out.println("Set-up Price : $ "+product.getsUpPrice());
System.out.println("Unit Cost : $ "+product.getunitCost());
System.out.println("Inventory Cost : $ "+product.getinvCost());
System.out.println("Amount of Stock : "+product.getstock());
System.out.println("Amount of Stock : "+product.getdRate());
第二个程序是带有数组列表的位,我似乎无法声明数组列表,也无法获取数据来填充数组列表。
公共类商店 私有 ArrayList ProductList = new ArrayList ();
public Store()
//ArrayList = "";
public void add(Product product)
ProductList.add(product);
public Product getProduct(String prodName)
for(int i = 0; i< ProductList.length; i++)
if(ProductList(i).name == prodName)
return ProductList(i);
任何帮助都会很棒,谢谢。
【问题讨论】:
【参考方案1】:在 ArrayList 中,您需要使用要访问的对象的索引调用 get 方法。 代替 getProduct 方法中的 ProductList(i),执行 ProductList.get(i)
getProduct 函数现在应该如下所示:
public Product getProduct(String prodName)
for (int i = 0; i < ProductList.size(); i++)
if (ProductList.get(i).getName().equals(prodName))
return ProductList.get(i);
return null;
在ProductList.get(i).getName().equals(prodName)
行中,不要使用==
,而是使用equals
方法进行比较。它将比较字符串的值。如果您使用==
,它将比较两个字符串的对象引用,在您的情况下,即使它的值相同,它也会始终给出 false,因为您用于比较的两个字符串对象不同。
也只是一个建议,在产品类中创建公共 getter setter 并通过 getName() 方法访问 name 变量。
【讨论】:
好的,感谢我更新了代码,但现在它返回找不到符号的错误。符号:方法长度 它到底在哪里给出错误?没有正确的日志就不可能提供帮助 位置:ArrayList在声明一个ArrayList时,需要指定它持有的数据类型。
在您的示例中,据我所知,productList
应该使用 private ArrayList<Product> productList = new ArrayList<>();
进行实例化
【讨论】:
以上是关于从不同文件添加项目并使用数组列表将它们存储在另一个文件中的主要内容,如果未能解决你的问题,请参考以下文章