Results 1 to 2 of 2

Thread: Reading from cmd

  1. #1

    Reading from cmd

    The below mentioned code i have written for exception handling. I am accepting three parameters from cmd that two integer values and third is operator if the first two argument are operator then i must generate exception, but when i am putting it into if condition as if (arg[0] == "+") its not actually checking it. Please tell me how to check this condition .


    class IllegalArgument extends Exception
    {
    String str = "";
    IllegalArgument()
    {
    str = "Argument must be in order of int, int, operator";
    }

    public String toString()
    {
    return str;
    }
    }

    class IllegalOperation extends Exception
    {
    String str = "";
    IllegalOperation()
    {
    str = "Operator should +,-,/,*";
    }

    public String toString()
    {
    return str;
    }
    }

    class Negative extends Exception
    {
    String str = "";
    Negative()
    {
    str = "Answer is negative";
    }

    public String toString()
    {
    return str;
    }
    }

    class ZeroException extends Exception
    {
    String str = "";
    ZeroException()
    {
    str = "Answer is Zero";
    }

    public String toString()
    {
    return str;
    }
    }

    class OperatorTest
    {
    public static void main(String arg[])
    {
    int ans = 0;
    // int a = Integer.parseInt(arg[0]);
    // int b = Integer.parseInt(arg[1]);
    String c = arg[2];

    /* System.out.println(a + c + b);
    if (c == "+")
    {
    ans = a + b;

    }
    if (c == "-")
    {
    ans = a - b;
    }
    if (c == "*")
    {
    ans = a * b;
    }
    if (c == "/")
    {
    ans = a / b;
    }*/

    try
    {
    if (arg[0] == "+" || arg[0] == "-" || arg[0] == "/" || arg[0] == "*" || arg[1] == "+" || arg[1] == "-" || arg[1] == "/" || arg[1] == "*" )
    {
    throw new IllegalArgument();
    }
    }
    catch (IllegalArgument e)
    {
    System.out.println(e);
    }

    try
    {
    if (c != "+" || c != "-" || c != "/" || c != "*")
    {
    throw new IllegalOperation();
    }
    }
    catch (IllegalOperation e)
    {
    System.out.println(e);
    }

    try
    {
    if (ans == 0)
    {
    throw new ZeroException();
    }
    }
    catch (ZeroException e)
    {
    System.out.println(e);
    }

    try
    {
    throw new Exception();
    }
    catch(Exception e)
    {
    System.out.println(e);
    }


    }
    }


  2. #2
    Contributing Member
    Join Date
    Sep 2006
    Answers
    42

    Re: Reading from cmd

    == is a compare value by reference. String is an object. So when you use ==,
    it compares the references of the two objects ( c and "+"). To compare the values, use "+".equals(c).
    Seeing your code, you will have to make this change everywhere.


    Regards,
    Sahil.



    --Smile, it makes people wonder what you are thinking.

    Last edited by SahilKabra; 10-18-2007 at 06:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact