Forums Register Login

Constructor error.

+Pie Number of slices to send: Send
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;
}

}
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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));
+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
�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?
pie. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1015 times.
Similar Threads
Read keyboard IO methods hangs.
getting NullPointerException
Data Structure Java
input from console
single arrays in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:21:19.