Hello, I'm Mirza and I'm in the first semester of an IT university. I like to experiment with Java, and occasionally I run into problems. Now, I've been visiting daniweb for quite a while now, and I've always found solutions to my problem, but this particular problem I just can't seem to solve/find the solution for.

The goal:
Write a program where the user can specify how many subjects a student has.
Input names of subjects.
Input grades ofsubjects.
Output subject - grade list.
Output average score.

The problem:
The 18th line gets skipped while the rest of the code gets executed correctly.

The code:

package fakultet;

import java.util.Scanner;

public class Fakultet {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        Student theStudent = new Student();

        System.out.println("Input number of subjects: ");
        theStudent.subjectNum = input.nextInt();
        theStudent.grades = new int[theStudent.subjectNum];
        theStudent.subjectNames = new String[theStudent.subjectNum];

        for (int i=0; i<theStudent.grades.length; i++){
            System.out.println("Input name for subject "+i+":");
            theStudent.subjectNames[i] = input.nextLine();
            System.out.println("Input grade for subject "+i+":");
            theStudent.grades[i] = input.nextInt();
        }

        for (int i=0; i<theStudent.grades.length; i++){
            System.out.println(theStudent.subjectNames[i]+" - "+theStudent.grades[i]);            
        }
        System.out.println("Average score: "+avgScore(theStudent.grades));
    }

    public static int avgScore(int[] gradeList){
        int score = 0;

        for (int i=0; i<gradeList.length; i++){
            score = score + gradeList[i];
        }
        score = score / gradeList.length;
        return score;
    }
}

I've also made an object:

package fakultet;

public class Student {
    int subjectNum;
    String[] subjectNames;
    int[] grades;   

}

Edited 3 Years Ago by Pobunjenik: typo

You're mixing nextInt and nextLine, which is a bad combination.
nextInt takes the integer value but leaves the end-of-line delimiter (\n). Then nextLine takes everything up to the end of line, is a string of zero length. You need to take or skip the end of line after a nextInt before you take the next "real" line.
Or. Only use nextLine and parse the ints etc yourself.

Could you put that in code?
I'm a noob at java, and "parsing" is a first one for me. :|

Parse: take some data in the form of a string and convert it into an integer or whatever. Java provides standard ,methods for doing that, eg Integer.parseInt(String s) takes a string and interprets it as an integer, and returns that value as an int. It's what nextInt is doing behind the scenes.
Personally I prefer to read all my input with nextLine, then use parseInt, and other methods like it, to interpret the data. That avoids problems like the one you have with nextInt and nextLine. It also works well if you stop using a Scanner and enhance your app with a GUI dialog that prompts for input.

Kk, thanks a lot, now I understand parsing.

As for GUI, we'll be doing that in the next semester, so right now we're keeping it at code level only.

well ... as for difference between "gui" and "code level" ... you do realize you 'build' your gui by writing code heh? :)
the best thing for you at this point is to learn to work with the api's, whose main page you can find here.
For all the classes in the jre you can find a list there of what methods are offered and what those methods actually do.
trying to write code for gui's and their events (using listeners and such) before you know how to read those, and how to use them in your benefit, is in most cases asking for trouble.

I've experimented with GUI, made a pretty simple calculator type program, so I know that's code too. Maybe I've misspoken, I meant hard coding, just excercise stuff. :D

Anyways, I'll get on the APIs ASAP! Thanks, oh Gurus of the Code!

Acording to the pros, much like this:

        String parseFromThis = "";
        for (int i=0; i<theStudent.grades.length; i++){
                System.out.println("Input name for subject "+i+":");
                theStudent.subjectNames[i] = input.nextLine();
                System.out.println("Input grade for subject "+i+":");
                parseFromThis = input.nextLine();
                theStudent.grades[i] = Integer.parseInt(parseFromThis);
            }           

I THINK this could also work:

        for (int i=0; i<theStudent.grades.length; i++){
                System.out.println("Input name for subject "+i+":");
                theStudent.subjectNames[i] = input.nextLine();
                System.out.println("Input grade for subject "+i+":");
                theStudent.grades[i] = Integer.parseInt(input.nextLine());
            }

Edited 3 Years Ago by Pobunjenik: typo

Comments
You're welcome :-) Both are correct.

Yes, the second version is OK too - it's the one I would use because it's clearer than the version with the extra String variable

This question has already been answered. Start a new discussion instead.