java使用循环添加到类中
我目前正在做一个项目,涉及从文本文件中读取数据并将其存储为一个新类。为了轻松完成这一切,我认为最好使用如下while循环:
while (text.hasNextLine()) {
System.out.print("Please enter class name: ");
tempName = kb.next();
//sort text into parts for the class
Class (tempName?) = new Class();
}
kb是扫描器,tempName是字符串变量
这样的代码行吗?我想我不能只是去“Class name=new Class”,因为每次循环运行时,它都会覆盖以前的数据,因为我需要存储所有数据。如果我能让它变成“Class name(x)=new Class”会更好,其中x是一个int变量,每次循环运行时都会增加,但我不确定这是否有效
如果它不起作用,另一种方法将不胜感激。我也不知道如何验证类名,这样数据就不会被意外覆盖
请注意:我不能使用ArrayList,因为项目说明禁止使用“ArrayList之类的动态数据结构”,所以我只能使用基本Java代码
编辑:以下是我必须使用的一个类
public class Customer_16997761 {
private int customerID;
private String surname;
private String firstname;
private String address;
private String suburb;
private int postcode;
//Bunch of get methods
}
我的文本文件有很多行,这些变量按顺序列出。我需要阅读这一行,把它分成几个部分,作为新客户保存,然后冲洗并重复,直到文件结束
共 (0) 个答案