From 2ea83407d3327cffc1b09ea568a0ed3456f2f334 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Tue, 23 May 2023 23:50:10 +0200 Subject: [PATCH] =?UTF-8?q?[scanner-with-spaces]=20how=20to=20take=20input?= =?UTF-8?q?=20as=20String=20with=20spaces=20in=20Java=E2=80=A6=20(#14044)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [scanner-with-spaces] how to take input as String with spaces in Java using Scanner?y * [scanner-with-spaces] fix typo --- .../scanner/InputWithSpacesUnitTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java new file mode 100644 index 0000000000..8a93c6ce65 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/scanner/InputWithSpacesUnitTest.java @@ -0,0 +1,97 @@ +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import org.junit.jupiter.api.Test; + +import com.google.common.collect.Lists; + +public class InputWithSpacesUnitTest { + @Test + void whenValuesContainSpaces_thenNextBreaksTheValue() { + String input = new StringBuilder().append("Michael Jackson\n") + .append("He was the 'King of Pop'.\n") + .toString(); + + Scanner sc = new Scanner(input); + String name = sc.next(); + String description = sc.next(); + assertEquals("Michael", name); + assertEquals("Jackson", description); + } + + @Test + void whenOneValuePerLineUsingNextLine_thenGetExpectedResult() { + String input = new StringBuilder().append("Michael Jackson\n") + .append("He was the 'King of Pop'.\n") + .toString(); + + Scanner sc = new Scanner(input); + String name = sc.nextLine(); + String description = sc.nextLine(); + assertEquals("Michael Jackson", name); + assertEquals("He was the 'King of Pop'.", description); + } + + @Test + void whenOneValuePerLineUsingNewLineAsDelimiter_thenGetExpectedResult() { + String input = new StringBuilder().append("Michael Jackson\n") + .append("He was the 'King of Pop'.\n") + .toString(); + + Scanner sc = new Scanner(input); + sc.useDelimiter("\\n"); + String name = sc.next(); + String description = sc.next(); + assertEquals("Michael Jackson", name); + assertEquals("He was the 'King of Pop'.", description); + } + + @Test + void whenValuesAreSeparatedByCommaUsingSplit_thenGetExpectedResult() { + String input = "Michael Jackson, Whitney Houston, John Lennon\n"; + + Scanner sc = new Scanner(input); + String[] names = sc.nextLine() + .split(", "); + assertArrayEquals(new String[] { "Michael Jackson", "Whitney Houston", "John Lennon" }, names); + } + + @Test + void whenValuesAreSeparatedByCommaSettingDelimiterWithoutNewline_thenGetExpectedResult() { + String input = new StringBuilder().append("Michael Jackson, Whitney Houston, John Lennon\n") + .append("Elvis Presley\n") + .toString(); + + Scanner sc = new Scanner(input); + sc.useDelimiter(", "); + List names = new ArrayList<>(); + while (sc.hasNext()) { + names.add(sc.next()); + } + //assertEquals(Lists.newArrayList("Michael Jackson", "Whitney Houston", "John Lennon", "Elvis Presley"), names); <-- Fail + assertEquals(3, names.size()); + assertEquals("John Lennon\nElvis Presley\n", names.get(2)); + + } + + @Test + void whenValuesAreSeparatedByCommaSettingDelimiter_thenGetExpectedResult() { + String input = new StringBuilder().append("Michael Jackson, Whitney Houston, John Lennon\n") + .append("Elvis Presley\n") + .toString(); + + Scanner sc = new Scanner(input); + sc.useDelimiter(", |\\n"); + List names = new ArrayList<>(); + while (sc.hasNext()) { + names.add(sc.next()); + } + assertEquals(Lists.newArrayList("Michael Jackson", "Whitney Houston", "John Lennon", "Elvis Presley"), names); + } +} \ No newline at end of file