BAEL-6461: What's the difference between Scanner next() and nextLine() methods? (#14057)
This commit is contained in:
parent
98e392f9de
commit
752b49ba02
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class NextVsNextLineUnitTest {
|
||||
|
||||
@Test
|
||||
void givenInput_whenUsingNextMethod_thenReturnToken() {
|
||||
String input = "Hello world";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
assertEquals("Hello", scanner.next());
|
||||
assertEquals("world", scanner.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInput_whenUsingNextMethodWithCustomDelimiter_thenReturnToken() {
|
||||
String input = "Hello :world";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
scanner.useDelimiter(":");
|
||||
|
||||
assertEquals("Hello ", scanner.next());
|
||||
assertEquals("world", scanner.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInput_whenUsingNextLineMethod_thenReturnEntireLine() {
|
||||
String input = "Hello world\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";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
scanner.useDelimiter(":");
|
||||
|
||||
assertEquals("Hello:world", scanner.nextLine());
|
||||
assertEquals("Welcome:to baeldung.com", scanner.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue