I am trying to get the number of Male and Female using substring. but in this case I have made 2 classes and use a scanner to input how many records I want. How do I combine the the number of counts in a for loop? I have got the count of the M and F's but it did not get my expected output. I have tried to remove the for loop and just only the A[i] = Output
but it needs to be in a loop or else it may be error. If I put again the for loop it works but it was seperated "it's on the Output box".
If I am doing wrong plz let me know.
public class Input {
String info, gender;
int resultf;
int resultm;
static Scanner in = new Scanner (System.in);
public void inputted() {
System.out.print("Enter a Gender: ");
info = in.nextLine();
gender = info.substring(0,1);
}
public void Output() {
resultm = info.length() - info.replaceAll("M", "").length();
resultf = info.length() - info.replaceAll("F", "").length();
System.out.println("Male: "+resultm);
System.out.println("Female: "+resultf);
}
}
public class Output {
static Input A[] = new Input [100];
static Scanner in = new Scanner (System.in);
public static void main (String args []) {
int i, size;
//Input
System.out.print("Enter how many record: ");
size = in.nextInt();
for (i = 0; i < size; i++) {
A[i] = new Input();
A[i].inputted();
}
System.out.println();
//Print
for (i = 0; i < size; i++) {
System.out.println("Gender: "+A[i].gender);
}
System.out.println();
//Output Section
for (i = 0; i < size; i++) {
A[i].Output();
}
}
}
OUTPUT:
Enter how many record: 2
Enter a Gender: M
Enter a Gender: F
Gender: M
Gender: F
Male: 1
Female: 0
Male: 0
Female: 1
EXPECTED OUTPUT:
Enter how many record: 2
Enter a Gender: M
Enter a Gender: F
Gender: M
Gender: F
Male: 1
Female: 1