Search code examples
javamethodsreturn

Method never reaching an if statement despite, its conditions being met


I'm trying to scan through a txt.file with name and gender and my code is supposed to take the user input of a name and gender, read through the txt file to see if that combination exists, and then either return the information (if there is a match) or say there was no match. I put place holder S.O.P statements just to help debug and it shows that it reaches the return statement where it's supposed to return the info from the txt file because it's found that match, but the method fails to pass the if statement where it returns the user info once there is a match, even if the info the user puts in is one that is supposed to have a match. The code is here :

public class Draft {

   public static void main(String[] args) throws FileNotFoundException {
      Scanner console = new Scanner(System.in);
      Scanner input = new Scanner(new File("names.txt"));

      ProgramIntro();

      System.out.print("name? ");
      String userName = console.nextLine();
      System.out.print("sex (M or F)? ");
      String userGender = console.nextLine();

      System.out.print(searchInfo (input, userName, userGender));  
   }    

   //searches file for user input match and returns value depending on whether a match exists
   public static String searchInfo (Scanner input, String userName, String userGender) {
   //goes through the file until there are no more entries
      while (input.hasNextLine()) {
         System.out.print("blach");
         //concatenates one line of file into one string
         String line = input.nextLine();
         //turns the focus into just one line
         Scanner lineScan = new Scanner(line);
         //runs loop just on one single line
         while (!lineScan.hasNextInt()) {
            System.out.print("bafdjkf");
            //sets the first thing in a line (the name) as String babyName
            String babyName = lineScan.next();
            System.out.print(babyName);
            //sets the first thing in a line (the name) as String gender
            String gender = lineScan.next();
            System.out.println(gender);
            if (userName.equalsIgnoreCase(babyName) && userGender.equalsIgnoreCase(gender)) { //THE PROBLEM LINE
               System.out.print("jgfgfgfgfgfg");
               System.out.println(line);
               return line;
            }
         }
      } 
      return "name/sex combination not found";            
   }

And it never reaches the if statement because the placeholder S.O.P in the if statement never prints.

blachbafdjkfCaleighF //Caleigh is the name we tested 
blachbafdjkfRisaF
blachbafdjkfRoninM
blachbafdjkfFronaF
blachbafdjkfDanaF
blachbafdjkfJesusM
blachbafdjkfHarleyM
blachbafdjkfJadaF

Where is the logic error in this program?


Solution

  • How come the program goes back to the first while loop and starts scanning through the rest of the names in the list despite finding a match?

    Because you call that method twice:

    searchInfo (input, userName, userGender);
    System.out.print(searchInfo (input, userName, userGender));
    

    You have to either skip the first call or store the result of the first call in a variable and instead of calling the method a second time print that variable.