1

So I'm still fairly new to Java and it's also been a while since I've programmed in Java. What I want know is how to assign an integer, like a student number, to an element in an array list. I've got three elements in my arraylist, and each element has to have a 7 digit student number with the 7th digit as the next number in sequence (ex. el 1 with student number 1234567, el 2 with 1234568, el 3 with 1234569). I'm not sure if I'm on the right track or not. This code is a work in progress and there are many things I still have to add or fix. My arraylist is in the next code block titled StudentApp.

public class Student    {

    //fields
    private String firstName;
    private String lastName;
    private int sNumber;
    private String major;
    private double gpa;

    private static int count()  {
        int count = 1234567;
        for (int i = 1; i <= count; i++)    {
            System.out.print(count);
        }
        return count;
    }

    //constructors
    public Student()    {
        firstName = null;
        lastName = null;
        sNumber = 1234567;
        major = null;
        gpa = 0.0;

    }

    public Student(String fName, String lName, int sNumber, String maj, double gpa) {
        firstName = fName;
        lastName = lName;
        sNumber = sNumber;
        major = maj;
        gpa = gpa; 
    }

    //methods

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getsNumber() {
        for(int sNumber = 0; sNumber >= 7; sNumber++)   {
            int sNumber = 1234567;
            sNumber++ 
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }

    @Override
    public String toString() {
        return "S" + sNumber + " " + firstName + " "+ lastName + " " + "(" + major +")" + " " + "gpa:" + gpa;
    }
}

import java.util.Scanner;
import java.util.ArrayList;

public class StudentApp {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Student> names = new ArrayList<Student> ();

        Student student1 = new Student();
        student1.setFirstName("Brian");
        student1.setLastName("Scholl");
        student1.setMajor("CS");
        student1.setGpa(3.5);

        Student student2 = new Student();
        student2.setFirstName("Emily");
        student2.setLastName("Davies");
        student2.setMajor("ME");
        student2.setGpa(3.7);

        Student student3 = new Student();
        student3.setFirstName("Sarah");
        student3.setLastName("Dixon");
        student3.setMajor("EE");
        student3.setGpa(3.8);

        names.add(student1);
        names.add(student2);
        names.add(student3);

        int choice;
        do{
            displayMenu();
            choice = input.nextInt();
            switch(choice)  {
            case 1:
                Student student4 = new Student();

                System.out.print("First name: ");
                String fName1 = input.nextLine();
                System.out.println();
                System.out.print("Last name: ");
                String lName1 = input.nextLine();
                System.out.println();

                System.out.print("Major: ");
                String major1 = input.nextLine();
                System.out.println();
                System.out.print("GPA: ");
                double gpa1 = input.nextDouble();

                student4.setFirstName(fName1);
                student4.setLastName(lName1);
                student4.setMajor(major1);
                student4.setGpa(gpa1);
                names.add(student4);
                break;
            case 2:

                System.out.print("Find student with sNumber S");
                input.nextInt();
                student1.getsNumber();
                //statement on finding a student
                break;
            case 3:
                //statement on deleting a student
                break;
            case 4:
                System.out.println(student1);
                System.out.println(student2);
                System.out.println(student3);
                break;
            case 5:
                names.size();
                break;
            case 6:
                System.out.println("Good bye");
                break;
            default:
                System.out.println("Invalid Selection");
                System.out.println();
                break;
            }
            System.out.println();
        }while(choice != 0 );


    }

    public static void displayMenu()    {
        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. Display the total number of students");
        System.out.println("6. Exit");
    }

}
5
  • 2
    I don't see an array in your code... and that loop is doing nothing. Commented Sep 8, 2016 at 23:00
  • Just add a variable initialized to the initial student number then increment it every time you add a new student. You an also save yourself some code by utilizing the constructor you wrote. names.add(new Student(...)); Commented Sep 8, 2016 at 23:04
  • @alfasin when I scroll down theres a arraylist in the main Commented Sep 8, 2016 at 23:24
  • @OlegSklyar, based on the code it's pretty clear your "answer" is incorrect. They don't have a list of integers but even if they did that would not achieve what they're looking for. Commented Sep 9, 2016 at 0:26
  • @ChiefTwoPencils As you know I have not provided any answer in SO terms, my was an early comment to an incomprehensible question, comment based on the title. And I am happy to remove it as incorrect :) Commented Sep 9, 2016 at 0:32

2 Answers 2

2

Add static attribute (studentNumberSequence) to the Student class with initialize value (e.g 1234567) and increase it's value in constructors. You can put the following line in constructors:

sNumber = studentNumberSequence++;

I suggest you to use Map (e.g HashMap) instead of ArrayList and apply 'sNumber' as the map key.

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

2 Comments

Apart from the fact that this will reset on every application restart and that it should be volatile that will do.
Agree but this is an elementary application and need an elementary solution :)
0

There's no reason to try to do this the way you are. Consider that you can simply define an initial student number; according to your code I would assume this would be int studentNumber = 1234567;.

Now, when you add a student to the list don't neglect to add that number or initialize all students with the same number; that's just going to over complicate things.

Given that you now have a number, get the other information from the user and add a new Student with those values plus the student number that you assign for them. For example:

names.add(new Student(firstName, lastName, studentNumber++, major, gpa));

Additionally, you have a number of other issues that I won't address here but warn you about.

  1. The count should not come from a student instance.
  2. Getting the student number should just give the student number (now that you have that figured out)
  3. Take care when you're naming constructor parameters the same as the instance variables; you need to distinguish between them somehow.
  4. You can't hardcode the outputting of all students; they can add more.

5 Comments

If you point out to code issues start with design: the above needs a total redesign first and not a couple of fixes here and there as the author suggests. Supplying technical Ids into the constructor is not a brilliant idea IMHO. Ideally one should have a separate and persistent Id service that every constructor such entities calls to get a new unique Id. Otherwise just use UUID, but again internally.
@OlegSklyar, a total redisign? There's hardly any design at all. It's a single class with basic members. Most of the mistakes in the class are due to their problem with the student number, others are not a matter of design but misunderstanding. As far as the app goes, sure, it's not ideal but obviously a student's effort and most likely limited or guided by the topics learned thus far which I have no knowledge of so I'm not going to go way outside the bounds of the question.
@OlegSklyar, to add more after your edit, I think you should consider the question and save your input for a question requiring such mechanisms; this clearly isn't it nor does this question begin to suggest or explicitly ask for it. Thanks.
Well, I do not like your answer whichever way you look at it. For a simple solution a static counter as suggested elsewhere is better. At the same time, pointing out other issues you seem to be aiming to help the OP to a better solution, but then you miss the elephant in the room. Either do not point out to further issues or consider the most serious first as otherwise they will never get addressed. A single class of mess is still a design decision, so yes, it needs a redesign.
@OlegSklyar, that's fine. However, saying the other recommendation is "better" is simply opinion and as your comment there suggests it's limited and with small variations would need to also be redesigned. I'm pointing out things that are incorrect. I'm answering the question first; if you don't like my approach exercise your vote and move on.

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.