Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 8
  • 5
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now