[英]Check if input is of Primitive type in Java?
我必須檢查每個輸入並打印出它是原始類型還是引用類型的實例。 但我每次都得到相同的輸出。
注意:我確實搜索過,但沒有運氣。 如何在 JAVA 中檢查輸入是整數還是字符串等?
public class Demo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<9; ++i){
String input = br.readLine();
showPremitive(input);//check for every input
}
}
public static void showPremitive(Object input){
try{
if (input instanceof Short)
System.out.println("Primitive : short");
else if(input instanceof Integer)
System.out.println("Primitive : int");
else if(input instanceof Long)
System.out.println("Primitive : long");
else if(input instanceof Float)
System.out.println("Primitive : float");
else if(input instanceof Double)
System.out.println("Primitive : double");
else if(input instanceof Boolean)
System.out.println("Primitive : bool");
else if(input instanceof Character)
System.out.println("Primitive : char");
else if(input instanceof Byte)
System.out.println("Primitive : byte");
else
System.out.println("Reference : string");
}
catch (InputMismatchException e){
System.out.println("Exception occur = "+e);
}
}
}
輸出:
Reference : string
Reference : string
Reference : string
Reference : string
Reference : string
Reference : string
Reference : string
Reference : string
Reference : string
您將輸入分配給String
變量。 這將是一個字符串。
String input = br.readLine();
//^^^ it's a string
// ^^^ readLine() returns String
使用您擁有的方法可能無法實現您想要實現的目標。
您正在使用java.io.BufferedReader.readLine()
進行輸入。
public String readLine()
throws IOException
無論輸入實際包含什么,它都會返回一個String
。
例如, "0"
是一個字符串,即使它包含一個數字並且可以轉換為一個int
。
一種可能的方法(如果有內置或現有方法,我不知道)是反復嘗試將輸入轉換為您要檢查的每種數據類型。 如果沒有匹配,則打印“字符串”。 例如:
String input = br.readLine();
try{
int i = Integer.parseInt(input);
System.out.println("It's an int!");
}catch(Exception e){
//well, guess not
//do the same for all the other types
}
在這里,我從int
開始。 但是,請確保從最獨特的類型開始。 例如,一個int
可以轉換為一個double
。 因此,首先檢查int
,或者當它可以放入int
時您可以說它是double
(例如,輸入是5
,在檢查它是否為double
之前檢查它是否為整數)。
在我解決您的問題之前,您似乎對 Java 和Scanner
有一些誤解。
您從輸入流中讀取的內容是字符或字節。 這些字符或字節可以被解釋為代表許多事物……但這是一個解釋問題。 此外,相同的字符序列可能意味着不同的事物……取決於您選擇如何解釋它們; 例如,“12345”可以很容易地解釋為字符串、整數或浮點數。 如果你將它映射到 Java 類型,那么它可能是String
、 short
、 int
、 long
、 float
或double
……等等。
關鍵是……你(程序員)必須告訴解析器(例如Scanner
)會發生什么。 你不能指望它(正確地)猜測。
假設您已設法讀取為引用或(真正的)原始類型,您將無法將它們分配給相同的變量。 Integer
等不是原始類型!
Scanner.readLine()
方法讀取當前行的其余部分並將其作為字符串返回。 它沒有試圖解釋它。 結果的 Java 類型是String
...,僅此而已。
那么你應該如何實現它。 好吧,這是一個粗略版本的草圖:
String input = br.readLine();
showPrimitive(input); // Note spelling!
public void showPrimitive(String input) {
if (input.equalsIgnoreCase("true") ||
input.equalsIgnoreCase("false")) {
System.out.println("Primitive : boolean");
return;
}
try {
long num = Long.parseLong(input);
if (num >= Byte.MIN_VALUE &&
num <= Byte.MAX_VALUE) {
System.out.println("Primitive : byte");
} else if (num >= Short.MIN_VALUE &&
num <= Short.MAX_VALUE) {
System.out.println("Primitive : short");
} else if (num >= Integer.MIN_VALUE &&
num <= Integer.MAX_VALUE) {
System.out.println("Primitive : int");
} else {
System.out.println("Primitive : long");
}
return;
} catch (NumberFormatException ex) {
// continue
}
// deal with floating point (c.f. above)
// deal with char: input length == 1
// anything else is a String.
}
請注意,上述內容需要以很多人反對的方式使用異常。 但是,做得更好是很棘手的……如果您要支持每種原始類型的所有可能值。
但我會回到我之前提出的觀點。 如果您查看上面的代碼,它會在如何解釋輸入方面做出硬性選擇。 但是你怎么知道你是否做出了正確的選擇呢? 答案:您必須指定輸入解析的行為方式......不要依靠其他東西來魔術給您“正確”的答案。
通過檢查每種數據類型的范圍。 請參考以下公式計算原始數據類型的范圍。 -2^n-1 到 +2^n-1
int Byte=0;
int Short=0;
int Int=0;
int Long=0;
long temp=1;
for(int i=1;i<=63;i++)
{
temp=temp*2;
}
long end=temp-1;
long temp1=temp*2;
temp=temp-temp1;
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=-128 && x<=127)
{
Byte++;
}
if(x>=-32768 && x<=32767)
{
Short++;
}
if(x>=-2147483648 && x<=2147483647)
{
Int++;
}
if(x>=temp && x<=end)
{
Long++;
}
if(Byte>0 && Short>0 && Int>0 && Long>0)
{
System.out.println("* byte");
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
Byte=0;
Short=0;
Int=0;
Long=0;
}
else if(Short>0 && Int>0 && Long>0)
{
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
Short=0;
Int=0;
Long=0;
}
else if(Int>0 && Long>0)
{
System.out.println("* int");
System.out.println("* long");
Int=0;
Long=0;
}
else if(Long>0)
{
System.out.println("* long");
Long=0;
}
else
{
}
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
預期輸出:
2
11111111
11111111 can be fitted in:
* int
* long
1
1 can be fitted in:
* byte
* short
* int
* long
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.