1 Reply Latest reply: Oct 1, 2012 3:19 PM by abillconsl RSS

    New to Java return the operator from the method check for valid nuymber

    896976
      I am new to jave I would like to check for valid numbers from the user  firstNumber  and  secondNumber  on this code.. 
      Also I would like to return the operator from the Method, so I return the operator from the method  
      something like 
       System.out.format("%s %c  %s",firstNumber,Operator,secondNumber); 
      
      package recomparenumbers;;
      import java.util.Scanner; // program uses clas 
      
      
      public class ReCompareNumbers {
          private static char Operator;
          
          int isThisANumber = 0;
      
          
          public static void main(String[] args) {
         
          
      
         Scanner input = new Scanner(System.in);
            
          System.out.println("This program tests two floating point numbers for equality");
         
          System.out.println("Please, enter the first number:");
          float     firstNumber  = input.nextFloat();
       
          System.out.println("Please enter the second number:");    
           float secondNumber = input.nextFloat();  
         
            float Operator = compareNumbers(firstNumber,secondNumber); 
              }
         
         /*
          * Method: compareNumbers
          *  
          *  
          */
          public static float compareNumbers(float firstNumber, float secondNumber){
             char operator2;
            if (firstNumber == secondNumber) {
               Operator = 1;
               System.out.format("%s %s %s\n",firstNumber,'=',secondNumber); 
              }
              else if (firstNumber > secondNumber) {
               Operator = 2;  
                  System.out.format("%s %s %s\n",firstNumber,'>',secondNumber);         
            } 
              else if (firstNumber < secondNumber) {
                Operator = 3;
               System.out.format("%s %s %s\n",firstNumber,'<',secondNumber);    
            }
                
              return Operator;
      }       
      }
      
      Desire output 
      This program tests two floating point numbers for equality
      Please, enter the first number:
      4
      Please enter the second number:
      5
      4.0 < 5.0
      ==
      run:
      This program tests two floating point numbers for equality
      Please, enter the first number:
      5
      Please enter the second number:
      5
      5.0 = 5.0