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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
This prints "Im here" - so the mthod validateInput is running - it finds more than 3 args I think

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="";

            System.out.println("Im here");
            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

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I entered

2 A 5

Which is supposed to say:

Usage: java Calculator operand1 operator operand2\n"+...

What do you see the problem is?
0
for_yanCommented:
You should never compasre strings like that - only with equals() method (therese are not chars event they have one chars)
ss[1]=="+" || ss[1]=="-" || ss[1]=="*" || ss[1]=="/"

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re> ss[1]=="+" || ss[1]=="-" || ss[1]=="*" || ss[1]=="/"

If above is not correct, what is the correct way of to compare?
0
for_yanCommented:

This is correct way to compare - now it works through this stage - you have some problems in the next methods also
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){

                 for(String s: ss){
                System.out.println(s);
            }

            String temp="";

            System.out.println("Im here");
            if (ss.length !=3) {
                temp = "Usage: java Calculator operand1 operator operand2\n";

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

            

            return temp;
        }

        public static void main(String[] args) {

            for(String s: args){
                System.out.println(s);
            }

            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

0
for_yanCommented:
there was one misprint about the suubtraction above
it is corrected beklow

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){

                 for(String s: ss){
                System.out.println(s);
            }

            String temp="";

            System.out.println("Im here");
            if (ss.length !=3) {
                temp = "Usage: java Calculator operand1 operator operand2\n";

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

            

            return temp;
        }

        public static void main(String[] args) {

            for(String s: args){
                System.out.println(s);
            }

            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

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
like:

ss[1].equals("+") || ss[1].equals("-")   etc?
0
for_yanCommented:
Yes, but better to look for mistakes not correctnes and leave soem thisng like that {},
so check incorrectnes - seee my code above

and NEVER use == for Strings and other objects - only for primitives == can be uysed
0
for_yanCommented:
there were still many other mistakes - like isNumeric()  should be checked !isNumeric()  for "wrongness"
then you should format with df.format(...) only when you need to format numbers , so df.frormat(args[0]) - is  unnecessary,
for printining.
So the code below works - at least for 2 + 3 case

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){

                 for(String s: ss){
                System.out.println(s);
            }

            String temp="";

            System.out.println("Im here");
            if (ss.length !=3) {
                temp = "Usage: java Calculator operand1 operator operand2\n";

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

            

            return temp;
        }

        public static void main(String[] args) {

            for(String s: args){
                System.out.println(s);
            }

            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("print The Calculation result: ");

            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(args[0]+ " " +    args[1] + " " + args[2] + " = "+  df.format(result)+"\n\n");
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
41.5 - 5.0 = 36.0     <-- should give 36.5

I need to go back and read more about number types I guess. But for now, you may want to tell me why. I took away the df.formt().`
0
for_yanCommented:

when you do this

if double d = 41.5
so when you do this    (int) d  
it will truncate and make 41 out of it

That's why you should not use any ints if you allow people to input non-integer numbers
then everything in your calculation should be double - no int's anywhwere


0
for_yanCommented:
df.format(d)  should take argument d which is double , so df.fromat(args[0]) throws error, as args[0] is a String

And you also don't need it - the way people eneterd it - the same way you'd rather print

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.