简体   繁体   中英

Storing multiple strings in an array list

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?

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);
        }
    }
}

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();
}

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());
}

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());
}

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();
}

}

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);
        }
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM