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

Thread: Calculator dividing by 0 problem.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculator dividing by 0 problem.

    New assignment, need to obviously make the number variables and action variables work together. All of them do apart from divide which has a major problem that I can't figure out how to solve

    class Calculator
    {
    private long count = 0;
    public void evaluate( char action, long number ){
    if ( action == '+' ){
    count = count + number;
    }
    else if ( action == '-' ){
    if ( number == 0) {
        count = count;
    }
    count = count - number;
    }
    else if ( action == '/' ){
    if ( number == 0 ){
    count = count;
    }
    count = count / number;
    }
    else if ( action == '*' ){
    count = count * number;
    }
    }
    public long getValue(){
    return count;
    }
     
    public void setValue( long number ){
    count = number;
    }
     
    public void reset(){
    count = 0;
    }
    }

    Yet again advice/reading material would be very helpful


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: Calculator dividing by 0 problem.

    What is the problem?

    If you are getting a runtime exception, post it along with the (runnable) code that gives rise to the exception. Indicate, somehow, which line(s) of your code the stack trace is referring to.

    -----

    I am a bit confused by what you have written for subtraction:

    else if ( action == '-' ){
        if ( number == 0) {
            count = count;
        }
        count = count - number;
    }

    What is that code supposed to do? Why do you assign the value of count to itself (an operation that does nothing) before performing the subtraction? Something similar could be asked of the division code.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculator dividing by 0 problem.

    Sorry man, I was in a hurry for work and posted the wrong code. Il post my Logic and Calculator code along with the exception.

    My assignment requires that number / zero will need to return as 0. My program breaks when I try this:

    Calculator:

    class Calculator
    {
    private long count = 0;
    public void evaluate( char action, long number ){
    if ( action == '+' ){
    count = count + number;
    }
    else if ( action == '-' ){
    count = count - number;
    }
    else if ( action == '/' ){
    count = count / number;
    }
    else if ( action == '*' ){
    count = count * number;
    }
    }
    public long getValue(){
    return count;
    }
     
    public void setValue( long number ){
    count = number;
    }
     
    public void reset(){
    count = 0;
    }
    }

    Logic:

    class Logic
    {
      private enum  State { FIRST_NUMBER, SUBSEQUENT_NUMBER };
      private State state = State.FIRST_NUMBER;
      private long  number = 0;
      private char  op = ' ';
      private Calculator calc = null;
     
     
      public Logic( Calculator calculator )
      {
        calc = calculator;
      }
     
      public String process( String button )
      {
        String info = null;
        if ( button.length() == 1 )
        {
          char c = button.charAt(0);
          if ( c >= '0' && c <= '9' )               // Digit 
          {
            number = number * 10 + c-'0';           // Build number 
          } else {
            switch ( c )
            {
              case 'C' : number = 0;
                         break;
              case '=' :
              case '+' : case '-' :
              case '*' : case '/' :
                switch ( state )
                {
                  case FIRST_NUMBER:
                    calc.setValue( number );
                    state = State.SUBSEQUENT_NUMBER;
                    break;
                  case SUBSEQUENT_NUMBER:
                    if ( op != '=' )
                      calc.evaluate( op, number );
                    break;
                }
                op = c;  number = 0;
                break;
            }
          }
        } else {
          if ( button.equals( "CR" ) )               // Clear Result 
          {
            calc.reset(); number = 0; state = State.FIRST_NUMBER;
          }
        }
     
        return info;
      }
     
      public long getResult()
      {
        return calc.getValue();
      }
     
      public long getNumber()
      {
        return number;
      }
    }

    Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero
    at Calculator.evaluate(Calculator.java:12)
    at Logic.process(Logic.java:40)
    at GUI$ButtonPress.actionPerformed(GUI.java:106)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6504)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:626 9)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713 )
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Calculator dividing by 0 problem.

    count = count / number;
    This is the line which is triggering the exception.

    This code would throw the / zero exception if number = 0, so I suggest putting in place a simple if-statement to check for that possibility before attempting the division.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Infix calculator problem
    By cubertron in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2011, 07:32 AM
  2. Carpet Calculator Problem using aggregation
    By gpelefty90 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2011, 03:31 PM
  3. Calculator layout problem
    By Choiseymitsu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 09:04 AM
  4. [SOLVED] Another Calculator Problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 4th, 2010, 03:31 PM
  5. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM