8 Replies Latest reply: Dec 20, 2012 8:49 AM by TPD-Opitz RSS

    How to limit user input to a Single Character??

    981014
      // Hi, i would like to ask on how can i limit my user input to a Single Character??, here is my code.

      //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      import java.util.Scanner;
      public class calc_scan {
           public static void main(String [] args)
           {
           Scanner input = new Scanner(System.in);
           double num1=0,num2=0, ans=0;
           char choice2;
           System.out.println("What would you like to do? ");
           System.out.println("A - Addition");
           System.out.println("S - Subtraction");
           System.out.println("M - Multiplication");
           System.out.println("D - Division");
           
           
           System.out.print("Enter Choice: ");
           choice2 = input.next().charAt(0);

           
           if(choice2=='A' || choice2=='a') //Addition
           {
                System.out.print("Enter first number: ");
                num1 = input.nextDouble();
                System.out.print("Enter second number: ");
                num2 = input.nextDouble();
                
           ans = num1+num2;
           System.out.print("The answer is "+ans);
           }
           else if(choice2=='S' || choice2=='s') //Subtraction
           {
                System.out.print("Enter first number: ");
                num1 = input.nextDouble();
                System.out.print("Enter second number: ");
                num2 = input.nextDouble();
                
           ans = num1-num2;
           System.out.print("The answer is "+ans);
           }
           else if(choice2=='M' || choice2=='m') // Multiplication
           {
                System.out.print("Enter first number: ");
                num1 = input.nextDouble();
                System.out.print("Enter second number: ");
                num2 = input.nextDouble();
                
           ans = num1*num2;     
           System.out.print("The answer is "+ans);
           }
           else if(choice2=='D' || choice2=='d') //Division
           {
                System.out.print("Enter first number: ");
                num1 = input.nextDouble();
                System.out.print("Enter second number: ");
                num2 = input.nextDouble();
                
                if (num2==0)
                {
                System.out.println("Cannot be divide by Zero");
                }
                else
                {
                     ans = num1/num2;
                     System.out.print("The answer is "+ans);
                }
                
           }
           else
                {
                System.out.println("Invalid Option");
                }
           
           }
      }
      //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

      In this code, whenever i tried to type a String of characters like ("Annnnnnnnnn") it is still follows the operation for Addition, what i would like to do is that the user can only enter a Single Character and not a String, if the user tried to enter the required character for the if else condition plus a string of characters , the system should not accept it.


      example
      user tried to input a string of characters like "Annnn"

      the output should be "Invalid"

      what would i like is that, the if else condition will only work if the user tried to input a Single Only Character

      example
      input: "A"

      the output should be the answer for addition

      now if the user tried to input "Annn"

      the output should be invalid.....