简体   繁体   中英

Creating an ArrayList using a Scanner

I am working on a project to create a list of students using an Array List. I know how to create a simple array list, however, I decided that I want to use the Scanner method. Unfortunately, THAT is where my troubles begin. Here is what my origin class looks like:

import java.util.Scanner;

/**
 * Used to create a single student.
 */

public class Student
{
private String Name;
private int Age;
private String Gender;
private int heightInches; //inches%maxInches
private int heightFeet; //inches/maxInches
private int Inches;
private final int maxInches = 12;
private int Weight;
private String Position;
private Scanner keybd;

/**
 * Constructor
 */
public Student(){
    keybd = new Scanner(System.in);
    setStudent();
}

/**
 * Method to create a student
 */
public void setStudent(){
    System.out.println("Enter name of student:");
    Name = keybd.next();
    System.out.println("Enter age of student:");
    Age = keybd.nextInt();
    System.out.println("Enter gender of student:");
    Gender = keybd.next();
    System.out.println("Enter height in inches of student:");
    Inches = keybd.nextInt();
    if(Inches>= maxInches){
        heightFeet = Inches/maxInches;
        heightInches = Inches%maxInches;
    }
    else{
        heightInches = Inches%maxInches;}
    System.out.println("Enter position of user:");
    Position = keybd.next();
    System.out.println("Enter weight of student:");
    Weight = keybd.nextInt();
}

/**
 * Returns height of student
 */
public void getHeight(){
    System.out.println(heightFeet + "'" + heightInches + "''");
}

/**
 * Prints details of student
 */
public void printDetails(){
    if((Position.equals("Doctor")) || (Position.equals("Coach"))){
        System.out.println(Name + " who is a " + Age + " year old " + Gender + " weighs " + Weight + " and is ");
        getHeight();}
    else{System.out.println(Name + " who is a " + Age + " year old " + Gender + " is ");
        getHeight();
    }
}

}

Unfortunately, when I try calling the setStudent method in my new class which will be calling the Students class in order to actually make a list, I run into issues. I really wanted to have an "if" statement along with a Scanner that if the user desires to add another student it would loop, otherwise it would end, however, since I cant even create a new student using the above code, it is not even worth my time to try attempting that yet.

Each Student object is its own instance of the Student class. When you read all that data in the constructor, you work with that particular Student object. You could make a loop in the constructor, creating new Student objects, but that would be fairly confusing -- after all, a constructor's job is to create one object.

(That aside, it's bad style to have the constructor ask the user for input. It makes the class untestable in unit testing, and it ties it to a console, making it unusable in a GUI program. You'd be better off by reading all the student data elsewhere, and then passing it to the Student constructor.)

So, some code outside the Student class, eg, your main method) should have the list of Students, collect the input, and create the Student objects.

I decided to try keeping it simple (first time in a long while that I took the simple way out) In my new class I put this:

/**
 * Method to add more students
 */
public void addStudent(){
    studentList.add  (new Student());
}

On the bright side, now I can try using an if-else method to add more students.

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