掃描儀類hasNextShort()方法
語法:public?boolean?hasNextShort();
public?boolean?hasNextShort(int?rad);hasNextShort()方法在java.util包中可用。
hasNextShort()方法用于檢查此掃描程序在其輸入中是否具有下一個標記,是否可以將其作為隱式基數中的short值進行操作。
hasNextShort(int rad)方法用于檢查此掃描程序在其輸入中是否具有下一個標記,可以將其作為顯式基數(rad)中的short值進行操作。
在將輸入表示為短值時,這些方法可能會引發異常。
IllegalStateException:如果未打開此掃描器,則可能引發此異常。
這些是非靜態方法,可通過類對象訪問;如果嘗試使用類名稱訪問這些方法,則會收到錯誤消息。
參數:在第一種情況下,hasNextShort(),它不接受任何參數。
在第二種情況下,hasNextShort(int rad),int rad –表示用作短值的基數。
返回值:
在這兩種情況下,該方法的返回類型均為boolean,當此Scanner下次輸入時作為短有效值,它將返回true,否則返回false。
示例//Java程序是演示示例
//的hasNextShort()掃描儀的方法
import?java.util.*;
import?java.util.regex.*;
public?class?HasNextShort?{
public?static?void?main(String[]?args)?{
String?str?=?"Java?Programming!?3?*?8=?24";
Short?val?=?125;
str?=?str?+?val;
//實例化掃描儀
Scanner?sc?=?new?Scanner(str);
while?(sc.hasNext())?{
//通過使用hasNextShort()方法是
//檢查該對象是否下一個標記
//在默認情況下表示是否簡短
//基數
boolean?status?=?sc.hasNextShort();
System.out.println("sc.hasNextShort():?"?+?status);
//通過使用hasNextShort(radix)方法可以
//檢查該對象是否下一個標記
//表示給定基數中的不足
//是否
status?=?sc.hasNextShort(4);
System.out.println("sc.hasNextShort(2):?"?+?status);
sc.next();
}
//掃描儀關閉
sc.close();
}
}
輸出結果sc.hasNextShort():?false
sc.hasNextShort(2):?false
sc.hasNextShort():?false
sc.hasNextShort(2):?false
sc.hasNextShort():?true
sc.hasNextShort(2):?true
sc.hasNextShort():?false
sc.hasNextShort(2):?false
sc.hasNextShort():?false
sc.hasNextShort(2):?false
sc.hasNextShort():?true
sc.hasNextShort(2):?false