aspose file tools*
The moose likes Beginning Java and the fly likes Help: writing a method to concatenate a line. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help: writing a method to concatenate a line." Watch "Help: writing a method to concatenate a line." New topic
Author

Help: writing a method to concatenate a line.

Kalona Ark
Greenhorn

Joined: Oct 31, 2007
Posts: 20
What am I doing wrong.

public static int concateLines(Stringline){
return Stringline;
{
I probably did this completely wrong.
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1344
    
  12
Originally posted by Kalona Ark:
What am I doing wrong.

public static int concateLines(Stringline){
return Stringline;
{
I probably did this completely wrong.


I'm not sure I've understood exactly what you are trying to do.

Your method declaration needs to specify the type as well as the actual parameter that it is going to accept:



In the above method, I say that I'm going to be giving the method an integer which I am calling 'someNember'. Then in my code, I can say:



Also notice that I am specifying what is going to come out of the method as well (the return type)


This means that I cannot do this:

because I've stated that the method returns an integer.

So in your method, we need to know
- what are you concatenating?
It sounds like you are working with strings. My guess is that you need to give the method at least two arguments (the things you are concatenating), and that you need to specify that you will be returning a String.

Go ahead and rewrite your method, post it here, and we'll help you if you get stuck!
Kalona Ark
Greenhorn

Joined: Oct 31, 2007
Posts: 20
I am concatenating this:

I need to have this text all in one line.

[ November 23, 2007: Message edited by: Kalona Ark ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39



Well, when you compile this, what error messages do you get? And what do you think is wrong, based on the error messages?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kalona Ark
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Lin.java:11: ';' expected
return String line;
^
Lin.java:11: not a statement
return String line;
^
Lin.java:12: reached end of file while parsing
}

I honesly do not know what I done wrong. I am really bad at methods. No matter how much I read and/or study methods, I can never understand them.

I tried to re-write it as a practice. This one got no errors.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16


On line 2, you declare the String variable as "line". However, the parameter is already called "line". Therefore, this line contains an error.

One line 3, you have added "String", but you're not declaring anything. You're just returning a value.

That's why your new code works - the String variable has a different name, and you've omitted the "String" in the return statement.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kalona Ark
Greenhorn

Joined: Oct 31, 2007
Posts: 20
Is this any better. I am trying to understand, but it is just not clicking.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

That's just excellent.
There is semantically little difference between "line[0].concat(line[1])" and "line[0] + line[1]". In both cases, the value of string line[0] is taken, the value of string line[1], and these are added into a new string object.

For simplicity's sake, just keep using + instead of concat, and you'll be fine for now. Later on you'll probably learn other techniques for concatenating strings, but the main difference between all techniques is performance. As a beginner, you don't want to bother with performance just yet.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39



Does it compile? I am assuming that it does... however...

When you run it, does it do what you expected? What does it do? And what did you expect it to do? And most important of all, what do you think is wrong?

Henry
 
 
subject: Help: writing a method to concatenate a line.
 
Similar Threads
20,000 ~ 30,000 rows Insert
Linked list Help
How to check the existing website(URL) is still working?
CharacterEncoding
Panel program(help plzzzzzzzzzz)