Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how could I throw an exception in this case

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default how could I throw an exception in this case

    guys, I'm reading a formula from file like: 2 + 2 - ( 3 - 1 ) * 3 $, but I want to be able to throw an exception if there are two operands together like: 2 + 2 3 * 4 $ or if there are two operators together like: 2 + + 3 $.

    here's what I have so far, the operator part the program detects it, but I'm having problem with two operands together. I would like to throw the exception where "Bad Formula!..." is

    while(in.hasNext())
            {
                String next = in.next();
                do
                {
                    if (optor.isOperator(next)) 
                    {
                        processOptor(next);
                        oprandCount = 0;
                    } 
                    else 
                    {
                        if(oprandCount == 0)
                        {
                            oprand.push(Integer.parseInt(next));
                            strOut += next+" ";
                            oprandCount++;                        
                        }
                        else
                        {
                            System.out.println("Bad formula!...");
                            System.exit(0);
                        }
                    }                                   
                    next = in.next();
     
                    if(next.equals("$"))
                        processOptor(next);
     
                }while(!next.equals("$"));
    Last edited by mia_tech; July 2nd, 2012 at 08:01 PM.


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: how could I throw an exception in this case

    Quote Originally Posted by mia_tech View Post
    I would like to throw the exception where "Bad Formula!..." is

    ...
                        else
                        {
                            // System.out.println("Bad formula!...");
                            // System.exit(0);
                           throw new Exception( "Bad formula, bad, bad!" );
    ...
    How about that?
    Need Java help? Check out the HotJoe Java Help forums!

  3. The Following User Says Thank You to stdunbar For This Useful Post:

    mia_tech (July 3rd, 2012)

Similar Threads

  1. Replies: 1
    Last Post: April 9th, 2012, 05:13 PM
  2. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  3. Applet throw ClassNotFound exception when one of the file is recompiled
    By philwei in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 12th, 2011, 01:21 PM
  4. Calculator Throw Error
    By bengregg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2011, 08:25 PM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 10:54 PM