Софтуерно Инженерство
Loading...
bond4o avatar bond4o 8 Точки

[03. Periodic Table] Sets and Maps

public class PeriodicTable {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeSet<String> elements = new TreeSet<>();
        Long n = scanner.nextLong();
        for (Long i = 0L; i < n; i++ ) {
            String[] input = scanner.nextLine().split(" ");
            elements.addAll(Arrays.asList(input));
        }
        for (String element : elements) {
            System.out.print(element + " ");
        }
    }
}
Получавам 40/100 и не мога да разбера къде е проблема.
Тагове:
0
Java Advanced 20/05/2016 16:20:25
bond4o:
issue resolved
svetljo avatar svetljo 27 Точки
Best Answer

С една мъничка добавка, твоят код проработи:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    TreeSet<String> elements = new TreeSet<>();
    Long n = scanner.nextLong();
    scanner.nextLine();
    
    for (Long i = 0L; i < n; i++ ) {
        String[] input = scanner.nextLine().split(" ");
        elements.addAll(Arrays.asList(input));
    }
    for (String element : elements) {
        System.out.print(element + " ");
    }
}

Сложих scanner.nextLine();, което изчита реда докрай.

Ето малко инфо от нета: https://stackoverflow.com/questions/5032356/using-scanner-nextline

I think your problem is that

int selection = scanner.nextInt();

reads just the number, not the end of line or anything after the number. When you

String sentence = scanner.nextLine();

This read the remainder of the line with the number on it (with nothing after the number I suspect)

Try placing a scanner.nextLine(); after each nextInt() if you intend to ignore the rest of the line.

 

Дано съм ти свършил работа.

Поздрави! Светльо

0
bond4o avatar bond4o 8 Точки

Благодаря, не го забелязах това, защото обикновенно ползвам само nextLine() и после парввам към число, именно заради този проблем.

0