JAVA-23392 Adding some more example for differences between Scanner n… (#14491)

* JAVA-23392 Adding some more example for differences between Scanner next() vs nextLine()

* Update NextVsNextLineUnitTest.java

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
timis1 2023-08-01 18:14:56 +03:00 committed by GitHub
parent 8f344ca0f7
commit 50147f3c8e
1 changed files with 36 additions and 0 deletions

View File

@ -17,6 +17,24 @@ class NextVsNextLineUnitTest {
}
}
@Test
void givenInput_whenUsingNextMethodWithMultipleWhiteSpaces_thenReturnToken() {
String input = "Hello world";
try (Scanner scanner = new Scanner(input)) {
assertEquals("Hello", scanner.next());
assertEquals("world", scanner.next());
}
}
@Test
void givenInput_whenUsingNextMethodWithTabAndNewLine_thenReturnToken() {
String input = "Hello \t\n world";
try (Scanner scanner = new Scanner(input)) {
assertEquals("Hello", scanner.next());
assertEquals("world", scanner.next());
}
}
@Test
void givenInput_whenUsingNextMethodWithCustomDelimiter_thenReturnToken() {
String input = "Hello :world";
@ -37,6 +55,24 @@ class NextVsNextLineUnitTest {
}
}
@Test
void givenInput_whenUsingNextLineMethodWithCR_thenReturnEntireLine() {
String input = "Hello world\rWelcome to baeldung.com";
try (Scanner scanner = new Scanner(input)) {
assertEquals("Hello world", scanner.nextLine());
assertEquals("Welcome to baeldung.com", scanner.nextLine());
}
}
@Test
void givenInput_whenUsingNextLineMethodWithCRLF_thenReturnEntireLine() {
String input = "Hello world\r\nWelcome to baeldung.com";
try (Scanner scanner = new Scanner(input)) {
assertEquals("Hello world", scanner.nextLine());
assertEquals("Welcome to baeldung.com", scanner.nextLine());
}
}
@Test
void givenInput_whenUsingNextLineWithCustomDelimiter_thenIgnoreDelimiter() {
String input = "Hello:world\nWelcome:to baeldung.com";