我需要打印出已排序数组的元素,并将它们写入文件。它是Customer类的一组对象(我自己的定义)。 Customer []评论最多包含30个这些对象。现在,问题是我需要数组中的元素数来做到这一点,但是我不能将该变量传递给方法,因为它不是main方法的一部分,也不是全局变量。在菜单方法中调用这些方法。 WriteFile和DisplayRecords都使用num_records。我遇到这个问题的原因是因为早些时候我没有菜单方法,只是简单地从main方法中执行操作,但显然需要菜单方法。如果问题看起来很模糊,我会提前道歉。
public static int readFile(String filename, Customer[] review)throws IOException{
int count=0;
Scanner scan = new Scanner (new File (filename));
/*Reading the first record separatly*/
Customer first = new Customer();
String[] a = scan.nextLine().split("=");
first.set_account_id(Integer.parseInt(a[1].trim()));
a = scan.nextLine().split("=");
first.set_name(a[1].toUpperCase().trim());
a = scan.nextLine().split("=");
first.set_address(a[1].trim());
a = scan.nextLine().split("=");
first.set_phone_number(a[1].trim());
a = scan.nextLine().split("=");
first.set_date_of_birth(a[1].trim());
a = scan.nextLine().split("=");
first.set_balance(Double.parseDouble(a[1].trim()));
scan.nextLine();// resets the buffer reader
review[0]= first;
count = count+1;
while (scan.hasNext()&& count>0){
Customer temp = new Customer();
String[] st = scan.nextLine().split("=");
for(int i=1;i<count;i++){
if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records
System.out.println("This account id is already in use so the record won't be read");
for (int k=0; k<7; k++)
scan.nextLine();
}
else
break;
}
temp.set_account_id(Integer.parseInt(st[1].trim()));
st = scan.nextLine().split("=");
temp.set_name(st[1].toUpperCase().trim());
st = scan.nextLine().split("=");
temp.set_address(st[1].trim());
st = scan.nextLine().split("=");
temp.set_phone_number(st[1].trim());
st = scan.nextLine().split("=");
temp.set_date_of_birth(st[1].trim());
st = scan.nextLine().split("=");
temp.set_balance(Double.parseDouble(st[1].trim()));
if (scan.hasNextLine()){
scan.nextLine();
}
int j;
for(j=0;j<count;j++){
if (temp.get_name().compareTo(review[j].get_name())<0){ // Putting records in ascending order
break;
}
}
count=count+1;
for (int k=count;k>j;k--){
review[k]=review[k-1];
}
review[j]= temp;
if (count>=30){
System.out.println("The number of records read has exceeded the limit and it will stop reading now");
break;
}
}
return count;
}
public static void displayRecords(Customer[] review, int num_records){
for (int k=0; k< num_records; k++){
System.out.println(review[k].get_name());
System.out.println(review[k].get_balance());
System.out.println(review[k].get_accountid());
System.out.println(review[k].get_date_of_birth());
System.out.println(review[k].get_address());
System.out.println(review[k].get_phone_number());
}
}
public static int writeFile(Customer[] review, int num_records ) throws IOException{
int count=0;
BufferedWriter out = new BufferedWriter(new FileWriter("newCustomers.txt"));
for (int i=0; i<num_records;i++){
out.write("Name = " + review[i].get_name()+ "\r\n");
out.write("Balance = " +(int)review[i].get_balance() + "\r\n");
out.write("Accoutn Id = " + review[i].get_accountid() + "\r\n");
out.write("DOB = " + review[i].get_date_of_birth() + "\r\n");
out.write("Address = " + review[i].get_address() + "\r\n");
out.write("Phone Number = " + review[i].get_phone_number() + "\r\n");
out.newLine();
count++;
}
out.close();
return count;
}
public static void menu(Customer[] records)throws IOException{
Scanner in = new Scanner(System.in);
int choice;
Boolean loopcont= true;
while (loopcont){
System.out.println("1. To Read the data");
System.out.println("2. To Display the date");
System.out.println("3. To Write the data");
System.out.println("4. Exit");
System.out.println("Enter your choice");
choice = in.nextInt();
switch (choice) {
case 1: {
String filename;
System.out.println("Enter the filename for the input file");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
filename = reader.readLine();
int num_records=0;
try{
num_records= readFile(filename,records);
System.out.println("The number of records read = " + num_records);
}
catch(IOException e){
e.printStackTrace();
}
break;
}
case 2: {
displayRecords(records,num_records);
break;
}
case 3: {
int num_rec_written = writeFile(records,num_records);
System.out.println("The number of records writeen = " + num_rec_written);
break;
}
case 4: {
loopcont = false;
break;
}
default: {
System.out.println("Incorrect input. Kindly enter again");
break;
}
}
}
}
答案 0 :(得分:3)
review.length
将为您提供Customer数组中的条目数,这可能会解决您的问题,另一种方法是使用foreach循环
for(Customer c : review){
// c is each customer in the array
}
答案 1 :(得分:0)
如果num_records
是数组的大小,则num_records = review.length
为真,因此无需明确传递num_records
。
请注意,在Java 5+中,您可以执行以下操作:
for (Customer customer : review){
System.out.println(customer .get_name());
System.out.println(customer .get_balance());
System.out.println(customer .get_accountid());
System.out.println(customer .get_date_of_birth());
System.out.println(customer .get_address());
System.out.println(customer .get_phone_number());
}
答案 2 :(得分:0)
假设我理解了这个问题,只需获取数组的长度!即。
int num_recourds = review.length;
答案 3 :(得分:0)
上面有答案,我想给你一些建议。尽量避免在您的应用程序中使用此代码:
System.out.println(customer .get_name());
System.out.println(customer .get_balance());
System.out.println(customer .get_accountid());
System.out.println(customer .get_date_of_birth());
System.out.println(customer .get_address());
System.out.println(customer .get_phone_number());
将其替换为Customer.getString()
方法。
答案 4 :(得分:0)
使用集合类(如ArrayList)而不是数组怎么样?该类封装了元素的数量以及元素本身。