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 Overflow
Top answer
1 of 3
2

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.

2 of 3
2

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 i can pass Float.parseFloat(), and
  • confirm that i contains 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 String so simply bump the counter, then next token
🌐
GitHub
github.com › RyanFehr › HackerRank › blob › master › Algorithms › Implementation › Strange Counter › Solution.java
HackerRank/Algorithms/Implementation/Strange Counter/Solution.java at master · RyanFehr/HackerRank
//Problem: https://www.hackerrank.com/challenges/strange-code · //Java 8 · /* · Brute forces is to keep a counter and wait until it equals t and return our current value · · int cycle = 3; · int step = 1; · int value = 3; · while(step != t) ·
Author   RyanFehr
🌐
YouTube
youtube.com › codelive
java type counter hackerrank solution github - YouTube
Get Free GPT4o from https://codegive.com certainly! the "java type counter" problem on hackerrank generally requires you to count the occurrences of differe...
Published   November 1, 2024
Views   64
🌐
YouTube
youtube.com › codelive
java type counter hackerrank certification solution - YouTube
Get Free GPT4o from https://codegive.com certainly! the java type counter problem on hackerrank requires you to count the number of occurrences of each type...
Published   November 1, 2024
Views   46
🌐
YouTube
youtube.com › playlist
Hackerrank Solutions - YouTube
Share your videos with friends, family, and the world
🌐
GitHub
github.com › khan-mujeeb › HackerRank-Certification-Solution- › blob › main › java(basics) › readme.md
HackerRank-Certification-Solution-/java(basics)/readme.md at main · khan-mujeeb/HackerRank-Certification-Solution-
Here you can find HackerRank Certification Solution - HackerRank-Certification-Solution-/java(basics)/readme.md at main · khan-mujeeb/HackerRank-Certification-Solution-
Author   khan-mujeeb
🌐
HackerRank
hackerrank.com › contests › code-leader › challenges › string-counter
String Counter | Code Leader Question | Contests
Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.
🌐
StudyX
studyx.ai › homework › 106599536-hacker-rank-java-type-counter-basic-certificate-question-solution
hacker rank Java type counter basic | StudyX
October 5, 2024 - This code will read input until EOF, count how many integers and strings are entered, and print the results. Adjust the type identification logic based on the specific requirements of your HackerRank question.
Find elsewhere
🌐
YouTube
youtube.com › watch
java type counter hackerrank solution
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
HackerRank
hackerrank.com › challenges › strange-code › problem
Strange Counter | HackerRank
There is a strange counter. At the first second, it displays the number . Each second, the number displayed by decrements by until it reaches . In next second, the timer resets to and continues counting down.
🌐
GitHub
gist.github.com › AbdullahMagat › 153b444d7f197cd197c82313f7857949
Hackerrank Java Datatypes Solution · GitHub
Hackerrank Java Datatypes Solution. GitHub Gist: instantly share code, notes, and snippets.
🌐
HackerRank
hackerrank.com › challenges › counter-game › forum
Counter game Discussions | Algorithms | HackerRank
Counter games are all about strategy and timing, which makes them surprisingly similar to survival horror experiences like thepoppytime. In both, you have to stay alert, anticipate moves, and act quickly to avoid setbacks.
🌐
GitHub
github.com › ozan › solutions › blob › master › hackerrank › counter-game.c
solutions/hackerrank/counter-game.c at master · ozan/solutions
Solutions to various problems in various languages - solutions/hackerrank/counter-game.c at master · ozan/solutions
Author   ozan
🌐
GitHub
github.com › sonmez-hakan › HackerRank › blob › master › Algorithms › Bit Manipulation › counter-game.java
hackerrank/Algorithms/Bit Manipulation/counter-game.java at master · sonmez-hakan/hackerrank
This repository is mostly Java & PHP solutions of HackerRank Algorithms & Data Structures' Questions. However, there are some C# solutions. - hackerrank/Algorithms/Bit Manipulation/counter-game.java at master · sonmez-hakan/hackerrank
Author   sonmez-hakan
🌐
HackerRank
hackerrank.com › challenges › count-solutions › problem
Count Solutions | HackerRank
Eric has four integers , , , and · Instantly, he wondered how many pairs of integers, , satisfy the following equation:
Top answer
1 of 2
1

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.");
        }

    }
}
2 of 2
1

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().

🌐
Scribd
es.scribd.com › document › 702344224 › JAVA-HACKERRANK-SOLUTIONS
Java Hackerrank Solutions | PDF | Software | Software Engineering
JAVA HACKERRANK SOLUTIONS - Free download as Text File (.txt), PDF File (.pdf) or read online for free. The document contains 16 code snippets from Java lessons on various topics: 1. The first snippet prints a welcome message in Java. 2. The second snippet takes user input and prints it back out.
Rating: 2.3 ​ - ​ 3 votes