0

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
eugen
  • 1,049
  • 6
  • 11
Miao
  • 23
  • 4
  • Each instance of `Input` that you create will have its own copy of `resultf` and `resultm`. You can get your desired output by simply using one `Input` object and updating it over and over in your loop rather than using an `Input[]`. I would also recommend you only use the `Scanner` methods in the `Output` class and instead just increment the variables in `Input` by accessing the variable directly or with additional methods. – Mihir Kekkar Sep 17 '19 at 01:43
  • I also recommend you don't call `nextInt()` followed by calls to `nextLine()` because that can lead to [problems with skipping input](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). – Mihir Kekkar Sep 17 '19 at 01:48

2 Answers2

0

In your last loop in main you called the Input.Output base on the size of your array, and you print both of the gender on that method. Change your Input.Output on how you print the data. Try adding checking if to check if your printing male or female. Or just change the logic for printing the result.

0

You have to count it in main function, not in class Input.

public class Input {
  //add functions below
  public boolean IsMale(){return resultm == 1;}
  public boolean IsFemale(){return resultf == 1;}

  //another way without boolean
  public int GetMaleCount(){return resultm;}
  public int GetFemaleCount(){return resultf;}
}

Change the Output Section in main to below:

int male = 0;
int female = 0;
for (i = 0; i < size; i++) {
  //if(A[i].IsMale()){male ++;}
  //if(A[i].IsFemale()){female ++;}
  //another way without boolean
  male += A[i].GetMaleCount();
  female += A[i].GetFemaleCount();
}
System.out.println("Male: "+male);
System.out.println("Female: "+female);
Peter-Yu
  • 126
  • 7
  • From OPs logic `resultm` and `resultf` can never be `1` if user inputs more than one character for gender. Hence your `IsMale()` and `IsFemale()` method would always return false for similar senario. – Giddy Naya Sep 17 '19 at 03:55
  • Is there other way to do this without using Boolean? This code works. But I haven't used Boolean before so I don't know how to simulate this in my mind. – Miao Sep 17 '19 at 04:15
  • @Miao I just update the answer that without using `boolean`. – Peter-Yu Sep 17 '19 at 05:11
  • Thanks! Anyways, can i ask why ```resultm == 1``` and ```resultm == 1``` is needed to be returned if i'm using the boolean? – Miao Sep 17 '19 at 11:09
  • When you want to know the total count of gender, can't do that in each `Input` object `A[i]`, in main function, you can get all object's gender count, then just different method to count. First is to check object has male or female or none, `resultm == 1` is doing this, the second way is to return `resultm` value, add it in main function. – Peter-Yu Sep 18 '19 at 01:25