• Post Reply Bookmark Topic Watch Topic
  • New Topic

append method  RSS feed

 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help fixing the append() method. I have a tester method too, but body is null in debugger.

Design a class Message that models an e-mail message. A message has a recipient, a
sender, and a message text. Support the following methods:
• A constructor that takes the sender and recipient
• A method append that appends a line of text to the message body
• A method toString that makes the message into one long string like this: "From:
Homer Simpson\nTo: Marge Blue\n . . ."

 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the specification for the "append" method.

Abu Apu wrote:• A method append that appends a line of text to the message body


Looking at your implementation of that method, you start by creating an empty list of Strings. Why is that? What does it have to do with the message body?
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Here's the specification for the "append" method.

Abu Apu wrote:• A method append that appends a line of text to the message body


Looking at your implementation of that method, you start by creating an empty list of Strings. Why is that? What does it have to do with the message body?


Thank you!!! . Because the Message class has to use an ArrayList<String> to hold the lines of the message together. It was given as part of the assignment so I figured why not make an Arraylist in the append method.
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abu Apu wrote:Because the Message class has to use an ArrayList<String> to hold the lines of the message together.


Okay.

It was given as part of the assignment so I figured why not make an Arraylist in the append method.


Why not? Because the message body should be an attribute of the Message class. That's what the assignment says, right? So you wouldn't create the message body each time you append a line to the Message.
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not? Because the message body should be an attribute of the Message class. That's what the assignment says, right? So you wouldn't create the message body each time you append a line to the Message


Hmmmm. Would the append method add the text line by line into the body? Then have body defined as an ArrayList at the top.



append method looking like this:


However, the error it gives me is "text cannot be resolved or is not a field." Gah. This assignment is so aggravating.... same goes for all the OOP programs I've had to do so far.
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty much correct, actually. Only in the append method you used "this.text"... what does "this" mean when you prefix it to a variable like that?
 
Liutauras Vilda
Marshal
Posts: 4651
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Paul's hint. Keyword this is designed to disambiguate and avoid conflicts in certain situations, when you have parameter/local/instance variables with the same name.

As an example i'll take one of your already created methods
Line 3. You have an instance variable body.
Line 8. You have a local variable body.
Line 10. By writing this.body you're telling to take this instance variable body and concatenate with text. So what happens with variable on line 8? Nothing actually. It is created locally and it is an empty array list with no elements in it. So you do 0 iterations with that loop and since all instance variables are being initialized to their default values (unless you assign different values to them). So instance variable body is null and remains null after the method gets executed.

Anyway, lets get back to your latest attempt.
First thing. Never write code in that way, I have in mind indentation. That line should look like one of the next examples:
orStick to the one standard and always follow it, unless you're being forced to use differently in your work place. To find out more about good code indentation you can find here. Quite old document but still practices widely applied.

Now about that method above. When you write this.text, you're telling to concatenate body variable with an instance variable text (which you don't have, right?). You do have parameter variable, which is similar to local variable, but just with higher scope (visibility). So, that line should look similar to body += text.
So why this keyword is not needed in this particular case prior the body variable? Because there are no conflicts between variables as you only have one body variable in that method.

I hope it helped a bit.
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the thorough explanations!



When I run the tester main class the error message is a NPE. It doesn't make any sense! The String text parameter is clearly being added to the ArrayList called body.... so how would it be null!? Before this I implemented



But it said 'incompatible types: String cannot be converted to ArrayList<String>. I tried making a conversion, but it failed.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you assign an instance of ArrayList do body?
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Where do you assign an instance of ArrayList do body?


Howdy Pawel!

It's assigned in the top of the code

 
Liutauras Vilda
Marshal
Posts: 4651
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to solve your problem in trial and error method, which led you (and at least me) to a confusion, as in all your posts you used different method editions.

Please post exact code (all, as it is not long) you're using now.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You showed how you declare body variable. I don't see you assign anything to it (using = operator).

 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is declaring an attribute of your class. At this point 'body' is defaulted to null.

You need to assign a value to it.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never assign anything to body.
That means it has default value (null) thus NPE.
 
Liutauras Vilda
Marshal
Posts: 4651
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In slightly different words, you need to create an instance of an array list.
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:In slightly different words, you need to create an instance of an array list.


Aw okay. I initialized it as a local scope of append method. Added a for loop to iterate and increment for its size.



However, body (ArrayList) and text (String) are incompatible types. What's the workaround to convert ArrayList into String?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That is simply initialising a variable which is local to the append method.

Do that initialisation where you declare the instance variable instead.
 
Liutauras Vilda
Marshal
Posts: 4651
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, lets get it clear.

That method append sounds confusing in the first place. It won't append anything. At best it would add an element to an array list. To do so, there is a method add. That would look:
In the above post was mentioned, that with latest revision of code, body is declared as local variable, so it will go out of scope when method will end.

Now the question is what do you mean by append? i.e.:

Scenario 1
Passed arguments to a method append during the multiple method calls, inputs: text0, text1, text2, text 3.
In an array list that would look like separate elements. In possion 0 there will be a string element text1, in position 1 there will be text2...
Similarly as in array would be elements in the positions starting from 0:

Scenario 2
There is a StruingBuilder class, which has append method.
And after calling method append with the same passed arguments 1 at a time, your would get:

Question is, what is your actual task?
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
append method is supposed to add the text lines into the ArrayList body.

I have a tester method which is supposed to call Message class. Tester code is



I think the problem lies in the while loop because the message will always be printed. Since I am using a while(input.hasNextLine) ... I'll try a simpler loop.

Output
 
Abu Apu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES!!! FINALLY IT WORKS!!!

Thank you all for the assistance!!! Omg I'm so happy. Now onto the next homework assignment.

P.S. for my classmates who might copy my algorithm. Good Luck. because the Professor has my algorithm in his inbox already
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like a loop with while...hasNextLine at the start and then equals("JavaSux") later on. You can terminate the input with ctrl‑D (Unix‑like systems) or ctrl‑Z on Windows®.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!