jeudi 7 mai 2020

i want to put user input and store it in an array when user types Exit, it should print the names the user typed in, ascending.. any tips?

So here is my code. i´ve not managed to use the ascending.. i want the user to type infinite amount of names and then type exit to get the result..

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

public class Arr {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("enter some Names");
        ArrayList<String> names = new ArrayList<String>();

        while (scanner.hasNextLine()) {
            if (scanner.hasNextLine()) {
                names.add(scanner.next());
            } else {
                String s1 = scanner.next();
                if ("exit".equalsIgnoreCase(s1)) {
                    break;
                }
            }
        }

        String string = "the Names you wrote are : ";
        for (String x : names) {
            System.out.print(x);
        }
        scanner.close();

    }
}

Aucun commentaire:

Enregistrer un commentaire