예제가 포함된 Java의 Scanner nextLine() 메서드
그만큼 다음라인() 의 방법 java.util.스캐너 클래스는 현재 행을 지나서 이 스캐너를 진행하고 건너뛴 입력을 반환합니다. 이 함수는 현재 줄의 나머지 부분을 인쇄하고 끝에 줄 구분 기호를 남겨 둡니다. 다음은 줄 구분 기호 뒤에 설정됩니다. 이 메서드는 줄 구분 기호를 찾기 위해 입력을 계속 검색하므로 줄 구분 기호가 없으면 건너뛸 줄을 검색하여 모든 입력을 검색할 수 있습니다.
통사론:
public String nextLine()
매개변수: 이 함수는 어떤 매개변수도 허용하지 않습니다.
반환 값: 이 메소드는 선 그건 건너뛰었어
예외: 이 함수는 아래 설명된 대로 두 가지 예외를 발생시킵니다.
- NoSuchElementException: 행이 발견되지 않은 경우 발생 IllegalStateException: 이 스캐너가 닫힌 경우 발생
아래 프로그램은 위의 기능을 보여줍니다.
프로그램 1:
// Java program to illustrate the> // nextLine() method of Scanner class in Java> // without parameter> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > String s => 'Gfg
Geeks
GeeksForGeeks'> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > // print the next line> > System.out.println(scanner.nextLine());> > > // print the next line again> > System.out.println(scanner.nextLine());> > > // print the next line again> > System.out.println(scanner.nextLine());> > > scanner.close();> > }> }> |
산출:
Gfg Geeks GeeksForGeeks
프로그램 2: NoSuchElementException을 보여주기 위해
// Java program to illustrate the> // nextLine() method of Scanner class in Java> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > try> {> > > String s => ''> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > System.out.println(scanner.nextLine());> > scanner.close();> > }> > catch> (Exception e) {> > System.out.println(> 'Exception thrown: '> + e);> > }> > }> }> |
산출:
Exception thrown: java.util.NoSuchElementException: No line found
프로그램 3: IllegalStateException을 보여주기 위해
// Java program to illustrate the> // nextLine() method of Scanner class in Java> // without parameter> > import> java.util.*;> > public> class> GFG1 {> > public> static> void> main(String[] argv)> > throws> Exception> > {> > > try> {> > > String s => 'Gfg'> ;> > > // create a new scanner> > // with the specified String Object> > Scanner scanner => new> Scanner(s);> > > scanner.close();> > > // Prints the new line> > System.out.println(scanner.nextLine());> > scanner.close();> > }> > catch> (Exception e) {> > System.out.println(> 'Exception thrown: '> + e);> > }> > }> }> |
산출:
Exception thrown: java.lang.IllegalStateException: Scanner closed
참조: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()