Job Seekers Information Filter

Asked

Viewed 160 times

-2

A program that provides information on job applicants: The number of candidates per sex.... There is an error in this if, ta returning only m or just f

package livroex_pag69;

import javax.swing.JOptionPane;


public class Ex_09_Recrutamento {
    public static void main(String[] args){
        String sexo, exp, esc, continuar ;
        int idade, m = 0, f = 0, numCand = 1;


    do{
        idade = Integer.parseInt(JOptionPane.showInputDialog("Idade: "));
        sexo = JOptionPane.showInputDialog("Sexo [m/f]: ");           
        esc = JOptionPane.showInputDialog("Cite a escolaridade:  ");
        exp = JOptionPane.showInputDialog("Possui experiência [s/n]: ");
        numCand++;
        continuar = JOptionPane.showInputDialog("Deseja continuar? [s/n]  ");

        }while(continuar.equals("s"));

        for (int i = 1; i < numCand; i++){
             if(sexo.equals("m")){          
                 m++;           
             }else{
                 f++;
             } 


        }

 JOptionPane.showMessageDialog(null, "Número de candidatos masculino: " + m+
                                     "\n Número de candidatos feminino: "+f);     

    }

}
  • Your logic is incorrect, because the "for" will only be executed when it continues to be different from s, so there will only be one result, always return only one sex, reevaluate your while loop

  • Hello Thiago, welcome to Sopt, before starting a look at our [Tour] -- I made a change in the title and description of your question if my change does not represent your need you can reverse it by accessing this link and choosing the review that best suits you. = D

  • 1

    Obg Icaro, as there is the character limit, I get lost in this part. Valew.

2 answers

1

Your code is rewriting the same variables every time you go through your do-while, to solve your problem, you must store the values in a array, or any other kind of list if you prefer.

Currently, your impression is always the last received in the inputs.

EDIT: In case you can’t use arrays or listas, as quoted in the comment, you must change the order in which your commands happen, causing the conditional and the printing to be done inside the loop do-while. Thus, they will be printed after the inputs, with each loop iteration.

  • Hello, I can’t use arrays or lists. It should be done one by one.

  • In this case, you must enter your print conditional, within the do-while loop, so that every time you input a value, it already prints. In this case your is would be unusable as you would be printing at input time.

  • 1

    Yes, it worked. Thank you Pedro. I have other conditionals to put in the program. Valew.

  • 1

    I’m glad I helped you! If you can mark the answer as correct, I’d appreciate it. Thanks!

0

I’ll try to explain in parts what’s going on in your code.

With each interaction of this block your previously informed data is lost, except the numCand which is a counter so it is incremented

  do {
   idade = Integer.parseInt(JOptionPane.showInputDialog("Idade: "));
   sexo = JOptionPane.showInputDialog("Sexo [m/f]: ");
   esc = JOptionPane.showInputDialog("Cite a escolaridade:  ");
   exp = JOptionPane.showInputDialog("Possui experiência [s/n]: ");
   numCand++;
   continuar = JOptionPane.showInputDialog("Deseja continuar? [s/n]  ");
  } while (continuar.equals("s"));

In that other block you’re making one for based on the counter that was generated by do while, i.e., the numCand, however the variable sexo and kind String and will always contain the value of the last interaction of do while

for (int i = 1; i < numCand; i++) {
   if (sexo.equals("m")) {
       m++;
   } else {
       f++;
   }

Example:
Imagine that in the do while Voce entered the information f, m, f, m. Your variable sexo will take the value m , your accountant numCand will take the value 4.
When you get to the block for you will see 4x that the variable sexo is equal to m.

A possible solution

If you do not intend to save this data and just want to know how many people of sex m and f have been informed you can put this if within the own do while, leaving +/- like this:

  do {
   idade = Integer.parseInt(JOptionPane.showInputDialog("Idade: "));
   sexo = JOptionPane.showInputDialog("Sexo [m/f]: ");
   esc = JOptionPane.showInputDialog("Cite a escolaridade:  ");
   exp = JOptionPane.showInputDialog("Possui experiência [s/n]: ");
   numCand++;
   continuar = JOptionPane.showInputDialog("Deseja continuar? [s/n]  ");

   if (sexo.equals("m")) {
       m++;
   } else {
       f++;
   }

  } while (continuar.equals("s"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.