I want to know whether the user inputs a char or an int, and hence take a different course of action depending upon the data type.
How to determine numeric Data Type in Java?
523 views Asked by AudioBubble At
2
There are 2 answers
0
Arnault Le Prévost-Corvellec
On
try java std parser :
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String inputString = bufferedReader.readLine();
Integer integerValue = getIntegerValueOf(inputString);
if (integerValue == null) {
Double doubleValue = getDoubleValueOf(inputString);
if (doubleValue == null) {
System.out.println("I don't know");
} else {
System.out.println("Congratulation, it's a double : " + doubleValue);
}
} else {
System.out.println("Congratulation, it's a integer : " + integerValue);
}
}
private static Integer getIntegerValueOf(String inputString) {
try {
return Integer.parseInt(inputString);
} catch (NumberFormatException e) {
return null;
}
}
private static Double getDoubleValueOf(String inputString) {
try {
return Double.parseDouble(inputString);
} catch (NumberFormatException e) {
return null;
}
}
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in TYPES
- Need clarification on VHDL expressions involving std_logic_vector, unsigned and literals, unsure about compiler interpretation
- Adding a different string to a table fails
- The type of B is displayed as A when `type B = A` is used. Why is it displayed as `any` when `type B = A | A` is used instead?
- why we got same data type in two versions like "int" and "integer" in php?
- Handling NaN entries in a dataframe created from CSV
- Cannot find type definition file for 'node' in react project
- Correct way to count types in whole corpus
- Typescript: how to get possible keys from const with limited values?
- Having two Image types in React TypeScript one for upload, one for display
- MOOC.fi Java Programming course 1 - Exercise 13 "Exercises" Part 6 - Compilation error
- Is is a mistake to use type keyword after curly braces in TS when importing constants and files fro one file?
- type annotations needed, try using a fully qualified path to specify the expected types
- Need a simple example how to catch a data type error en C++
- Pyspark reads data as string but on Mongo they are double
- Extract a Maybe from a heterogeneous collection
Related Questions in INT
- DECIMAL values being converted to INT + 1 on its own
- Int++ operator isn't increasing the int the first time it's ran
- when converting uint/int to numbers what format should i use ? Little endian or LSB & MSB?
- Understanding SQL Server Int Data Types
- Eclipse - Comp Issues
- How can i move a frame with buttons in tkinter?
- How can I find out the number of characters in a String in Java?
- Error: "int" object is not subscriptable in python
- Postgresql : cast uuid to int
- how do I resolve the following error - TypeError: unsupported operand type(s) for -: 'int' and 'str'
- Does JavaScript store reference to every element in array of ints?
- Inconsistent "Required type: byte provided: int" in Java
- How to cout a vector from an outside function?
- I want to use one int in two scripts and decrease it in the second one
- How do you get the type of a primitive literal in Java?
Related Questions in DOUBLE
- Dynamic array of structures in C++/ cannot fill a dynamic array of doubles in structure from dynamic array of structures
- Convert Cell Array of Symbolic Functions to Double Array of Symbolic Functions MATLAB
- why does the default round function in python fix double rounding errors?
- Imprecision in float integers in C
- How much accuracy is lost due to representing time as a IEEE 754 binary64 number?
- Java printf not working as expected when dividing
- Can't use double, can't use int, what do I use to stop the rounding? (Java)
- Determining the Upper Limit of Math.random() Output in Java
- How to set double precision for elements of an array in Fortran 90
- Why is double printing the same value as float in C when they have different sizes?
- Calculate lowest common indivisible number in JavaScript
- matrix type in R conversion
- How can i solve the double scrollbar problem?
- Facing issue while converting 18 digit double value to long
- How Can Calculators Display So Many Digits of PI?
Related Questions in METADATATYPE
- EF Core DataAnnotations not used in Razor page
- MetadataType don't work with partial class in ASP.NET MVC
- DataTypeAnnotations MetadataType not working as expected
- MetadataType and Attribute.IsDefined
- How to determine numeric Data Type in Java?
- C# Custom Attributes not being applied
- ModelMetadataType has no effect on model in asp.net core 2.0
- Getting most basic MetaDataType to work
- EF6 Migrations Precision in MetadataType
- ASP.NET Web API Help Page MetadataType Data Annotations
- Json schema exception no protocol error when creating a metadata type in Mule using a Json schema
- Barcode scanning exclude AVmetadaTypeFace
- How to specify in metadata which column to use for display value
- How to edit the metadata information of an object in cloud storage bucket using golang
- Using a MetadataType with WCF
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
You can read line as
Stringand then check string with regex. For example if you are reading fromstdin