[英]Printing an ArrayList from a different function
此代碼的目的是接受用戶輸入並將其顯示為單個記錄。 我編寫了一個菜單,后接2個方法(設置和顯示),它們使用Scanner類來填充arrayList,但無法使用display方法來打印它們。
這是代碼,請告訴我如何解決。
import java.io.*;
import java.util.*;
public class Records2 {
static ArrayList information = new ArrayList();
public static void main(String[] args) {
int choice;
do {
System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n Exit");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int choice;
System.out.println("Enter your Choice : ");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
switch (choice) {
case 1: System.out.println("Getting ready to Add a Record ");
set();
break;
case 2: System.out.println("Getting ready to Delete a Record ");
//delete();
break;
case 3: System.out.println("Getting ready to Update a Record ");
//update();
break;
case 4: System.out.println("Here is your record ");
display();
break;
case 5: System.out.println("Out we go.");
System.exit(0);
//exit();
break;
default: System.out.println("Try again");
break;
}
} while ( choice > 5 || choice < 1 );
}
public static void set() {
System.out.println("Please enter the ID, Name and Address of the person \n");
Scanner readId = new Scanner(System.in);
int ID = readId.nextInt();
Scanner readName = new Scanner(System.in);
String name = readName.nextLine();
Scanner readAddress = new Scanner(System.in);
String address = readAddress.nextLine();
//ArrayList information = new ArrayList();
information.add(ID);
information.add(name);
information.add(address);
}
public static void display() {
System.out.println("here is your list" + information);
//how to differentiate from one set to another?
}
}
這是因為您的while循環!
while ( choice > 5 || choice < 1 );
選擇choose = 1后,代碼將退出循環(用於set())
因此,永遠不會調用該顯示。
使用: while ( choice > 0 && choice < 6)
這是完整的代碼-
import java.io.*;
import java.util.*;
public class Records2 {
static ArrayList<Person> information = new ArrayList<Person>();
public static void main(String[] args) {
int choice;
do {
System.out.println("\n-------------------------------\n Enter your Choice : ");
System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n 5.Exit");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Getting ready to Add a Record ");
set();
break;
case 2:
System.out.println("Getting ready to Delete a Record. Enter id - ");
int choice2 = sc.nextInt();
delete(choice2);
break;
case 3:
System.out.println("Getting ready to Update a Record ");
// update();
break;
case 4:
System.out.println("Here is your record ");
display();
break;
case 5:
System.out.println("Out we go.");
System.exit(0);
// exit();
break;
default:
System.out.println("Try again");
break;
}
} while ( choice > 1 || choice < 5 );
}
public static void set() {
System.out.println("Please enter the ID, Name and Address of the person \n");
Scanner readId = new Scanner(System.in);
int ID = readId.nextInt();
Scanner readName = new Scanner(System.in);
String name = readName.nextLine();
Scanner readAddress = new Scanner(System.in);
String address = readAddress.nextLine();
// ArrayList information = new ArrayList();
Person person=new Person();
person.setID(ID);
person.setName(name);
person.setAddress(address);
information.add(person);
}
public static void display() {
System.out.println("here is your list" + information);
// how to differentiate from one set to another?
}
public static void delete(int passedid) {
// System.out.println("here is your list" + information);
// how to differentiate from one set to another?
System.out.println("Before Deleting - "+information);
Iterator<Person> it=information.iterator();
while (it.hasNext()) {
Person p = it.next();
if(p.getID()==passedid){
it.remove();
}
}
System.out.println("AfterDeleting -"+information);
}
}
class Person{
int ID;
String name;
String address;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [ID=" + ID + ", name=" + name + ", address=" + address
+ "]";
}
}
在這里它的輸出-
-------------------------------
Enter your Choice :
1.Add
2.Delete
3.Update
4.Show
5.Exit
1
Getting ready to Add a Record
Please enter the ID, Name and Address of the person
1
A
AA
-------------------------------
Enter your Choice :
1.Add
2.Delete
3.Update
4.Show
5.Exit
1
Getting ready to Add a Record
Please enter the ID, Name and Address of the person
2
B
BB
-------------------------------
Enter your Choice :
1.Add
2.Delete
3.Update
4.Show
5.Exit
2
Getting ready to Delete a Record. Enter id -
1
Before Deleting - [Person [ID=1, name=A, address=AA], Person [ID=2, name=B, address=BB]]
After Deleing - [Person [ID=2, name=B, address=BB]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.