Home > Software engineering >  How can I get the for loop to stop to stop running and proceed to the next methid once the program h
How can I get the for loop to stop to stop running and proceed to the next methid once the program h

Time:04-05

The code is first getting the user to input the number of students in a class. Then the system will allow the user to add the names of those students. After which the user can assign a mark and credit hours for subjects for each of those students. Once the marks and credit hours have been entered for each student, the system should then calculate the GPA. However, the system continues to run the loop and it does not execute and display the GPA. Can I be assisted as the why this loop does not execute:

public class testing1 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int result;
        String[] names = new String[100];
        System.out.println("Enter number of students (Must be between 1-50)");
        result = in .nextInt();
        System.out.println("Thank you, Please enter the names for the "   result   " Students");
        int nameCount = 0;
        while (nameCount < result) {
            System.out.println("Enter First Name: ");
            String s1 = in .next();
            System.out.println("The students name is"   " "   s1);
            System.out.println();
            names[nameCount] = s1;
            nameCount  ;
        }
        double[][] marks = new double[100][2];
        int[][] credits = new int[100][2];
        double[] gpa = new double[100];
        String[] subjects = {"Math", "Science",};
        int cnt = 0;
        Scanner s1 = new Scanner(System.in);
        gpa[cnt] = 0;
        int totalCredits = 0;
        for (int i = 0; i < names.length; i  ) {
            for(int j = 0; j < subjects.length; j  ) {
                System.out.println();
                System.out.println();
                System.out.println("Please enter "   names[i]  "'s mark in "    subjects[j]   ": ");
                marks[cnt][i] = s1.nextDouble();
                System.out.println("Please enter "   names[i]  "'s Credit hours for "  subjects[j]   ": ");
                credits[cnt][i] = s1.nextInt();
                totalCredits  = credits[cnt][i];
                gpa[cnt]  = marks[cnt][i] * credits[cnt][i];
            }
            cnt  ;
        }
        gpa[cnt] /= totalCredits;
        System.out.printf("GPA of :"   names[cnt]   " is %.2f.", gpa[cnt]);
        return;
    }
}

Below is the output when I run the system from the beginning:

Enter number of students (Must be between 1-50)
2
Thank you, Please enter the names for the 2 Students
Enter First Name: 
Alan
The students name is Alan

Enter First Name: 
Bob
The students name is Bob



Please enter Alan's mark in Math: 
12
Please enter Alan's Credit hours for Math: 
12


Please enter Alan's mark in Science: 
12
Please enter Alan's Credit hours for Science: 
12


Please enter Bob's mark in Math: 
12
Please enter Bob's Credit hours for Math: 
12


Please enter Bob's mark in Science: 
12
Please enter Bob's Credit hours for Science: 
12


Please enter null's mark in Math: 
12
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
    at testing1.testing1.main(testing1.java:59)

CodePudding user response:

Update due to the question has changed

In the first loop you're iterating over the whole array of names. You also need a i < nameCount condition because the arrays have data from the begining until as many students as the user has entered. So, the condition will be as follow:

//other code
for (int i = 0; i < names.length && i < nameCount; i  ) {
    //other code
}

If you want to stop a loop, then you can set to true some condition variable and break the loop depending on its state. Something like:

for (...) {
    //sentences
    if (someCondition)
        break;
}

CodePudding user response:

Notes after the code.

import java.util.Scanner;

public class Testing2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of students (between 1-50): ");
        int result = in.nextInt();
        in.nextLine();
        String[] names = new String[result];
        System.out.println("Thank you, please enter the names for the "   result   " students.");
        for (int i = 0; i < result; i  ) {
            System.out.printf("Enter student %d first name: ", (i   1));
            names[i] = in.nextLine();
        }
        double[] gpa = new double[result];
        String[] subjects = {"Math", "Science",};
        double[][] marks = new double[result][subjects.length];
        int[][] credits = new int[result][subjects.length];
        for (int i = 0; i < result; i  ) {
            int totalCredits = 0;
            for(int j = 0; j < subjects.length; j  ) {
                System.out.printf("Please enter %s's mark in %s: ", names[i], subjects[j]);
                marks[i][j] = in.nextDouble();
                System.out.printf("Please enter %s's credit hours for %s: ", names[i], subjects[j]);
                credits[i][j] = in.nextInt();
                totalCredits  = credits[i][j];
                gpa[i]  = marks[i][j] * credits[i][j];
            }
            gpa[i] /= totalCredits;
            System.out.printf("GPA of %s is %.2f%n", names[i], gpa[i]);
        }
    }
}
  • You only need one Scanner object. No need to create a new Scanner each time you want to get input from the user.
  • Declare variables when you require them.
  • You can use result to declare your names array. Then you can use a for loop to get the names.
  • No need for variable s1. Assign the value returned by method nextLine directly to the names array.
  • You can also use result when you declare the other arrays, including marks, credits and gpa. In fact, you can use any expression that returns an int result for any dimension of an array.
  • No need for variables nameCount and cnt.
  • All calculations need to be done within the nested for loops.
  • You need to reset totalCredits for each student.

Output from a sample run:

Enter number of students (between 1-50): 2
Thank you, please enter the names for the 2 students.
Enter student 1 first name: George
Enter student 2 first name: Edson
Please enter George's mark in Math: 2.0
Please enter George's credit hours for Math: 3
Please enter George's mark in Science: 4.0
Please enter George's credit hours for Science: 2
GPA of George is 2.80
Please enter Edson's mark in Math: 2.67
Please enter Edson's credit hours for Math: 3
Please enter Edson's mark in Science: 1.33
Please enter Edson's credit hours for Science: 4
GPA of Edson is 1.90
  • Related