without supplying command line arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    #1

    without supplying command line arguments

    Friends, I wish to consult about this big arithmetic program.
    This program basically is calculating big numbers. It work great from command prompt, for instance: java bigal 999999999999999 99 + 5.
    Is this program works without supplying command line arguments?
    If possible then please guide me how to get it to work...really appreciate any help. many thanks.....

    Code:
    public static void main(String[] args) 
        {
            jonelo.sugar.util.GeneralProgram.requiresMinimumJavaVersion("1.3.1");
            try {
                new Main(args);
            } catch (BigAlException e) 
            {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
    an excerpt from the Main method ....
    Code:
    public Main(String[] args) throws BigAlException 
        {
    
            if (args.length < 1) {
                printHelpShort();
                return;
            }
    
            if (args.length < 2) {
                if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("-h"))
                    printHelpLong("/help/bigal/help_en.txt"); else
                if (args[0].equalsIgnoreCase("hilfe"))
                    printHelpLong("/help/bigal/help_de.txt"); else
                printHelpShort();
                return;
            }
    
            // operation valid?
            int opcode = receiveOperation(args[1]);
            if (opcode==UNKNOWN) throw new BigAlException("invalid operation");
    
            // general init
            BigInteger b1=null, b2=null, b3=null;
            BigDecimal d1=null, d2=null, d3=null;
            String out="";
            // default init values for fibonacci
            int fibseed0=0;
            int fibseed1=1;
    
            // 2nd number required?
            if (!isSingleOperation(opcode)) {
                if (args.length < 3) {
                    throw new BigAlException("This operation requires a 2nd number!");
                }
                if (isDecimalOperation(opcode))
                  d2 = receiveDecNumber(args[2]); else
                  b2 = receiveNumber(args[2]);
            }
    
            if (isDecimalOperation(opcode))
              d1 = receiveDecNumber(args[0]); else
              b1 = receiveNumber(args[0]);
    
            // remember the counter
            long begin = System.currentTimeMillis();
            switch (opcode) {
                case ADD:       out=toString(d1.add(d2));
                                break;
                case SUB:       out=toString(d1.subtract(d2));
                                break;
                case MUL:       out=toString(d1.multiply(d2));
                                break;
                case DIV:
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shana07
    Friends, I wish to consult about this big arithmetic program.
    This program basically is calculating big numbers. It work great from command prompt, for instance: java bigal 999999999999999 99 + 5.
    Is this program works without supplying command line arguments?
    If possible then please guide me how to get it to work...really appreciate any help. many thanks.....

    Code:
    public static void main(String[] args) 
    {
    jonelo.sugar.util.GeneralProgram.requiresMinimumJavaVersion("1.3.1");
    try {
    new Main(args);
    } catch (BigAlException e) 
    {
    System.err.println(e.getMessage());
    System.exit(1);
    }
    }
    an excerpt from the Main method ....
    Code:
    public Main(String[] args) throws BigAlException 
    {
     
    if (args.length < 1) {
    printHelpShort();
    return;
    }
     
    if (args.length < 2) {
    if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("-h"))
    printHelpLong("/help/bigal/help_en.txt"); else
    if (args[0].equalsIgnoreCase("hilfe"))
    printHelpLong("/help/bigal/help_de.txt"); else
    printHelpShort();
    return;
    }
     
    // operation valid?
    int opcode = receiveOperation(args[1]);
    if (opcode==UNKNOWN) throw new BigAlException("invalid operation");
     
    // general init
    BigInteger b1=null, b2=null, b3=null;
    BigDecimal d1=null, d2=null, d3=null;
    String out="";
    // default init values for fibonacci
    int fibseed0=0;
    int fibseed1=1;
     
    // 2nd number required?
    if (!isSingleOperation(opcode)) {
    if (args.length < 3) {
    throw new BigAlException("This operation requires a 2nd number!");
    }
    if (isDecimalOperation(opcode))
    d2 = receiveDecNumber(args[2]); else
    b2 = receiveNumber(args[2]);
    }
     
    if (isDecimalOperation(opcode))
    d1 = receiveDecNumber(args[0]); else
    b1 = receiveNumber(args[0]);
     
    // remember the counter
    long begin = System.currentTimeMillis();
    switch (opcode) {
    case ADD: out=toString(d1.add(d2));
    break;
    case SUB: out=toString(d1.subtract(d2));
    break;
    case MUL: out=toString(d1.multiply(d2));
    break;
    case DIV:
    Create your own array of String args and pass it here:

    Code:
     new Main(args);
    Replace args with your array of arguments.

    Comment

    • shana07
      Contributor
      • Jan 2007
      • 280

      #3
      Originally posted by r035198x
      Create your own array of String args and pass it here:

      Code:
       new Main(args);
      Replace args with your array of arguments.
      I put this code in main and compile, but no output is given.something wrong in my input parameter then...?
      Code:
      try 
              {
                  String[] arrayArgs = new String[] { "55555" + "6" };
      
                  new Main(arrayArgs);
              } catch (BigAlException e)

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        your string initialisation { "55555" + "6" } concatenates to a single string {"555556"}
        try initialising the array so
        Code:
        try 
                {
                    String[] arrayArgs = { "55555", "+", "6" };
                    new Main(arrayArgs);
                } catch (BigAlException e)

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          you could also enter your input using a JOptionPane and split the returned string, e.g.
          Code:
              try 
                  {
                      String str = JOptionPane.showInputDialog(null,"enter expression ");
                      String[] arrayArgs = str.split(" ");
                      Main(arrayArgs);
                  } catch (Exception e){}

          Comment

          • shana07
            Contributor
            • Jan 2007
            • 280

            #6
            Originally posted by horace1
            your string initialisation { "55555" + "6" } concatenates to a single string {"555556"}
            try initialising the array so
            Code:
            try 
                    {
                        String[] arrayArgs = { "55555", "+", "6" };
                        new Main(arrayArgs);
                    } catch (BigAlException e)
            Thanks for your help..This one also not giving any output.
            I don't know what else should I put to get output.
            What kind of arguments that I need to put because the arguments then need to be passed to receiveoperatio n method. I put again main and receiveOperatio n here for reference...kin dly advise.
            Code:
            public Main(String[] args) throws BigAlException 
                {
            
                    if (args.length < 1) {
                        printHelpShort();
                        return;
                    }
            
                    if (args.length < 2) {
                        if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("-h"))
                            printHelpLong("/help/bigal/help_en.txt"); else
                        if (args[0].equalsIgnoreCase("hilfe"))
                            printHelpLong("/help/bigal/help_de.txt"); else
                        printHelpShort();
                        return;
                    }
            
                    // operation valid?
                    int opcode = receiveOperation(args[1]);
                    if (opcode==UNKNOWN) throw new BigAlException("invalid operation");
            Code:
            private static final int receiveOperation(String operation) {
                    int opcode = UNKNOWN;
                    operation=operation.toLowerCase();
                    if (operation.equals("add") || operation.equals("+"))
                        opcode = ADD; else
                    if (operation.equals("sub") || operation.equals("-"))
                        opcode = SUB; else
                    if (operation.equals("mul") || operation.equals("*") || operation.equals("x") )
                        opcode = MUL; else
                    if (operation.equals("div") || operation.equals("/") || operation.equals(":") )
                        opcode = DIV; else
                    if (operation.equals("divide") || operation.equals("//") || operation.equals("::") )
                        opcode = DIVIDE; else
                    if (operation.equals("mod") || operation.equals("%"))
                        opcode = MOD; else
                    if (operation.equals("fib") || operation.equals("fibonacci"))
                        opcode = FIB; else
                    if (operation.equals("luc") || operation.equals("lucas"))
                        opcode = LUC; else
                    if (operation.equals("nk") || operation.equals("choose"))

            Comment

            • shana07
              Contributor
              • Jan 2007
              • 280

              #7
              Originally posted by horace1
              your string initialisation { "55555" + "6" } concatenates to a single string {"555556"}
              try initialising the array so
              Code:
              try 
                      {
                          String[] arrayArgs = { "55555", "+", "6" };
                          new Main(arrayArgs);
                      } catch (BigAlException e)
              Sorry, I've made wrong.
              This one works! Thanks to you...
              I understand about String now.

              Comment

              Working...