Problem: I'm having trouble in this part of my code

if (sexCode[ctr] == 'F') 
            totalF++;
      else 
            totalM++;

I want my program to determine how many females and males in the list but whenever I execute my program I always get an output with an error that looks like this:

*the ouput actually have proper spacing but when I try to paste it here and Quote it, it removed the spaces :) between the elements.

List of Students
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
Student No. Name Course Sex
397-1010 R.Santos BSIT M
380-1120 A.Cruz BSCS M
390-1350 J.Lim BSIT F
345-1235 S.Corona ACT F

at studlist.StudList.main(StudList.java:33)
Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

I want an output that looks like this but with the no. of females and males already determined not 0. Pls. can anyone help me?

List of Students

Student No. Name Course Sex
397-1010 R.Santos BSIT M
380-1120 A.Cruz BSCS M
390-1350 J.Lim BSIT F
345-1235 S.Corona ACT F

Total Records Printed: 4
No. of Females: 0
No. of Males : 0

here's my code:

package studlist;

public class StudList {

    public static void main(String[] args) {
        //Variable Declaration
        
        int totalRec = 0, totalF = 0, totalM = 0, ctr;
           
    String[] studNo = {"397-1010", "380-1120", "390-1350", "345-1235"};
    String[] studNa = {"R.Santos", "A.Cruz  ", "J.Lim   ", "S.Corona"};
    String[] course = {"BSIT", "BSCS", "BSIT", "ACT"};
    char[] sexCode = {'M', 'M', 'F', 'F'};
    
    //Print Titles & Subtitles
    
    System.out.println("           List of Students");
    System.out.println();
    System.out.print("Student No.\tName\t\tCourse\t\tSex \n");
    
    //Print Student Records
    
    for (ctr = 0; ctr < studNo.length; ctr++)
    {
        System.out.print(studNo[ctr] + "\t");
        System.out.print(studNa[ctr] + "\t");
        System.out.print(course[ctr] + "\t\t");
        System.out.print(sexCode[ctr] + "\n");
    }
    System.out.println();


      if (sexCode[ctr] == 'F') 
            totalF++;
      else 
            totalM++;
      
      totalRec = ctr;  
        //Print Summary
   
   System.out.println("Total Records Printed: " + totalRec);
   System.out.println("No. of Females: " + totalF);
   System.out.println("No. of Males  : " + totalM);

    }
}

Edited 4 Years Ago by Ashenvale: to make it more readable

It's generally bad form to use the index of a 'for' loop, like 'ctr', outside the loop.

Your problem is that your four-line "if" statement is outside the loop. It executes only once.


('totalRec = ctr;' is correct. But I might suggest using 'totalRec = studNo.length;' instead.)

the problem isn't that it only executes once, the problem is, that when it executes, ctr = studNo.length, which means that that index doesn't exist.
you're trying to access
sexCode[4]
while
sexCode[3]
is the last element.

but I agree with Jeff's statement, that your if-else should either be placed within the for loop you already have, or within a second one, depending on the output you want.

:D great thanks, haha! you're 100% right XD thank you so much for replying... actually this is the lecture I copied from the board I guess even profs make mistakes

I guess even profs make mistakes

either that, or the assignment was to figure out why that exception occured in the first place :)

nope :) he didn't give us any assignment this was given as an example while he was explaining to us how to use array :D

can always happen ...
I remember one of my College professors, who handed us 'solutions' to our assignments.
off course, he didn't mean to say that "our work was not 'good' enough to study, he only wanted every one of us to have an application that would work and was efficiënt coded"

I tried it, it wouldn't even compile :)

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