简体   繁体   中英

How to print complete list of input through a Arraylist?

Program statement:
To print student age,location and many phone nos. My code is printing the name but I am having a problem in print the complete list of mobile numbers ,My code is printing only the recent number even if student enters two mobile number.

What mistake I am making here:

Below is my complete program: I have created Three classes:

public class MyStudents {

    int age;
    String location;
    int size;
    ArrayList<String> mob;

    MyStudents(int age, String location, ArrayList<String> mob, int size) {
        this.age = age;
        this.location = location;
        this.size = size;
        this.mob = mob;
    }

    public int getAge() {
        return age;
    }

    public String getLocation() {
        return location;
    }

    public ArrayList<String> getMobileNumber() {
        return mob;
    }
}

public class AddStudentDetails {

    List<MyStudents> arrayList = new ArrayList<>();

    List<MyStudents> buildStudents() {
        System.out.println("ENTER no.OF STUDENTS");
        Scanner in = new Scanner(System.in);
        MyStudents myStudents = new MyStudents(0, null, null, 0);
        int numberOfStudents = in.nextInt();

        ArrayList<String> mob1 = new ArrayList<>();
        while (numberOfStudents > 0) {
            System.out.println("Enter How many mobile numbers You want to provide:");
            int size = in.nextInt();
            int age = in.nextInt();
            String location = in.next();

            int i = 1;
            while (size > 0) {
                mob1 = new ArrayList<>();
                System.out.println("Enter your" + i + "mobile number:");
                String num = in.next();
                mob1.add(num);

                i++;
                size--;
                System.out.println(mob1.size());
            }
            myStudents = new MyStudents(age, location, mob1, size);
            arrayList.add(myStudents);
            numberOfStudents--;
            System.out.println("num"+numberOfStudents);
        }
        return arrayList;
    }
}

public class FetchStudentDetails {

    public void fetchdetails() {
        // TODO Auto-generated method stub
        AddStudentDetails add = new AddStudentDetails();
        List<MyStudents> fetch = add.buildStudents();

        for (MyStudents ms : fetch) {
            System.out.println(ms.age+"..."+ms.location+""+ms.mob);
        }
    }
}

My current output:

1...g[232323]
1...r[2323232]

Expected result:

 1...g[232323,1222555]
  1...r[2323232.56589] 

In the while loop where you read the phone numbers, you only keep the last phone number (since you create a new ArrayList in each iteration).

Change it to:

mob1=new ArrayList<>();
while(size>0){
    System.out.println("Enter your"+ i +"mobile number:");
    String num=in.next();
    mob1.add(num);

    i=i+1;
    size--;
    System.out.println(mob1.size());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM