wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Constructor error. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor error." Watch "Constructor error." New topic
Author

Constructor error.

Lim Youjie
Greenhorn

Joined: Dec 27, 2004
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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



[Jess in Action][AskingGoodQuestions]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Lim Youjie
Greenhorn

Joined: Dec 27, 2004
Posts: 22
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

Joined: Oct 10, 2005
Posts: 16
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

Joined: Dec 27, 2004
Posts: 22
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

Joined: Oct 02, 2003
Posts: 11154
    
  16

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!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16
�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?
 
jQuery in Action, 2nd edition
 
subject: Constructor error.
 
Similar Threads
input from console
Data Structure Java
getting NullPointerException
single arrays in java
Read keyboard IO methods hangs.