Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have to create the following program:

Create a program that allows you to type names. The names that the user types and submits (with the enter key) should be stored in an arraylist. The names in the arraylist should then be printed on the screen.

My problem is that I've only been able to only store one name in my arraylist.

My code:

ArrayList <String> namen = new ArrayList <>();        
System.out.print("Enter a name: ");        
Scanner in = new Scanner(System.in);        
namen.add(in.next());        
for (String naam: namen) {
    System.out.println(naam);
}

The first problem is that after one name has been typed and submitted, you can't type anything, so I have to reset the input like with integer values:

if (varOne > varTwo) {
    System.out.println("Too high!");
    varOne = raadGetal.nextInt();
}

The varOne = raadGetal.nextInt(); line resets the input, so you can type again. But how do I achieve the same thing with the code above, with Strings and an arraylist?

share|improve this question
up vote 1 down vote accepted

You always declare ArrayList<String> namen = new ArrayList<>(); after that it is an empty list. Try something like this:

public class MyClass {
    private ArrayList<String> namen = new ArrayList<>();

    public void addName(){
        System.out.print("Enter a name: ");
        Scanner in = new Scanner(System.in);
        namen.add(in.next());

        for(String naam: namen){
            System.out.println(naam);
        }
    }
}
share|improve this answer
    
The ArrayList definition in the OP's program is correct. He's using the diamond operator (<>) on the right hand side which was introduced with Java 7. – helpermethod Nov 22 '13 at 10:03
    
But that list is empty afterwards, so it contains only one name and the OP want it to have more names in it. – hbsrud Nov 22 '13 at 10:07

Comparison operator > is not supported by String. Use compareTo method to compare String value.

if(varOne.compareTo(varTwo)>0) {
System.out.println("Too high!");
varOne = raadGetal.nextInt();
}
share|improve this answer
    
varOne is being read as int as the OP mentioned: varOne = raadGetal.nextInt(); in his question – Sage Nov 22 '13 at 10:03

The problem isn't your ArrayList, it's the Scanner usage. If you want to ask for multiple names, use a loop.

for(int i = 0; i < NUMBER_OF_NAMES_TO_GET; i++){
    namen.add(in.next());
}
share|improve this answer
    
Thank you very much! This snippet did the job! Only one little question, how can I let the user input unlimited names? As far as I know a while loop is for running code when you don't know when the loop should stop. Can you help me with something like that? – user2793161 Nov 23 '13 at 19:16
    
@user2793161 you are absolutely right about the usage of a while loop. You can say something like while (!namen.contains("exit")) instead of the for loop. This will quit once someone types "exit", keep in mind that you should then remove the exit name from the namen list. – Ron E Nov 25 '13 at 4:34
    
Okay thanks a lot Ron! It works perfectly now. The assignment was that when a user enters an empty string, the loop should stop and the last inputted value before the empty string should be removed. – user2793161 Nov 27 '13 at 19:54
    
@user2793161 You're welcome, you should upvote/accept answers so others can find the best solution too! – Ron E Nov 28 '13 at 0:12

You can ask the user first the number of names he wants to give

int noOfNames = in.nextInt();

and then based on this you can create a loop to store all the names

for(int i = 0; i <noOfNames ; i++){
    names.add(in.next());
}
share|improve this answer

Try calling it in the loop. Its just executing once. try the below:-

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

public class addName {

private void name() {
    ArrayList<String> namen = new ArrayList<String>();

    int count = 1;

while(count>0){
    System.out.print("Enter a name: ");
    Scanner in = new Scanner(System.in);
    String a = in.next();
    namen.add(a);
    if (a.equals("exit")) {
        System.out.println("Exiting");
        System.exit(0);
    }
    else{
        for (String naam : namen) {
                System.out.println(naam);
    }
    }
    count++;
}
}

public static void main(String[] args) {
    addName addNames = new addName();
    addNames.name();
}

}

share|improve this answer

Lutz, You can write/modify your code as follows:
please note that you should always use String.equals() method for checking the equality of two Strings.

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

public class AddNames {

public static void main(String[] args) {

    ArrayList<String> namen = new ArrayList<String>();
    System.out.print("Enter a name(or write exit to close the program) : ");
    Scanner in = new Scanner(System.in);
    String name = in.next();
    while (!name.equals("exit")) {

        namen.add(name);
        name = in.next();
    }
    if (name.equals("exit")) {
        System.out
                .println("Exiting the program. The names you have entered are:");
        for (String naam : namen) {
            System.out.println(naam);
        }
    }
}
}
share|improve this answer
    
Thanks for your snippet! – user2793161 Nov 27 '13 at 19:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.