This discussion is archived
3 Replies Latest reply: Mar 15, 2008 8:04 AM by 807601 RSS

ArrayList

807601 Newbie
Currently Being Moderated
I'm trying to make a very simple Java App using arraylists.

I need it to prompt me for names and take those names until the user types in done.

Am I on the right track with my code at all? Sorry I'm just learning how to do this and am not very good at it yet.

string nameInput;

Needs to be changed because it errors out at cannot find symbol.

import java.util.Scanner;
import java.util.ArrayList;

public class MyAL
{
string nameInput;
int numberOfNames=0;
public static void main() { 
Scanner input;
input=new Scanner(System.in);

ArrayList listname = new ArrayList();

do{
System.out.print("Enter a name: ");
nameInput=input.nextString();
numberOfNames=numberOfNames+1;
}
while(nameInput.equals("Done"));
System.out.print("Number of names entered: " +numberOfNames);
}
  • 1. Re: ArrayList
    Herko_ter_Horst Newbie
    Currently Being Moderated
    You need to be more precise with upper and lower case characters.

    When posting code, please use the code tags (there's a CODE button at the top when you post a message).

    When you get an error messages, copy/paste into your message and indicate the line that gives the error in your posted code.

    You need to add the input to the arraylist for it to work, obviously. You will also need to review the condition on the while loop.

    You will NOT need to keep track of how many items were entered, you can use ArrayList.size() for that.

    HtH
  • 2. Re: ArrayList
    807601 Newbie
    Currently Being Moderated
    Try this:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class MyAL
    {
    static String nameInput;
    static int numberOfNames=0;
    public static void main(String args[]) {
    Scanner input=new Scanner(System.in);
    
    do{
    System.out.print("Enter a name: ");
    nameInput=input.next();
    
    if(nameInput.equals("Exit")) {
    System.out.print("Number of names entered: " +numberOfNames);
    break;
    }
    
    
    System.out.println(nameInput);
    numberOfNames=numberOfNames+1;
    
    
    }
    while(true);
    
    }
    
    }
  • 3. Re: ArrayList
    807601 Newbie
    Currently Being Moderated
    You've got many many mistakes as pointed out. Please study the basic tutorials to learn how to get a program up and running. You can find out more here:
    http://java.sun.com/docs/books/tutorial/index.html