0

I've been stuck for a while now in an issue, and the program is not doing what I believe it should be doing.

When I run the program and it gets to the part where it asks you for the name of the course, the program skips the first iteration, or other depending how many courses were inputted. It only allows for input on the last iteration. For the following for loops, the program skips them without ever allowing in input.

My questions is, are the for loops incorrect or is the the String arrays that are not inputting the information correctly to their subscript?

    import java.util.Scanner;           //Needed for Scanner class
    public class StudentRecords
    {
    public static void main(String[] args)
    {
    int courses;
    int students;
    int[] course = new int[5];
    int[] student = new int[5];
    double GPA = 0;
    String[] courseNumber = new String[5];
    double[] creditHours = new double[5];
    String[] letterGrade = new String[5];

    //Scanner object for user input
    Scanner kb = new Scanner(System.in);

    System.out.println("This program will help you determine the GPA \n"
                        + "for each student entered.");
    System.out.println("");

    System.out.println("How many student's GPA are you going to calculate?");
    System.out.print("Enter amount of students (Maximum of 5 students): ");
    students = kb.nextInt();
    student = new int[students];

    System.out.println("");

    for(int index = 0 ; index < student.length; index++)
    {
        System.out.print("Student " + (index + 1) + " information: ");
        System.out.println("");

        System.out.print("How many courses did student " +
                            (index + 1)  + " take? ");
        courses = kb.nextInt();
        course = new int[courses];

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("What is the name of course #" + (i + 1));
            courseNumber[i] = kb.nextLine();
        }

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("How many credit hours is " + courseNumber[i]);
            creditHours[i] = kb.nextDouble();
        }

        for(int i = 0; i < course.length; i++)
        {
            System.out.println("What is the final letter grade for " + courseNumber[i]);
            letterGrade[i] = kb.nextLine();
        }

        for( i = 0; i < student.lenght<
    }
    }
 }

P.S. This is the question I am working on:

Write a program with the following inputs, all of which are stored in arrays (size 5). First how many courses the student took that semester (can’t be greater than 5). Store in ARRAYS for each student, the course number/name (for example ICT 435), credit hour ( 1-4), and letter grade(A-F). Determine the GPA for the semester.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
Edù
  • 9
  • 1
  • 2
    Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Arjan Aug 02 '16 at 17:02
  • @Edù you can accept the answer if it solved your problem. – Kaushal28 Aug 02 '16 at 17:21

2 Answers2

1

The nextLine() Scanner method can be kind of weird. A beginning Java course I took mentioned that, after a method retrieving a number (nextDouble(), for example), there's a new line character at the end of the line. The next time you use nextLine(), it reads that new line character as input, not giving you a chance to type anything.

If you put a nextLine() before the loop asking for the course names,

kb.nextLine(); // <-- here
for(int i = 0; i < course.length; i++)
{
    System.out.println("What is the name of course #" + (i + 1));
    courseNumber[i] = kb.nextLine();
}

it will pass that new line. Any subsequent nextLine() calls will actually let you give input. That is, You'll need to do this before the letter grade loop as well,

kb.nextLine(); // <-- here
for(int i = 0; i < course.length; i++)
{
    System.out.println("What is the final letter grade for " + courseNumber[i]);
    letterGrade[i] = kb.nextLine();
}

since you ask for numbers before this loop too.

This worked for me. Hope it helps!

Zac
  • 404
  • 7
  • 15
0

try to use:

courseNumber[i] = kb.next();

and

letterGrade[i] = kb.next();

in your for loops while scanning the strings. Instead of

courseNumber[i] = kb.nextLine();

and

letterGrade[i] = kb.nextLine();

see this link for more details:

Community
  • 1
  • 1
Kaushal28
  • 4,823
  • 4
  • 30
  • 57