Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Please help me first time user, and need help with a method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me first time user, and need help with a method

    Hi guys, thanks for stopping by to help, inside my StudetList.java, the method AddS(meaning addstudent), i was wondering how i can take a users input and setting it to first name, last name, student number, and gpa, don't i need to call the method from my student.java?
    package student;
    /**
     * 
     * @author Boo
     *this program gets each student info and set it, and print as a string.
     */
     
    public class Student 
    {
    	//fields
    	private String firstName;
    	private String lastName;
    	private int SNumber;
    	private String Major;
    	private double gpa;
    	private static int count = 0;
     
    	//default constructor
    	public Student ()
    	{
     
    	}
     
     
    	public Student (String firstName, String lastName, int SNumber, String major, double gpa)
    	{
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.SNumber = SNumber;
    		this.Major = major;
    		this.gpa = gpa;		
    		count++;
    	}
     
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
     
    	public String getLastName() {
    		return lastName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
     
    	public int getSNumber() {
    		return SNumber;
    	}
     
    	public void setSNumber(int sNumber) {
    		SNumber = sNumber;
    	}
     
    	public String getMajor() {
    		return Major;
    	}
     
    	public void setMajor(String major) {
    		Major = major;
    	}
     
    	public double getGpa() {
    		return gpa;
    	}
     
    	public void setGpa(double gpa) {
    		this.gpa = gpa;
    	}
     
     
    	public void decrementCount(){
    		count--;
    	}
     
    	@Override
    	public String toString () {
    		return String.format(getFirstName(), getLastName(), getSNumber(), getMajor(),  getGpa());
    	}
    }
    Attached Files Attached Files
    Last edited by Norm; February 7th, 2013 at 04:40 PM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    Please post the code here on the thread, not as links.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keyboard scanner to take users input and enter students information method

    I believe I have completed my student.java and I am not done with my StudentList.java, under AddS method in StudentList.java, i have created a scanner to take users input, prompting for user to enter firstname, lastname, gpa, and student number, then do i have to go over to my student.java and call those methods and pass it using users input as the variable? please help, i am stuck
    package student;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     * This class prints a list of menu, and according to users selection from char 1-6, each method will be called and executed.
     * @author Boo
     *
     */
     
    public class StudentList {
    	//creates an Arraylist that contains the elements of Student (in Student.class)
    	ArrayList<Student> List = new ArrayList<Student>();
     
    	public static void main (String[] args){
    		//creates a scanner keyboard that captures keyboard inputs
    		Scanner keybd = new Scanner(System.in);
    		char selection;
     
    		//display menu
    		do{
    			System.out.println("\n------------");
    			System.out.println(" Main Menu\n");
    			System.out.println("1. Add a Student");
    			System.out.println("2. Find a Student");
    			System.out.println("3. Delete a Student");
    			System.out.println("4. Display all Students");
    			System.out.println("5. Total number of Students");
    			System.out.println("6. Exit");
     
    			//get menu selection
    			selection = keybd.next().charAt(0);
     
    			//process menu selection, and when a char is entered, method will be called, executed, and break when completed.
    			switch (selection)
    			{			
    				case '1':
    					AddS();
    					break;
    				case '2':
    					FindS();
    					break;
    				case '3':
    					DeleteS();
    					break;
    				case '4':
    					DisplayS();
    					break;
    				case '5':
    					TotalS();
    					break;
    				case '6':
    					//recognize as valid selection but do nothing
    					break;
    				default:
    					System.out.printf("%c\n",7);
    					System.out.println("Invalid Selection");			
    			}//end switch
    		}while( selection != '6');			
    	}//end main()
     
    	private static void AddS()
    	{
    		//creates a scanner which will take the users input and set it as input
    		//then call setFirstName method which takes input as the variable
    		Scanner keybd = new Scanner (System.in);
    		char input = keybd.next().charAt(0);
    //		setFirstName(input);
     
    	}
    	private static void FindS()
    	{
     
    	}
    	private static void DeleteS()
    	{
     
    	}
    	private static void DisplayS()
    	{
     
    	}
    	private static void TotalS()
    	{
     
    	}
     
     
     
    }

    package student;
    /**
     * 
     * @author Boo
     *this program gets each student info and set it, and print as a string.
     */
     
    public class Student 
    {
    	//fields
    	private String firstName;
    	private String lastName;
    	private int SNumber;
    	private String Major;
    	private double gpa;
    	private static int count = 0;
     
    	//default constructor
    	public Student ()
    	{
     
    	}
     
     
    	public Student (String firstName, String lastName, int SNumber, String major, double gpa)
    	{
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.SNumber = SNumber;
    		this.Major = major;
    		this.gpa = gpa;		
    		count++;
    	}
     
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
     
    	public String getLastName() {
    		return lastName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
     
    	public int getSNumber() {
    		return SNumber;
    	}
     
    	public void setSNumber(int sNumber) {
    		SNumber = sNumber;
    	}
     
    	public String getMajor() {
    		return Major;
    	}
     
    	public void setMajor(String major) {
    		Major = major;
    	}
     
    	public double getGpa() {
    		return gpa;
    	}
     
    	public void setGpa(double gpa) {
    		this.gpa = gpa;
    	}
     
     
    	public void decrementCount(){
    		count--;
    	}
     
    	@Override
    	public String toString () {
    		return String.format(getFirstName(), getLastName(), getSNumber(), getMajor(),  getGpa());
    	}
    }

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    how do i do that? i see people's post can be scrolled up and down, but all i know is control +C then control +v

    --- Update ---


    package student;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     * This class prints a list of menu, and according to users selection from char 1-6, each method will be called and executed.
     * @author Boo
     *
     */
     
    public class StudentList {
    	//creates an Arraylist that contains the elements of Student (in Student.class)
    	ArrayList<Student> List = new ArrayList<Student>();
     
    	public static void main (String[] args){
    		//creates a scanner keyboard that captures keyboard inputs
    		Scanner keybd = new Scanner(System.in);
    		char selection;
     
    		//display menu
    		do{
    			System.out.println("\n------------");
    			System.out.println(" Main Menu\n");
    			System.out.println("1. Add a Student");
    			System.out.println("2. Find a Student");
    			System.out.println("3. Delete a Student");
    			System.out.println("4. Display all Students");
    			System.out.println("5. Total number of Students");
    			System.out.println("6. Exit");
     
    			//get menu selection
    			selection = keybd.next().charAt(0);
     
    			//process menu selection, and when a char is entered, method will be called, executed, and break when completed.
    			switch (selection)
    			{			
    				case '1':
    					AddS();
    					break;
    				case '2':
    					FindS();
    					break;
    				case '3':
    					DeleteS();
    					break;
    				case '4':
    					DisplayS();
    					break;
    				case '5':
    					TotalS();
    					break;
    				case '6':
    					//recognize as valid selection but do nothing
    					break;
    				default:
    					System.out.printf("%c\n",7);
    					System.out.println("Invalid Selection");			
    			}//end switch
    		}while( selection != '6');			
    	}//end main()
     
    	private static void AddS()
    	{
    		//creates a scanner which will take the users input and set it as input
    		//then call setFirstName method which takes input as the variable
    		Scanner keybd = new Scanner (System.in);
    		char input = keybd.next().charAt(0);
    //		setFirstName(input);
     
    	}
    	private static void FindS()
    	{
     
    	}
    	private static void DeleteS()
    	{
     
    	}
    	private static void DisplayS()
    	{
     
    	}
    	private static void TotalS()
    	{
     
    	}
     
     
     
    }


    --- Update ---

    Could you please remove this post, sorry for doing it wrong, i have posted a new one but doing it the right way with the right format hehe thanks
    Last edited by Norm; February 7th, 2013 at 04:39 PM. Reason: Added code tags

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    I've merged the threads.
    i am stuck
    What problems are you having? Can you explain?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    Under my student.java, scroll all the way down and you will see private static void AddS(), I am stuck there, I am suppose to prompt the user to add a student information such as firstName, lastName, studentNumber, and gpa, as you see the getters and setters method in the Student.java. I have created a keyboard scanner to take the users input, but how do i ask the user each time and set the the info to each of those method? do i have to call those method, or do i store it in the arraylist?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    but how do i ask the user each time
    Use the println() method to ask a question
    and one of the Scanner class methods to read the answer.
    When you have all the answers you need to build a Student object, use the Student class's constructor to build an object and save that object in the arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    Quote Originally Posted by Norm View Post
    Use the println() method to ask a question
    and one of the Scanner class methods to read the answer.
    When you have all the answers you need to build a Student object, use the Student class's constructor to build an object and save that object in the arraylist.
    should i ask each question individually and a scanner right after each? and once i have all the answers, how do i construct the student object, since my student constructor is in another class?? thank you verymuch

    private static void AddS(String firstname, String lastName, int SNumber, String major, double gpa)
    	{
    		Scanner keybd = new Scanner (System.in);
    //		char input = keybd.next().charAt(0);
     
    		//creates a scanner which will take the users input and set it as input
    		//then call setFirstName method which takes input as the variable
    		System.out.println("enter firstname");
    		char firstname1 = keybd.next(Student);
    		System.out.println("enter lastname");
    		System.out.println("enter studentnumber");
    		System.out.println("enter major");
    		System.out.println("enter gpa");
     
    		list1
    //		setFirstName(input);


    --- Update ---

    update: i figured out that i have to do String firstname = keybd.next(); and when i check by printing it out, it printed what i wrote but my questions to the above still remains, of how do i gather all that answers and make it a student object, do i have to call the student constructor? so confused

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    why is the int snumber = keybd.next(); giving me an error, suggested fix was "change type to String" but in student class constructor, it is suppose to be an int. and am I doing it right now? I have set the student1.firstName = firstname; i think it is wrong though, cause i had to change the Student class fields to default meaning i have to remove the private, so it is accessible, but then it should be in the Student constructor not the field? I still don't understand how I can construct the Student, all i am doing right now, is giving its attribute with the user input, or maybe i did construct one since i did wrote new Student(). how would I go about storing this in the ArrayList? please help, and thanks
    private static void AddS()//Object Student
    	{
    		Scanner keybd = new Scanner (System.in);
    //		char input = keybd.next().charAt(0);
    		//create a new student object
    		//new operator invokes the object constructor
    		Student student1 = new Student();//should be stored in the ArrayList, then print the ArrayList of Student Objects
     
    		//creates a scanner which will take the users input and set it as input
    		//then call setFirstName method which takes input as the variable
    		System.out.println("enter firstname");
    		String firstname = keybd.next();
    		student1.firstName = firstname;
    		student1.setFirstName(firstname);//pass firstname into this method
    //		getFirstName();
     
    		System.out.println(firstname);
     
    		System.out.println("enter lastname");
    		String lastname = keybd.next();
    		student1.lastName = lastname;
    		System.out.println(lastname);
     
    //		System.out.println("enter studentnumber");
    		int snumber = keybd.next(); 
    //		student1.SNumber = snumber;
    //		System.out.println(snumber);
    		System.out.println("enter major");
    		String major = keybd.next();
    //		student1.setFirstName(firstName) = major;
    		System.out.println(major);

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    You need to read the API doc for the Scanner class to find the correct method to use to read an int value.
    When writing an assignment statement to save a value returned by a method, the type of the variable that is receiving the value (on left of =) must match the type that the method returns.

    how I can construct the Student
    See the links in post#8. They show how to use a class's constructor.
    Basically: get all the values needed in the Student class's constructor and use them in the call to the constructor when you create an instance of Student.
    public Student (String firstName, String lastName, int SNumber, String major, double gpa)
    You need 5 values for this constructor.

    how would I go about storing this in the ArrayList
    Use the ArrayList class's add() method.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    I think I got it, not sure though could you please confirm if I am right or did I did wrong again?
    private static void AddS(String firstName, String lastName,  int SNumber,  String major, double gpa)//Object Student
    	{
    		Scanner keybd = new Scanner (System.in);
    		//create a new student object
    		//new operator invokes the object constructor
    		Student student1 = new Student(firstName, lastName,  SNumber,  major, gpa );//should be stored in the ArrayList, then print the ArrayList of Student Objects		
     
    		//then call setFirstName method which takes input as the variable
    		System.out.println("enter firstname");
    		String firstname = keybd.nextLine();
    		firstName = firstname;
    //		student1.firstName = firstName;
    //		student1.setFirstName(firstName);
    		//pass firstname into this method, this object now have action and behavior
     
    		System.out.println("enter lastname");
    		String lastname = keybd.nextLine();
    		lastName = lastname;
    //		student1.lastName = lastName;
    //		student1.setLastName(lastName);
    //		System.out.println(lastname);
     
    		System.out.println("enter studentnumber");
    		int snumber = keybd.nextInt(); 
    		SNumber = snumber;
    //		student1.SNumber = sNumber;
    //		student1.setSNumber(sNumber);
    		//call setSNumber method and pass value of sNumber into it
     
    		System.out.println("enter major");
    		String Major = keybd.next();
    		major = Major;
    //		student1.Major=major;
    //		student1.setMajor(major);
    //		System.out.println(major);
     
    		System.out.println("enter gpa");
    		double Gpa= keybd.nextDouble();//read input as a double
    		gpa = Gpa;
    //		student1.gpa=gpa;//set the Object field gpa to be gpa
    //		student1.setGpa(gpa);
    		//student1Object calls setGpa method and passing gpa value in it
    //		System.out.println(gpa);		
     
    //		int onelength = wordlistone.length;
    		//		list1 = student1;
    		System.out.print(student1);//why is it oly printing first name

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

        String firstname = keybd.nextLine();
        firstName = firstname;
    It's not necessary to copy from one variable to another. Save the value in the variable:
      firstName = keybd.nextLine();
    This statement should be AFTER the variables have been given values:
        Student student1 = new Student(firstName, lastName,  SNumber,  major, gpa );
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    package student;
    /**
     * 
     * @author Boo
     *this program gets each student info and set it, and print as a string.
     */
     
    public class Student 
    {
    	//fields
    	//why does it have to be private
    	private String firstName;
    	private String lastName;
    	private int SNumber;
    	private String Major;
    	private double gpa;
    	public static int count = 0;
     
    	//default constructor
    	public Student ()
    	{
     
    	}
     
    	/**
    	 * Constructs a student object, constructor always have same name as the class name
    	 * @param firstName
    	 * @param lastName
    	 * @param SNumber
    	 * @param major
    	 * @param gpa
    	 */
    	public Student (String firstName, String lastName, int SNumber, String major, double gpa)//parameter have same name as class field, which is known as shadow.
    	{
    		//referring to the passed-in argument
    		this.firstName = firstName;
    		this.lastName = lastName;
    		this.SNumber = SNumber;
    		this.Major = major;
    		this.gpa = gpa;		
    		count++;
    	}
     
    	public String getFirstName() {
    		return firstName;
    	}
     
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
     
    	public String getLastName() {
    		return lastName;
    	}
     
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
     
    	public int getSNumber() {
    		return SNumber;
    	}
     
    	public void setSNumber(int sNumber) {
    		SNumber = sNumber;
    	}
     
    	public String getMajor() {
    		return Major;
    	}
     
    	public void setMajor(String major) {
    		Major = major;
    	}
     
    	public double getGpa() {
    		return gpa;
    	}
     
    	public void setGpa(double gpa) {
    		this.gpa = gpa;
    	}
     
     
    	public void decrementCount(){
    		count--;
    	}
     
    	@Override
    	public String toString () {
    		return String.format(getFirstName(), getLastName(), getSNumber(), getMajor(),  getGpa());
    	}
    }
    package student;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    /**
     * This class prints a list of menu, and according to users selection from char 1-6, each method will be called and executed.
     * @author Boo
     *
     */
     
    public class StudentList {
    	private static final int numb = 0;
    	//creates an Arraylist that contains the elements of Student (in Student.class)
    	//actually a collection that can hold any amount, changes itself, while with array you have to specify an amount that it can hold.
    	ArrayList<Student> list = new ArrayList<Student>();
    	Scanner keybd = new Scanner(System.in);
     
    	public void menu()
    	{
    		//creates a scanner keyboard that captures keyboard inputs
     
    		char selection;
     
    		//display menu
    		do{
    			System.out.println("\n------------");
    			System.out.println(" Main Menu\n");
    			System.out.println("1. Add a Student");
    			System.out.println("2. Find a Student");
    			System.out.println("3. Delete a Student");
    			System.out.println("4. Display all Students");
    			System.out.println("5. Total number of Students");
    			System.out.println("6. Exit");
     
    			//get menu selection
    			selection = keybd.next().charAt(0);
     
    			//process menu selection, and when a char is entered, method will be called, executed, and exit to the menu selection when completed
    			//(also so that it doesn't execute the other methods, without be selected by the user)
     
    			switch (selection)
    			{			
    				case '1':
    					AddS();
    					break;
    				case '2':
    					FindS();
    					break;
    				case '3':
    					DeleteS();
    					break;
    				case '4':
    					DisplayS();
    					break;
    				case '5':
    					TotalS();
    					break;
    				case '6':
    					//recognize as valid selection but do nothing
    					break;
    				default:
    					System.out.printf("%c\n",7);
    					System.out.println("Invalid Selection");			
    			}//end switch
    		}while( selection != '6');			
    	}//end main()
     
    	/**
    	 * uses println() to ask questions, then use scanner to read the answers
    	 * once there is all the answers to build a Student object, use the Student's class constructor to build an object and save that object in the arraylist
    	 * @param firstName, lastName,  SNumber,  major, gpa 
    	 * 
    	 */
    //	Java does not allow us to pass methods into methods, but allow us to pass an object into a method and then invoke the object's methods
    	public void AddS()//Object Student, String firstName, String lastName,  int SNumber,  String major, double gpa
    	{
    		Scanner keybd = new Scanner (System.in);
    		//new operator invokes the object constructor
     
     
    		//then call setFirstName method which takes input as the variable
    		System.out.println("enter firstname");
    		String firstName = keybd.nextLine();
     
     
    		//pass firstname into this method, this object now have action and behavior
     
    		System.out.println("enter lastname");
    		String lastName = keybd.nextLine();
     
    //		System.out.println(lastname);
     
    		System.out.println("enter studentnumber");
    		int SNumber = keybd.nextInt(); 
     
    		//call setSNumber method and pass value of sNumber into it
     
    		System.out.println("enter major");
    		String major = keybd.next();
     
    //		System.out.println(major);
     
    		System.out.println("enter gpa");
    		double gpa= keybd.nextDouble();//read input as a double
     
     
    		//create a new student object		
    		Student student = new Student(firstName, lastName,  SNumber,  major, gpa );
    		list.add(student);
     
    		System.out.print(student);//why is it oly printing first name		
     
    	}
     
    	//static means there is only 1 copy of it, such as whenever you use that class and its static members, you are using the same 1 copy of its properties.
    	/**
    	 * find a Student from the ArrayList by typing out their firstname, so if it matches anyone from the ArrayList, print that student.
    	 */
    	public void FindS()
    	{
    		//keyboard can be accessed anywhere since it has been declared public.
    		System.out.print("please enter Student Number");
    		int ssn = keybd.nextInt();
    //		Find Student using SSN not firstname
    		for (Student people: list){
    			if (ssn==people.getSNumber()){
    				System.out.print(people);
    			}
    		}
    	}
    	/**
    	 * Delete a student from the ArrayList by name, so if the user input equals the ArrayList student first name, then remove the student.
    	 */
    	public void DeleteS()
    	{
    //		LOOK AT THE JAVA API TO LOOK AT BUNCH OF REMOVE METHODS for ArrayList
    		System.out.print("please enter Student Number");
    		int ssn = keybd.nextInt(); 
    		for(Student people : list)
    		{
    			if(ssn == people.getSNumber())
    			{
     
    		}
    		}
    	}
    	/**
    	 * Display all the students in the ArrayList
    	 */
    	public void DisplayS()
    	{
    		//using enhanced for loop to go through the list of an ArrayList, then print each student .
    		for (Student people: list){
    			System.out.println(people);
    		}
    	}
    	/**
    	 * display total number of students
    	 * print the number of student in the ArrayList, basically ArrayList.length (something like that)
    	 * probably requres some iteration to complete this method, a loop that go through the list and increment by 1 untill it reaches the ArrayList.length
    	 * @param student1 
    	 */
    	public void TotalS()
    	{
     
    		System.out.print(Student.count);
    	}
     
    //	public StudentList (String fn, String ln, int sn, String mj, double gpa)
    //	{
    //		Student student1 = new Student ("fn", "ln", 5555, "mj", 3.5); 
    //		System.out.print(student1);
    //	}
     
    }
    package student;
     
    public class StudentApp {
    public static void main (String[] args){
    	StudentList student = new StudentList();
    	student.menu();
    }
     
    }

    this is my updated work, now my final and last two questions are i do not know how to remove the student from the list, in the DeleteS() method, how do i do that?
    second question is, when I am printing out the student, from the list, it is only showing their firstName, and not lastname, snumber, major, and gpa, is my getters in the toString method wrong? please help

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    how to remove the student from the list, in the DeleteS() method,
    Look at the API doc for the ArrayList class, The method to use would be listed there.
    the toString method wrong?
    Check the API doc for the method you are using to format the String that is returned.
    I suggest that you write a 5 line testing program that uses the method in various ways to see how to use it.
    Change the format and the data to print and try it several ways to find the one you like.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help me first time user, and need help with a method

    public void RemoveS()
    	{
    		//LOOK AT THE JAVA API TO LOOK AT BUNCH OF REMOVE METHODS for ArrayList
    		System.out.print("please enter Student Number");
    		int ssn = keybd.nextInt(); 
    		for(Student people : list)
    		{
    			if(ssn == people.getSNumber())
    			{
    				list.remove(people);
    			}
    		}
    	}
    Exception in thread "main" java.util.ConcurrentModificationException
    	at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
    	at java.util.AbstractList$Itr.next(Unknown Source)
    	at student.StudentList.RemoveS(StudentList.java:119)
    	at student.StudentList.menu(StudentList.java:57)
    	at student.StudentApp.main(StudentApp.java:18)

    why is it not working, i watched youtube on how to remove object from ArrayList, and read it on api, but it is still giving me an error, please tell me whats wrong? thank you very much

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Please help me first time user, and need help with a method

    The foreach for statement uses an iterator that doesn't want the list changed as it goes through the list. Read the API doc for the error message, it's a class.
    Use the original style for loop with a call to get() and it should work.
    Will there ever be more than one object found and removed in the loop? If not, break from the loop as soon as the object has been removed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Asking for user input in the main method?
    By HeroProtagonist in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 19th, 2012, 07:00 AM
  2. help me with readFileAttributes() method, Time question
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 2nd, 2012, 04:10 AM
  3. Prime Factorization Method: Won't reproduce all Factors every time
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2011, 06:16 AM
  4. execute a method over a maximum period of time
    By PedroCosta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 24th, 2011, 03:06 PM
  5. [SOLVED] Help with getting user input for my factorial method
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 02:24 PM