-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaDatatypes.java
More file actions
66 lines (55 loc) · 2.58 KB
/
JavaDatatypes.java
File metadata and controls
66 lines (55 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Java Datatypes
Java has 8 Primitive Data Types; they are char, boolean, byte, short, int, long, float, and double. In this problem we are only concerned about integer datatypes used to hold integer values (byte, short, int, long). Let's take a closer look at them:
byte data type is an 8-bit signed integer.
short data type is an 16-bit signed integer.
int data type is an 32-bit signed integer.
long data type is an 64-bit signed integer.
(Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)
Given an integer number, you have to determine which of these datatypes you can use to store that number.
If there are multiple suitable datatypes, list them all in the order above.
Input Format
The first line will contain an integer T, which denotes the number of inputs that will follow.
Each of the next T lines will contain an integer n. The number can be arbitrarily large or small!
Output Format
For each n, list all the datatypes it can be fitted into ordered by the size of the datatype.
If it can't be fitted into any of these datatypes, print "n can't be fitted anywhere." See the sample output for the exact
formatting.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class JavaDatatypes {
static String whoCanFitTheNumber(String numString)
{
String answer = "";
try{
long num = Long.parseLong(numString);
answer = numString + " can be fitted in:\n";
if((num<=Byte.MAX_VALUE) && (num>=Byte.MIN_VALUE)){
answer = answer.concat("* byte\n* short\n* int\n* long");
}else if((num <= Short.MAX_VALUE) && (num >= Short.MIN_VALUE)){
answer = answer.concat("* short\n* int\n* long");
}else if((num <= Integer.MAX_VALUE) && (num >= Integer.MIN_VALUE)){
answer = answer.concat("* int\n* long");
}else{
answer = answer.concat("* long");
}
}catch (NumberFormatException e){
answer = numString+" can't be fitted anywhere.";
}
return answer;
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution.
*/
Scanner scanner = new Scanner(System.in);
int numTestCases = scanner.nextInt() ;
scanner.nextLine();
for(int i=0; i<numTestCases;i++){
String numString = scanner.nextLine();
System.out.println(whoCanFitTheNumber(numString));
}
}
}