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.