Avatar of Mike Eghtebas
Mike Eghtebas
Flag for United States of America

asked on 

main not firing?

In the TestCalculator class below, three arguments are supplied as
2 + 3

or -, *, / operators to calculate the result.

In:

        public static void main(String[] args) {
     
            String errorMessage = validInput(args);      //<---- The main or this call is not firing

           System.out.println("Test return: "+errorMessage+"\n");  <-- because this doesn't print

Question: What am I doing wrong?

Please note that in the code below intentionally Exception handling is not used.

Thank you.
import java.text.DecimalFormat;

public class TestCalculator {
    
   public static boolean isNumeric(String s) {
       
        boolean b = true;
        
        try {   
          float f = Float.parseFloat(s);
        }
        catch(Exception ex){
            b = false;
        }
            return b;
        }
   
        public static String validData(String [] ss){

            String temp="";
            
            if(isNumeric(ss[0])) {
                temp= "Wrong Input: " + ss[0];
            }else if(isNumeric(ss[2])) {
                temp= "Wrong Input: " + ss[2];
            }else if(Double.parseDouble(ss[2])==0.0 && ss[1].charAt(0)=='/') {
                temp= "Division by zero";
            } 
            return temp;
        }

        public static String validInput(String [] ss){

            String temp="";
            
            if (ss.length !=3) {
                temp = "Usage: java Calculator operand1 operator operand2\n";        
            }else if(ss[1]=="+" || ss[1]=="-" || ss[1]=="*" || ss[1]=="/"){
            }else{
                temp = "Usage: java Calculator operand1 operator operand2\n"+
                       "       (Make sure to have * inside \"*\" as shown here.";
            }

            return temp;
        }
        
        public static void main(String[] args) {
      
            String errorMessage = validInput(args);

            System.out.println("Test return: "+errorMessage+"\n");
        
            if(errorMessage.trim().length() != 0){

                System.out.println(errorMessage+"\n");
                        
            }else{

                errorMessage = validData(args);
        
                if(errorMessage.trim().length() != 0){
                    System.out.println("Error: " + errorMessage + "\n\n" +
                        "Usage: java Calculator operand1 operator operand2\n");                    
                }else if(errorMessage.toString()==""){
                    System.out.println(errorMessage + "\n");                    
                }else{
                    printTheCalculationresult(args);
                }
            }
        }

        public static void printTheCalculationresult(String[] args)
        {
            System.out.print("printTheCalculationresult");

            DecimalFormat df=new DecimalFormat("0.##");
            double result=0.0;

            switch (args[1].charAt(0)){
                case '+': result=(int) Double.parseDouble(args[0]) +
                                 (int) Double.parseDouble(args[2]);
                          break;
                case '-': result=Integer.parseInt(args[0]) -
                                 Integer.parseInt(args[2]);
                          break;
                case '*': result=(int)Double.parseDouble(args[0])*
                                 (int) Double.parseDouble(args[2]);
                          break;
                case '/': result=Integer.parseInt(args[0]) /
                                 Integer.parseInt(args[2]);
            }

            System.out.print(df.format(args[0])+' '+ 
                                       args[1]+' '+
                             df.format(args[2])+" = "+
                             df.format(result)+"\n\n");
    }    
}

Open in new window

Java

Avatar of undefined
Last Comment
Mike Eghtebas

8/22/2022 - Mon