Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions Java/Introduction/JavaDatatypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,29 @@

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class JavaDatatypes {

static String whoCanFitTheNumber(String numString)
class Solution{
public static void main(String []argh)
{
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");
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();

for(int i=0;i<t;i++)
{

try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=-128 && x<=127)System.out.println("* byte");
if((x <= Short.MAX_VALUE) && (x >= Short.MIN_VALUE)) System.out.println("* short");
if((x <= Integer.MAX_VALUE) && (x >= Integer.MIN_VALUE))System.out.println("* int");
if((x <= Long.MAX_VALUE) && (x >= Long.MIN_VALUE))System.out.println("* 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));
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}

}
}
}
}