user2517185
user2517185

Reputation: 9

Looping an array

  void searchForPopulationChange()
  {
     String goAgain;
     int input;
     int searchCount = 0;
     boolean found = false;

     while(found == false){
        System.out.println ("Enter the Number for Population Change to be found: ");
        input = scan.nextInt();



        for (searchCount = 0; searchCount < populationChange.length; searchCount++)
        {
           if (populationChange[searchCount] == input)
           {
              found = true;
              System.out.print(""+countyNames[searchCount]+" County / City with a population of "+populationChange[searchCount]+" individuals\n");
           } 

        }


     }
  }

}

hello! I am working on a method that will take an users input, lets say (5000) and search a data file with those corresponding numbers. and return the corresponding number, and county that it corresponds with.

However, I am able to get this code to run to return the correct value, but i am unable to get it to run when i enter an "incorrect" value.

Any pointers? Thank you!

Upvotes: 0

Views: 119

Answers (1)

Mark M
Mark M

Reputation: 1600

It's a bit unclear, but I assume you want something to handle if the input is incorrect (not an integer)? Use hasNextInt so you will only capture integers.

Scanner scanner = new Scanner(System.in);
while (!scanner.hasNextInt()) {
    scanner.nextLine();
}
int num = scanner.nextInt();

This will keep looping the input until it is a valid integer. You can include a message in the loop reminding the user to input a correct number.

If you want something to display if your number has no match inside of the array, simply add code after your for block, if found == false. For example:

for (searchCount = 0; searchCount < populationChange.length; searchCount++)
    {
       if (populationChange[searchCount] == input)
       {
          found = true;
          System.out.print(""+countyNames[searchCount]+" County / City with a population of "+populationChange[searchCount]+" individuals\n");
       } 

    }
if (found == false) {
     System.out.println("Error, No records found!");
}

Since found is still false, your while loop kicks in and prints your line requesting for input again.

EDIT: Since you seem to have problem adding these two concepts to your code, here's the whole function:

void searchForPopulationChange() {
 String goAgain;
 int input;
 int searchCount = 0;
 boolean found = false;

 while(found == false){
    System.out.println ("Enter the Number for Population Change to be found: ");
    Scanner scanner = new Scanner(System.in);
    while (!scanner.hasNextInt()) {
      scanner.nextLine();
      }
    input = scanner.nextInt();

    for (searchCount = 0; searchCount < populationChange.length; searchCount++)
    {
       if (populationChange[searchCount] == input)
       {
          found = true;
          System.out.print(""+countyNames[searchCount]+" County / City with a population of "+populationChange[searchCount]+" individuals\n");
       } 

    }

    if (found == false) {
       System.out.println("Error, No records found!");
    }
  }
}

Upvotes: 2

Related Questions