As Integer is a sub-type of Float, Float will count in Integers into your float count. If you solely just want the number of float that is not integer, just take float count minus integer count.
Answer from sean on Stack OverflowAs Integer is a sub-type of Float, Float will count in Integers into your float count. If you solely just want the number of float that is not integer, just take float count minus integer count.
Some tokens (like "50") are parseable both as integer and float. Your code gives both of them a chance, even though your intention is to only count each token as one type or the other (so count "50" as an integer, not as a float).
A simple fix could be to modify your float check to be:
- make sure that
ican passFloat.parseFloat(), and - confirm that
icontains a decimal point
That code edit could look like this:
Float.parseFloat(i);
if (i.contains(".")) {
counter++;
continue;
}
Or, as a more involved alternative, instead of checking all input tokens for floats, then re-checking all input tokens for integers, I would take a different approach.
First, I would change the code to stop checking the entire input string repeatedly. This is helpful for 2nd part below, but also cleans up an unnecessary inefficiency of re-checking tokens even if you've already identified their data type. If you had a large number of tokens, it's wasteful to try parsing all of them as float, then try all of them as integer, then try a third time to count plain strings.
Second, since you would be checking (and counting) each token only one time, you could:
- check for integer (before float) – if it passes
Integer.parseInt(), increment that counter and move on to the next token - if it doesn't parse as integer, make an attempt to parse that token as float – just
Float.parseFloat()without looking for decimal; if it worked then bump counter, move to next token - if it doesn't parse as integer or float, you know it's a
Stringso simply bump the counter, then next token
Videos
Problem in you code is that you use double pow(double a, double b). When you have result closer to Long.MAX_VALUE - the result is rounded. You can check it by BitInteger:
String str1 = BigInteger.valueOf(2).pow(bits[j] - 1).toString();
String str2 = String.valueOf((long)Math.pow(2, bits[j] - 1));
These strings should be equal every time, but for 2^16 - it's not. That's why for 2^16 your x <= range - 1 is not correct.
You do not need to use for loop to check this. Java has constants like Byte.MIN_VALUE, Byte.MAX_VALUE.
public static void main(String[] argh) {
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 >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
System.out.println("* byte");
if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE)
System.out.println("* short");
if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE)
System.out.println("* int");
System.out.println("* long");
} catch(Exception e) {
System.out.println(sc.next() + " can't be fitted anywhere.");
}
}
}
Just omit the check for 64 bits and long.
If long x = sc.nextLong(); succeeds without exception, you already know that the number fits into the long type. So, you can always print "* long" if you didn't get an exception.
One remark on using Math.pow(): it's based on floating-point arithmetic and as such, it might involve small rounding errors. Powers of 2 can be computed by bit-shifting, so
long range = (long) (Math.pow(2, bits[j] - 1))
can be replaced by
long range = 2L << (bits[j] - 1)
This gives exact results and also is faster than Math.pow().