File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes methods that return arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods that return arrays" Watch "methods that return arrays" New topic
Author

methods that return arrays

Sally Curtis
Greenhorn

Joined: Jan 10, 2004
Posts: 26
i am writing a method that returns an array full of students names. The students names are given in a file, so my method called ReadFile, reads the file and adds each line(name) to the array
i'm not sure my code is correct, plus i'm not familiar with writing methods that return and array.
If someone could comment on this code, i would really appreciate it.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
To return an array from a method is simple. You use the type with the array operator ([]). So to return an array of type int:

Make sure the type after the return keyword at the end of the method (or anywhere there is a return inside the method) is the same as the return type in the method declaration (at the top).

This example assumes there is only one name per line in the text file. There are a lot of ways to do these kind of things. Here we just read the names and add them to a string using a comma as sepatator and after the file reading is finished convert the string to an array.
Sally Curtis
Greenhorn

Joined: Jan 10, 2004
Posts: 26
im a bit confused, you said that you would commas as the separator, well what if in the text file, there are no commas separating the names for example
John Doe
Sue McGonnal
just spaces,
and also in the readFile method, what exactly does the .Split method do?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
In the example code above I am reading each line of the text file (and assuming one name per line) adding each name followed by a comma (as delimiter). You can see this in the while loop:

After the while loop the String names holds all the names read from the file, separated by commas (that I added as I gathered the names). Then I remove the last (trailing) comma in preparation for converting the string to an array.

You can find the split method in the String api. It creates and returns an array of strings demarked by the delimeter specified in the argument ("," here).

I added some System.out.println statements to readFile to show what names looks like during the process. The output of names at the end is the same as the text file that contains the names (one name per line).
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
You might also want to consider using an ArrayList to read your items, then the toArray() as you return. That way, you can have your cake (ArrayList.add()) and eat it too.

For example:



Paul M. Santa Maria, SCJP
Sally Curtis
Greenhorn

Joined: Jan 10, 2004
Posts: 26
my data file contains a list of the names but the list has no commas after each name. I tried making the delimeter ""+"" two spaces but it didnt work. so how would i accomplish making the delimeter equal to some kind of space?

also id like to thank the people in this post who helped. I really appreciate the help!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You don't need commas in the file.
Is there more than one name per line in your file? If there are what does the file look like?
Here is the file I used in the example code above:
 
wood burning stoves
 
subject: methods that return arrays
 
Similar Threads
Java: Convert a binary file to "text" and back again.
How do I read and write a binary file?
text file
Could anyone help with this???
Confused with file read