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

Thread: Solution for error message: Bad operand for binary operator '-'

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Solution for error message: Bad operand for binary operator '-'

    Hi all, I'll try to be as clear as I can...

    I am creating a mini GUI project with a JFrame window that has two buttons added to it labelled 'Credit' and 'Deposit', my intention is to alsoadd two Textfields, one that allows the user to enter a nominal value of how much they wish to Credit or Deposit their balance by and another which shows the value of there resultant balance. Now, the steps are 1st the person enters a value into the textfield, 2nd presses either Credit or Deposit and 3rd the resulting balance shows the new value.

    My issue is I am now in the process of defining how the 'credit' button works when pressed, which is to simply subtract the value of how much the user wishes to withdraw from account from balance value; However, I have been stopped in my tracks by the error message seen in the snippet of code below, why has this error arisen? and what is the solution?

    ------------------------------------------------------------------------------------------------------------------------

    public class DefinitionAcc1{

    public static DefinitionAcc1 creditAcc(DefinitionAcc1 ba, DefinitionAcc1 hm1){
    DefinitionAcc1 result1 = new DefinitionAcc1();

    result1 = ba - hm1; <----THIS LINE INVOKES THE ERROR MESSAGE: Bad operand for binary operator '-'
    return result1;

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    What data types are ba and hm1? The binary operator - takes numeric primitives like int or double.

    What do you want to subtract in that statement? Do your classes have a method that you can call that will do the subtraction and return an object that the method can return?

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

    MostinCredible21 (September 7th, 2011)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    You are trying to subtract one object from another. That doesn't make sense. Waht is the result of "fox" - "paint"? What is the result of Fred - Barney? Perhaps you want to subtract some value of an object from the equivalent value in the other object. If so you can either retrieve the values from the objects and then do the subtraction or you can write a method in the class that takes another object and then performs the subtraction. eg:
    class Foo {
        private int value;
     
        Foo (int v) {
            value = v;
        }
     
        public int sub(Foo other) {
            return value - other.value;
        }
     
        public int getValue() {
            return value;
        }
     
        public static void main(String[] args) {
            Foo f1 = new Foo(100);
            Foo f2 = new Foo(12);
            System.out.println(f1 - f2); // HUH???
            System.out.println(f1.sub(f2)); // OK
            System.out.println(f1.getValue() - f2.getValue()); // OK 
        }
    }
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    MostinCredible21 (September 7th, 2011)

  6. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    Quote Originally Posted by Norm View Post
    What data types are ba and hm1? The binary operator - takes numeric primitives like int or double.

    What do you want to subtract in that statement? Do your classes have a method that you can call that will do the subtraction and return an object that the method can return?
    Hey Norm
    'Binary operator minus only takes numeric primitives....' note taken. My incorrect thinking was that I could define my own data type............ I guess
    Thanks for the questions asked this makes my thinking slightly clearer in what I am hoping to achieve and how to get there...

    However, I am confused when you ask the question what do I want to subtract in the statement, perhaps I did not make it clear in my explanation...I am subtracting how much the end-user enters into the textfield which is a value I wanted to go into 'hm1' object, by the balance which I want to be already stored in object 'ba'....does that answer your question?

    Yes, the code I have shown was to be the method called to carry out the subtraction by another class which I have not shown.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    I am subtracting how much the end-user enters into the textfield which is a value I wanted to go into 'hm1' object, by the balance which I want to be already stored in object 'ba'
    You need methods to work on the contents of class objects.

    Why are you using objects to store numeric values? Why not use primitives like int or double?

  8. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    Quote Originally Posted by Junky View Post
    You are trying to subtract one object from another. That doesn't make sense. Waht is the result of "fox" - "paint"? What is the result of Fred - Barney? Perhaps you want to subtract some value of an object from the equivalent value in the other object. If so you can either retrieve the values from the objects and then do the subtraction or you can write a method in the class that takes another object and then performs the subtraction. eg:
    class Foo {
        private int value;
     
        Foo (int v) {
            value = v;
        }
     
        public int sub(Foo other) {
            return value - other.value;
        }
     
        public int getValue() {
            return value;
        }
     
        public static void main(String[] args) {
            Foo f1 = new Foo(100);
            Foo f2 = new Foo(12);
            System.out.println(f1 - f2); // HUH???
            System.out.println(f1.sub(f2)); // OK
            System.out.println(f1.getValue() - f2.getValue()); // OK 
        }
    }
    Hey Junky

    Thanks for the analogy, I never thought what I was doing was a problem until the analogies you described highlighted the error in my thinking.

    Your quite right when you ponder..."Perhaps you want to subtract some value of an object from the equivalent value in the other object", the value I wish to subtract is how much the end-user enters into the textfield which is a value I wanted to go into 'hm1' object, by the balance which I want to be already stored in object 'ba', however, the values of these two objects will more than likely not equal each other...i.e. unlikely to be £5.00 - £5.00 but more likely...£5.00 - £2.34. It's random is what I am trying to put across.
    Understand?
    what do you suggest I do now?

    Looking at the code you have showed me has helped me to understand what I am trying to do, massive thanks for that

  9. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    Quote Originally Posted by Norm View Post
    You need methods to work on the contents of class objects.
    k...starting to get it now, what sort of statements would need to be included in such methods?
    -----------------------------------------------------------------------------------------------
    Quote Originally Posted by Norm View Post
    Why are you using objects to store numeric values? Why not use primitives like int or double?
    public class DefinitionAcc1{

    public static DefinitionAcc1 creditAcc(double ba, double hm1){ //is this how you use the double primitive like you suggested?
    DefinitionAcc1 result1 = new DefinitionAcc1();

    result1 = ba - hm1; //<---Changing primitives to double invoked error message: incompatible data types, required: DefinitionAcc1(); found: double
    return result1;

    }

    Thanks for your help so far its bringing me along slowly but surely

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    what do you suggest I do now?
    Are the two values you want to get the difference of held as Strings and not as numeric values?
    To subtract you will need to convert the Strings to numeric values.
    If the values are already numeric, why are they in class objects?

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Solution for error message: Bad operand for binary operator '-'

    Change the constructor to either of these:

    public static DefinitionAcc1 creditAcc(double ba, double hm1){
    DefinitionAcc1 result1 = new DefinitionAcc1(ba - hm1);
    or
    DefinitionAcc1 result2 = new DefinitionAcc1(ba, hm1);
    You need to decide what the creditAcc() method is supposed to do.
    Does it create a new DefinitionAcc1 object
    or update an existing one?

    You need to step back from writing this code and do some design work to decide what each method is supposed to do. What is happening now looks like you are writing code without having thought out what that code should do.

Similar Threads

  1. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 02:49 PM
  2. How to display error message box
    By jasonxman in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 21st, 2011, 02:47 PM
  3. [SOLVED] Help making an error message.
    By Lost_Secret in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2011, 04:48 PM
  4. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  5. help with a error message
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2010, 02:56 PM

Tags for this Thread