掃描儀類toString()方法 (Scanner Class toString() method)
toString() method is available in java.util package.
toString()方法在java.util包中可用。
toString() method is used to string denotation of this Scanner and it contains information related to tracing.
toString()方法用于對此掃描程序進行字符串表示,并且包含與跟蹤有關的信息。
toString() 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.
toString()方法是一種非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
toString() method does not throw an exception at the time of string denotation of this Scanner.
在此掃描程序的字符串表示形式時, toString()方法不會引發異常。
Syntax:
句法:
public String toString();
Parameter(s):
參數:
It does not accept any parameter.
它不接受任何參數。
Return value:
返回值:
The return type of the method is String, it returns string representation of this Scanner.
該方法的返回類型為String ,它返回此Scanner的字符串表示形式。
Example:
例:
// Java program to demonstrate the example
// of String toString() method of Scanner
import java.util.*;
public class ToStringOfScanner {
public static void main(String[] args) {
String str = "Hi, true \n IncludeHelp! 8 + 2.0f = 10.0f";
// Instantiate Scanner with the
// given str
Scanner sc = new Scanner(str);
// Display Scanner
System.out.println("sc.nextLine(): " + sc.nextLine());
// By using toString() method is to
// represent string for this Scanner
System.out.println("sc.toString(): " + sc.toString());
// close the scanner
sc.close();
}
}
Output
輸出量
sc.nextLine(): Hi, true
sc.toString(): java.util.Scanner[delimiters=\p{javaWhitespace}+][position=10][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]
翻譯自: https://www.includehelp.com/java/scanner-tostring-method-with-example.aspx