![](/img/trans.png)
[英]Java library for converting Object to numeric (Integer, Long etc..)
[英]How can I check if an input is a integer or String, etc.. in 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");
}
}
}
不需要回答了,謝謝!
使用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);
}
}
}
你可以用 Regex 試試這個:
String input = "34";
if(input.matches("^\\d+(\\.\\d+)?")) {
//okay
} else {
// not okay !
}
這里,
^\\\\d+
表示輸入以數字 0-9 開頭,
()?
可能/也可能不會發生
\\\\.
允許輸入一個句號
Scanner input = new Scanner (System.in);
if (input.hasNextInt()) System.out.println("This input is of type Integer.");
else if (input.hasNextFloat()) System.out.println("This input is of type Float.");
else if (input.hasNextLine()) System.out.println("This input is of type string.");
else if (input.hasNextDouble()) System.out.println("This input is of type Double.");
else if (input.hasNextBoolean()) System.out.println("This input is of type Boolean.");
else if (input.hasNextLong())
System.out.println("This input is of type Long.");
class Main{
public static void main(String args[]){
String str;
Scanner sc=new Scanner(System.in);
int n,
boolean flag=false;
while(!flag){
try{
str=sc.nextLine();
n=Integer.parseInt(str);
flag=true;
}
catch(NumberFormatException e){
System.out.println("enter an no");
str=sc.nextLine();
}
}
}
}
討厭在 6 年后提出這個問題,但我找到了另一種可能的解決方案。
目前正在參加編碼訓練營,不得不解決類似的問題。 我們引入布爾值並根據 try/catch 塊的結果更改它們的值。 然后我們使用簡單的 if 語句檢查布爾值。 您可以省略打印並輸入您的代碼。 這是它的樣子:
import java.io.IOException;
import java.util.Scanner;
public class DataTypeFinder {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String input = "";
while (true) { //so we can check multiple inputs (optional)
input = scan.nextLine();
if ("END".equals(input)) { //a way to exit the loop
break;
}
boolean isInt = true; //introduce boolean to check if input is of type Integer
try { // surround with try/catch
int integer = Integer.parseInt(input); //boolean is still true if it works
} catch (NumberFormatException e) {
isInt = false; //changed to false if it doesn't
}
boolean isDouble = true; //same story
try {
double dbl = Double.parseDouble(input);
} catch (NumberFormatException e) {
isDouble = false;
}
if (isInt) {
System.out.printf("%s is integer type%n", input);
} else if (isDouble) {
System.out.printf("%s is floating point type%n", input);
} else if (input.length() == 1) { //this could be useless depending on your case
System.out.printf("%s is character type%n", input);
} else if ("true".equals(input.toLowerCase()) || "false".equals(input.toLowerCase())) {
System.out.printf("%s is boolean type%n", input);
} else {
System.out.printf("%s is string type%n", input);
}
}
}
}
這個可以嗎?
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());
}
}
輸出:
subham@subham-SVE15125CNB:~/Desktop$ javac Test.java
subham@subham-SVE15125CNB:~/Desktop$ java Test
hello
The type of the variable is : java.lang.String
但是 Zechariax 想要一個不使用 try catch 的答案
您可以使用 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");
}
}
}
嘗試使用 Integer 而不是 int 的 instanceof 函數..每個原語也有一個類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.