0

I currently have my code set up so a student's name and subject is added to my arraylist, and then printed when user is done. How ever as I'm not wanting to add a string now, I want to add a student number, i'm unfamiliar with how to go about this. I have tried replacing set with add, and string with int, but to no prevail.

Here is my main code

import java.util.*;

public class StudentData 
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        ArrayList<Student> studentList = new ArrayList<Student>();
        String yesNo = "true";
        do
        {
            System.out.println("Enter student's name: ");
            String name = in.next();
            System.out.println("Enter student's subject: ");
            String subject = in.next();
            System.out.println("Enter student's number: "); 
            int number = in.nextInt();
            Student s = new Student(name,subject,number);   
            s.setName(name);
            s.setSubject(subject);
            s.Number.add(number);
            studentList.add(s);


            do
            {   
                System.out.println("Would you like to enter data for another student? Yes/No ");
                yesNo = in.next();
                }
            while (!yesNo.equalsIgnoreCase("YES") && !yesNo.equalsIgnoreCase("NO"));
            }   
        while (yesNo.equalsIgnoreCase("YES"));


        for(int i = 0; i < studentList.size(); i++)
        {
            System.out.println(studentList.get(i).getName());
            System.out.println(studentList.get(i).getSubject());
            }
        }
    }

and

class Student 
{
    private String studentName;
    private String studentSubject;
    public Student(String name, String subject, int number )
    {
        setName(name);
        setSubject(subject);
        Number.add(number);
        }
    public String getName()
    {
        return studentName;
        }
    public void setName(String name)
    {
        studentName = name;
        }
    public String getSubject()
    {
        return studentSubject;
        }
    public void setSubject(String subject)
    {
        studentSubject = subject;
    }
    public int getNumber()
    {
        return studentNumber; 
        }
    public void Number.add(int number)
    {
        studentNumber = number;
    }
}
3
  • what do you mean an integer. ? Would you be able to elaborate a bit more? Thanks in advanced Commented Dec 3, 2013 at 0:10
  • It doesn't look like your Student class has a place to store the student number. And what's Number.add? Commented Dec 3, 2013 at 0:14
  • 2
    It looks like when you create each Student, you don't actually need to explicitly call s.setName(name) and those other setters, because the constructor for Student automatically handles that. Commented Dec 3, 2013 at 0:22

1 Answer 1

2

As you are storing Student objects in your list you are also storing the member variables of each object as you entered them. So no different approach needs to be taken to store an integer.

You can declare your private int studentNumber; in your student class, add a getter and a setter for it and then modify your constructor so that setStudentNumber(number); would work in the same way as setting your two Strings up.

Then to iterate through your list you could make use of the 'enhanced-for' syntax instead of a plain old for loop, meaning:

for (Student s : studentList) {
    System.out.println(s.getName());
    System.out.println(s.getSubject());
    System.out.println(s.getStudentNumber());
}

Hope that this helps, if you need anything more just give me a shout below.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm curious, is it necessary for there to be new ArrayList<Integer> or is the Integer class not required there?
Thanks for your response, but if i was to set it up like List<Integer> studentNumbers = new ArrayList<>(); wouldn't it be invalid for the other inputs im also using it for? ie Strings
@Bucco Most IDEs now flag new ArrayList<Integer>(); as a redundant type argument so it's not really necessary.
@user3050340 I slightly misunderstood your question then it seems, could you clarify what exactly you would like stored in your list?
Previously i had the code so it only stored student name and subject, I now want to add the students number to the same array but, so i would like to store a number in the array. Hope thats clear, thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.