Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arraylist" Watch "arraylist" New topic
Author

arraylist

Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
Hi-
I am using the ArrayList in my program. I declare it once in the main and have 3 other methods that are called when needed. One method prompts the user for a name and adds it to the arraylist. The second method prompts for another name and adds it as well, depending upon if the user said yes or no. The final method will print the contents of the arraylist(all the names and number of entries in the arraylist). I know that I need to pass variable names to each of the methods. The first metod will get the name of the arraylist. The second method I currently don't have anything being passed to it. The final method receives the name of the first entry. See the code below.
import java.util.ArrayList;

public class Exercise1{

private static void getname(String myname){//prompt user to enter a beer name and store in an array
GameHelper io = new GameHelper( );
//ArrayList<String> myname = new ArrayList<String>( );
String beer = io.getUserInput("What's your favorite beer?");
myname.add(beer);//adds name to arraylist
morebeer( );//calls morebeer method to see if user wants to enter more favorites
}//closes getname( );

private static void morebeer( ){
GameHelper io = new GameHelper( );
String action = io.getUserInput("Enter another favorite? 'y' for more beer, 'n' for milk:");//prompts user for more input
if (action.equals("y")){//user wants to enter more beer
getname( );
}else {
beerresult( );
}//closes if
}//closes morebeer( );

private static void beerresult(String beer){
//ArrayList<String> myname = new ArrayList<String>( );
int count = 0;
int size = myname.size( );
while (count < size){
System.out.println(myname.get(count));
count ++;
}//closes while loop
System.out.println(size);
}//closes beerresult( );

public static void main (String [ ] args){
ArrayList<String> myname = new ArrayList<String>( );
getname( );//calls getname method

}//closes main
}//closes class
My problem is that when I add names to the arraylist and subsequently no longer wish to add any names, the code only prints out '0', with no names following.

Thanks for your help,
Dave
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Reposted for readability

[ October 02, 2005: Message edited by: Michael Ernest ]

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, it will help a lot if you use UBB CODE tags when you post your code here. They will preserve the formatting in your code so that we can read it more easily.

With that said, I tried to compile your code, but I don't have the GameHelper class. Even if I ignore those errors, there are a few other errors that stop your code from even compiling, so I'm surprised that you can run it. The first error (other than ones involving the GameHelper class) is

This is in the method declared as

Your comment says that you are trying to add the name to an ArrayList, but myname is a String (not an ArrayList). If I understand what you are doing, the parameter should be an ArrayList instead of a String so that you can add the name to the ArrayList.

Hopefully this will help with your problems. If not, please post your modified code. (Be sure to use CODE tags.) I will be glad to help you further from there.

Layne


Java API Documentation
The Java Tutorial
Dave Kairys
Ranch Hand

Joined: Sep 09, 2005
Posts: 33
Do you mean to pass the parameter 'ArrayList' as in the following:
private String getname(ArrayList), or would I pass the name of the arraylist:
ArrayList<String> myname = new ArrayList<String>( ); namely, myname? Also, in terms of the UBB code guidelines, can I just click on the CODE button below and insert appropriate lines of code in between the tags and all is well?

Thanks,
Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: arraylist