Java Scanner nextLine()方法与示例
掃描儀類nextLine()方法 (Scanner Class nextLine() method)
nextLine() method is available in java.util package.
nextLine()方法在java.util包中可用。
nextLine() method is used to get the skipped line.
nextLine()方法用于獲取跳過的行。
nextLine() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
nextLine()方法是一種非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
nextLine() method may throw an exception at the time of returning a new line.
nextLine()方法在返回新行時可能會引發異常。
- NoSuchElementException: This exception may throw when no line exists.NoSuchElementException :不存在任何行時,可能引發此異常。
- IllegalStateException: This exception may throw when this Scanner is not opened.IllegalStateException :如果未打開此掃描器,則可能引發此異常。
Syntax:
句法:
public String nextLine();Parameter(s):
參數:
It does not accept any parameter.
它不接受任何參數。
Return value:
返回值:
The return type of the method is String, it returns skipped line.
該方法的返回類型為String ,它返回跳過的行。
Example:
例:
// Java program to demonstrate the example // of String nextLine() method of Scanner import java.util.*;public class NextLineOfScanner {public static void main(String[] args) {String str = "Hi, true \n IncludeHelp! 8 + 2.0f = 10.0f";// Instantiate Scanner with the // given strScanner sc = new Scanner(str);// Display ScannerSystem.out.println("sc.nextLine(): " + sc.nextLine());// Display NextLineSystem.out.println("sc.nextLine(): " + sc.nextLine());// close the scannersc.close();} }Output
輸出量
sc.nextLine(): Hi, true sc.nextLine(): IncludeHelp! 8 + 2.0f = 10.0f翻譯自: https://www.includehelp.com/java/scanner-nextline-method-with-example.aspx
總結
以上是生活随笔為你收集整理的Java Scanner nextLine()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python打印多个变量_在Python
- 下一篇: java美元兑换,(Java实现) 美元