Scott P
Scott P

Reputation: 29

java user input menu can't select an option

still learning Java in school, but I am working on a program where the user inputs an option. 1,2,3,4. The problem is that one of the options I select, oddEvenZero(Option 2), doesn't end. The program keeps asking the user to input an integer and doesn't display the results and doesn't bring back the menu as well. Thanks.

import java.util.Scanner;

public class IntFun
{
  public static void main(String[] args)
  {
     int option;
     int integer;
     int evenCount = 0, oddCount = 0, zeroCount = 0;
     int optionOne;

     Scanner kb = new Scanner(System.in);
     System.out.println("Welcome to Integer Fun.");
     System.out.println("Please enter a non-negative integer: ");
     integer = kb.nextInt();
     kb.nextLine();

     while((integer < 0))
     {
        System.out.println("I am sorry that is not a non-negative integer.");
        System.out.println("");
        System.out.println("Please enter a non-negative integer: ");
        integer = kb.nextInt();
     }

     option = displayMenu(kb);

     while (option != 4)
     {
        switch (option)
        {
           case 1:
                    System.out.println("Option 1");
              break;
           case 2:
              optionOne(integer, evenCount, oddCount, zeroCount);
              System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
              break;
           case 3:
              System.out.println("Option 3");
              break;
        }
            option = displayMenu(kb);
     }
  }

  private static int displayMenu(Scanner kb)
  {
     int option = 0;
     while (option != 1 && option != 2 && option != 3 && option != 4)
     {
        System.out.println("\t\t1. Enter a new number\n\t\t2. Print the number of odd digits, even digits and zeros in the integer\n\t\t3. Print the sum of the digits of the integer\n\t\t4. Quit the program");
        option = kb.nextInt();
            kb.nextLine();
        if (!(option == 1 || option == 2 || option == 3 || option == 4))
           System.out.println("Invalid choice");
     }
     return option;
  }

  private static int optionOne(int integer, int evenCount, int oddCount, int zeroCount)
  {
  while (integer > 0)
  {
  integer = integer % 10;
  if (integer==0)
  {
     zeroCount++;
  }
  else if (integer%2==0)
  {
     evenCount++;
  }
  else
  {
     oddCount++;
  }
  }
  return integer % 10;


  }
  }

Upvotes: 1

Views: 1126

Answers (1)

Natalja Olefire
Natalja Olefire

Reputation: 432

In your while cycle you should take digits of your big number one by one. Taking the reminder of division to 10 will give you last digit of your number. But your next number should be not the same, but integer part of division of this number to 10. Also your passed to the method variables will not be available outside, so if you want to print them - do it in your method directly, or return them somehow (you will need some object for it most probably). Try this:

  private static int optionOne(int integer, int evenCount, int oddCount, int zeroCount)
  {
      int test = integer;
      int temp = test; 
      do 
      {
          test = temp % 10;
          temp = temp / 10;

          System.out.println("Current temp number: " + temp);
          System.out.println("Current test digit: " + test);
          if (test==0)
          {
             zeroCount++;
          }
          else if (test%2==0)
          {
             evenCount++;
          }
          else
          {
             oddCount++;
          }

      } while (temp > 0 );
      System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
      return integer % 10; // do you really need it?
  }

Upvotes: 2

Related Questions