Jump to content

calculator using Command Line Arguments in java : 12 + 13

- - - - -

  • Please log in to reply
7 replies to this topic

#1
baijupshah

baijupshah

    Newbie

  • Members
  • Pip
  • 3 posts
Dear All,
I wrote this code to implement a cmd line arg calc in Java. It it not running. Please help.
I run the program from Eclipse : arguments I pass are 12 + 13 . 3 arguments arg0 is first value, arg 1 is symbol to perform calculation and arg2 is second value.. Here goes the code...
public class CommandLine {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
        String s2 = args[1];
        char ch = s2.charAt(0);
        int i,j,total;
        i = Integer.parseInt(args[0]);
        j = Integer.parseInt(args[2]);
        
        
        switch(ch)
        {
        case '+' :
            total = i + j;
            break;
        case '*' :
             total = i *j;
             System.out.println("Multiply");
             break;
                // other symbols to be included later...
        }
        
        System.out.println(args[0] + ch + args[2] + " " + total);
        
        
    }

}


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Wait, but isn't args[0] supposed to be the executable filename? Or is it the executable filename just for C/C++?

#3
common_man

common_man

    Newbie

  • Members
  • Pip
  • 8 posts
what error are you getting?

#4
baijupshah

baijupshah

    Newbie

  • Members
  • Pip
  • 3 posts
Program works fine with + - or / symbols from cmd line. * is giving me the problem. I have worked out the solution for that too.. I am sending ' x ' for multiplication. I don't what's wrong with '*' sign.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
*Moved to Correct Forum*

Remember that, at least in Unix command prompts, * is the wildcard character. Passing * as an argument to the Java Program would simply expand to any file within the current directory, which means there would be many arguments as opposed to a few. I suggest you use a different multiplication character, like for example 'x'.

@RhetoricalRuvim: Java argument arrays start with the first argument, not the name of the program as it was executed on the shell. Actually getting that name in Java can prove somewhat a pain, but there are ways.
Wow I changed my sig!

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I tried making a calculator in C/C++ once, under Linux (Ubuntu), and the same thing happened with the '*' character.

#7
baijupshah

baijupshah

    Newbie

  • Members
  • Pip
  • 3 posts

RhetoricalRuvim said:

I tried making a calculator in C/C++ once, under Linux (Ubuntu), and the same thing happened with the '*' character.

oh yes... * is a wild card letter in Windows too... that's why I was getting error...

Thanks for the information.

#8
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
For the command line, instead of 'calculator 2 * 4' try 'calculator 2 \* 4'

I did a little experiment with C++ on Linux, and the '\*' turned into '*' , rather than a list of filenames.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users