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 Fat 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 FTotal 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);
}
}