-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Datatypes.java
More file actions
50 lines (46 loc) · 1.88 KB
/
Java_Datatypes.java
File metadata and controls
50 lines (46 loc) · 1.88 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
/*Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise,
we'll work with the primitives used to hold integer values (byte, short, int, and long):
A byte is an 8-bit signed integer.
A short is a 16-bit signed integer.
An int is a 32-bit signed integer.
A long is a 64-bit signed integer.
Given an input integer, you must determine which primitive data types are capable of properly storing that input.
*/
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));
}
}
}