Array setting length and storing information

Go To StackoverFlow.com

1

This is homework. I'm trying to work with arrays and this is the first project working with them. My book shows all kinds of examples but the way they code the examples doesn't do any justice to what the assignment calls for.

I am trying to write a program that asks a user to enter students into the system. The program first asks how many you will enter then it will prompt you for the first name, last name, and score.

What I am trying to accomplish with this section of code is to ask the user how many students they will enter. The line of code that says

    getStudentCount();

is a method that collects that information and then returns studentCount

I tried to code this to where the length of the array is going to be the number the user enters but it's not working so I wanted to ask for guidance. Ideally if this works and the user enters 3 then you will be prompted to enter the information 3 times. If the user enters 0 then the program doesn't ask you anything.

    public static void main(String[] args)
{

    System.out.println("Welcome to the Student Scores Application.");

    int studentCount = 1;

    getStudentCount();
    studentCount = sc.nextInt();

    String [] students = new String[studentCount];
    for (int i = 0; i < students.length; i++)
    {

        Student s = new Student();
        String firstName = getString("Enter first name: ");
        s.setFirstName(firstName);

        String lastName = getString("Enter last name: ");
        s.setLastName(lastName);

        int score = getScore("Enter score: ");
        s.setScore(score);


    }

}

Everything I had in the program worked up until I tried to code

    String [] students = new String[studentCount];
    for (int i = 0; i < students.length; i++)

which tells me there is something wrong with the way I am doing this.

Also the assignment asks that I store the information in the array. I'm not clear on how to call it or I guess store it.... I have another class with setters and getters. Is that enough to store it? How would I call it? Again this is homework so any guidance is appreciated. Thanks!

2012-04-04 00:23
by Jeremy B
Did you get an error of some kind? What went wrong - Louis Wasserman 2012-04-04 00:25
@LouisWasserman I get two issues. not errors. Right now when I run the program it welcomes the user, asks them for the number of students to enter and then does nothing. Doesn't prompt them to enter a name or anything. The other issue was before I attempted to create the array. The program would ask how many entries but if you typed 2 or 3 it only asked once so it wasn't getting the information from the getStudentCount() metho - Jeremy B 2012-04-04 00:29


4

Well that's at least a homework example that shows some work on the part of the askee (and nicely written), so here's some help:

You set the studentCount to 1 and then call getStudentcount(), but never assigns the return value to your variable, hence the variable stays 1 (though you're overwriting it afterwards with sc.nextInt() which is probably not what you want if you already have a nice method for it). The fix is just to assign the return value of your method to the variable [1]

[1] Yes I know I shouldn't answer homework questions completely, but I really saw no way whatsoever to answer that only partially - proposals welcome though :)

2012-04-04 00:29
by Voo
Am I correctly storing the information in the array? or at least am I on the right track? Since I just learned about this and I'm coding it for the first time I want to make sure I'm not doing this wrong - Jeremy B 2012-04-04 00:38
"... but I really saw no way whatsoever to answer that only partially - proposals welcome though". See my answer ... grasshopper :- - Stephen C 2012-04-04 00:41
@Stephen Learning from the master ;) already upvoted your - Voo 2012-04-04 00:48
@Jeremy Well there's another quite similar bug in there, cough look carefully how you assign something to your array (well I'm working on the "not directly answering part" ;) - Voo 2012-04-04 00:49


3

My hints:

  1. Look carefully at how you are getting the student count, and where you are putting it. You seem to be getting the count in two different ways, and that is at best redundant, and probably a bug.

  2. Presumably there is a stack trace. Read it!!

    The stack trace will tell you what exception was thrown, what its diagnostic message is, where it was thrown, and where in your code it was executing when the problem happened.

  3. If you are having difficulty visualizing what is going on, use your Java IDE's debugger and single step the program.


Based on your follow-up comments, there isn't a stack trace. But if there was one, you should read it :-)

2012-04-04 00:34
by Stephen C


0

I don't understand your lines

getStudentCount();
studentCount = sc.nextInt();

if getStudentCount() "returns" the number they input (as you describe in your text), seems like you should be doing

int studentCOunt = getStudentCount();
2012-04-04 00:32
by user949300


0

I'm trying not to completely do your homework for you, but take a look at the code below for a glimpse of how to do this:

    /* Some Declarations You're Going to Need */
private static Scanner scan = new Scanner(System.in); //Private, only need it in here
public static Student[] students; //Public, access it from any class you may need

public static void main(String[] args) {
    System.out.println("Welcome to the Student Scores Application.");
    System.out.print("Amount of students:");
    int studentCount = scan.nextInt();
    students = new Student[studentCount];
    for (int i = 0; i < students.length; i++) {
        students[i] = new Student();

        System.out.print("Enter first name:");
        String firstName = scan.next();
        students[i].setFirstName(firstName);

        System.out.print("Enter last name:");
        String lastName = scan.next();
        students[i].setLastName(lastName);

        System.out.print("Enter score:");
        int score = scan.nextInt();
        students[i].setScore(score);
    }
    scan = null; //Done with scanner
}
2012-04-04 01:02
by Anthony S.
Ads