2014-09-13 167 views
1

我想知道如何檢查用戶的輸入是否是某種原始類型(我的意思是整數,字符串等等......我認爲它被稱爲原始類型?)。我想要一個用戶輸入一些內容,然後檢查它是否是一個String,並相應地執行某個操作。 (JAVA) 我已經看到了一些代碼是這樣的:如何在JAVA中檢查輸入是整數還是字符串等?

if (input == (String)input) { (RANDOM STUFF HERE) } 

或類似

if input.equals((String) input) 

而且他們不工作。我想知道如何才能只檢查一個字符串?(編輯出) 我想知道是否有一個功能可以做到這一點?感謝您的答案

編輯:在大家的幫助下我已經創建了我的固定代碼,做什麼,我希望它:

package files; 
import java.util.*; 
public class CheckforSomething { 
static Scanner inputofUser = new Scanner(System.in);  
static Object userInput; 
static Object classofInput; 
public static void main(String[] args){ 
    try{ 
    System.out.print("Enter an integer, only an integer: "); 
    userInput = inputofUser.nextInt(); 

    classofInput = userInput.getClass(); 
    System.out.println(classofInput); 
    } catch(InputMismatchException e) { 
     System.out.println("Not an integer, crashing down"); 

    } 

} 



} 

無需再回答,謝謝!

+0

*是什麼*'input'? *它來自哪裏,以及*如何*?即。如果它是一個字符串,那麼它將永遠是一個字符串..和演員沒有任何意義。 – user2864740 2014-09-13 04:55:24

回答

1

使用的instanceof根據你的類型檢查的類型和類型轉換:

public class A { 

    public static void main(String[]s){ 

     show(5); 
     show("hello"); 
    } 
    public static void show(Object obj){ 
     if(obj instanceof Integer){ 
      System.out.println((Integer)obj); 
     }else if(obj instanceof String){ 
      System.out.println((String)obj); 
     } 
    } 
} 
+0

這適用於上下文,但不適用於掃描儀。:( – jiggumbob 2014-09-13 06:13:24

+0

如果我從'stdio'輸入信息呢?在這種情況下我需要幫助。 – roottraveller 2016-01-03 09:43:36

-1

嘗試的instanceof函數整數,而不是INT ..每個原始還有一類

+1

你能給更多的描述或代碼嗎?我不知道如何使用它,這將有助於感謝 – jiggumbob 2014-09-13 04:50:47

-1

這是好嗎?

class Test 
{ 
    public static void main(String args[]) 
    { 
     java.util.Scanner in = new java.util.Scanner(System.in); 
     String x = in.nextLine(); 
     System.out.println("\n The type of the variable is : "+x.getClass()); 
    } 
} 

輸出:

[email protected]:~/Desktop$ javac Test.java 
[email protected]:~/Desktop$ java Test 
hello 

The type of the variable is : java.lang.String 
2

您可以用正則表達式試試這個:

String input = "34"; 
if(input.matches("^\\d+(\\.\\d+)?")) { 
    //okay 
} else { 
    // not okay ! 
} 

這裏,

^\\d+說,輸入以數字0-9開頭,

()?可能/或可能不會發生

\\.允許輸入

-1

一個週期,但Zechariax使用嘗試捕捉

爲此,可以使用NumberForamtter和一個ParsePosition實現希望與出一個答案。 看看這個解決方案

import java.text.NumberFormat; 
import java.text.ParsePosition; 


    public class TypeChecker { 
     public static void main(String[] args) { 
     String temp = "a"; // "1" 
     NumberFormat numberFormatter = NumberFormat.getInstance(); 
     ParsePosition parsePosition = new ParsePosition(0); 
     numberFormatter.parse(temp, parsePosition); 
     if(temp.length() == parsePosition.getIndex()) { 
      System.out.println("It is a number"); 
     } else { 
      System.out.println("It is a not number"); 
     } 
    } 
} 
+0

這看起來非常好,但我並不高級(對不起),並改變了我的想法整個嘗試抓住它看起來比這個複雜的代碼更容易。但謝謝:) – jiggumbob 2014-09-13 05:21:25

+0

我同意Zechariax。嘗試catch是一個簡單的解決方案。但是如果你沒有這個選項,你可以隨時使用它。 – Govind 2014-09-13 05:29:56

+0

好吧,還有一種方法可以使用if(randomVar =(int)){}或類似的東西嗎?這將是最有用的。 – jiggumbob 2014-09-13 05:34:44

相關問題