Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor error.

 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im quite new to java. may i know what is wrong is my constructor. Theres an error stating that "operator + cannot be applied to int,MyInteger." Thanks

//Below are the codings from the main method

InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader kbd = new BufferedReader (isr);

System.out.print("Enter first integer number: ");
MyInteger a = new MyInteger(Integer.parseInt(kbd.readLine()));
System.out.print("Enter second integer number: ");
MyInteger b = new MyInteger(Integer.parseInt(kbd.readLine()));
System.out.println(a + "+" + b + " is: " + a.add(b));


//Below is the constructor MyInteger

public class MyInteger
{

private int number;

public MyInteger(int initialnumber)
{
number = initialnumber;
}

public int add (MyInteger addednumber)
{
number = number + addednumber;
return number;
}

}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the method add(), the argument is of type "MyInteger", and you're tying to apply to "+" operator to that argument; hence the message that operator "+" can't be applied to MyInteger.

You can't add an int to a class object. Maybe you mean

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The + operator is for primative types, not Objects (unless that Object is a String, which is a special case). So:

is fine, but:

is not. Now look at your code - do you see what you need to do to fix it?
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but although i get the desired results for add() but it does not work for sub();
For example for the 1st input i enter '9'
and 2nd input i enter '5'
for add() i get 14 ---> correct ans
but for sub() i get 9(14-5); --->do not know why the number variable used is 14 instead of 9.

Try to use this.number to solve but it din help.

//method for addition
public int add (MyInteger addednumber)
{
number = number + addednumber.number;
return number;
}

//method for subtraction
public int sub(MyInteger addednumber)
{
number = number - addednumber.number;
return number;
}

Thanks
 
Martin Wingert
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your sub() and add() method looks like they should work. The problem must be in the main method. How does your main look now? I assume that you have added some kind of loop to permit several calculations.
 
Lim Youjie
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my main method. i cant find out wats wrong. Thanks

InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader kbd = new BufferedReader (isr);

System.out.print("Enter first integer number: ");
MyInteger a = new MyInteger(Integer.parseInt(kbd.readLine()));
System.out.print("Enter second integer number: ");
MyInteger b = new MyInteger(Integer.parseInt(kbd.readLine()));
System.out.println(a + "+" + b + " is: " + a.add(b));
System.out.println(a + "-" + b + " is: " + a.sub(b));
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sagolo,

You may not have read the Naming Policy on your way in. Basically, it requires a real sounding first and last name. a first initial is ok, but the name cannot be anything obviously ficticious.

Would you be so kind as to edit your profile here to comply?

Thanks!!!
 
Martin Wingert
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
�hm..well..that's because your add() and sub() methods alter the internal variable in the MyInteger class. So if input 9 as input 1 and then 5 as input 2 the add method will add 5 to the int called number in MyInteger. Since the add method does this:

number = number + addednumber.number;

If you didn't want the add method to alter the value of MyInteger then just return the recalculated value and don't touch the internal variable:

public int add (MyInteger addednumber)
{
//number = number + addednumber.number;
return number+addednumber.number;
}

Do you get my explanation?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic