WAP in java to input a three digit number and calculate each digit's frequency

Viewed 37

I made a program for the question and it's working fine, but in some cases it's not working like when I enter 656, it's showing like this:

The error

The code is showed below:

public static void main(String[] args) {
    Scanner rx = new Scanner(System.in);
    int ui,uiy,troll3,troll1;
    float uix,uis,uiz,uit;
    System.out.println("Enter a valid three digit number to calculate the frequency of the digits in it. \n");
    ui = rx.nextInt();
    if(ui>99&&ui<=999) {

        uis = (float) ui;
        //System.out.println(uis+" uis");
        uix = uis / 10;
        //System.out.println(uix+" uix");
        uiy = (int) uix;
        //System.out.println(uiy+" uiy");
        troll3 = (int) ((uix - uiy) * 10); //1st digit
        //System.out.println("3d " + troll3);
        uiz = uix / 10;
        //System.out.println(uiz+ " uiz");
        troll1 = (int) uiz;
        //System.out.println("1d " + troll1);
        uit = (uiz - troll1) * 10;
        //System.out.println(uit+" uit");
        int troll2 = (int) uit;
        //System.out.println("2d " + troll2);
        if (troll1 == troll2 && troll1 == troll3) {
            System.out.println("The number " + troll1 + " appears three times.");
        } else if (troll1 != troll2 && troll2 != troll3 && troll1 != troll3) {
            System.out.println("The number " + troll1 + " appears one time.");
            System.out.println("The number " + troll2 + " appears one time.");
            System.out.println("The number " + troll3 + " appears one time.");
        } else if (troll1 == troll2) {
            System.out.println("The number " + troll1 + " appears two times.");
            System.out.println("The number " + troll3 + " appears one time.");
        } else if (troll1 == troll3) {
            System.out.println("The number " + troll3 + " appears two times.");
            System.out.println("The number " + troll2 + " appears one time.");
        } else if (troll2 == troll3) {
            System.out.println("The number " + troll2 + " appears two times.");
            System.out.println("The number " + troll1 + " appears one time.");
        }
    }
    else{

        System.out.println("The entered number is invalid");
    }





}

It mostly gives an error when it consists of digit 5 in the middle. It shows an increment in values and swap in values. Please do help.

Thanks in advance! :-)

1 Answers

Why are you converting to float? float and double attempts to represent an infinite infinity of numbers (there are an infinite amount of integers. Between 2 integers, there are an infinite amount of numbers too: An infinite amount of infinities)... using only 32 bits. This is obviously impossible so instead only a few numbers are representable, and anything else is silently rounded to one of the select few. This means float and double introduce rounding errors.

After any math done to any double or float, == is broken. You can't use those; at best, you can try 'delta equality' (not a == b, but Math.abs(a - b) < 0.00001) but making the claim that your code works for all possible inputs becomes very difficult indeed, it's not going to be very fast, and the code readability isn't great either. So, don't.

Stop using floats, problem solved.

Your 'math' to get the individual digits is a bit circumspect and isn't going to just work if you replace things with int either. What you're missing is the % operator: Module (a.k.a. remainder).

Given, say, 656:

int in = 657;
int digit1 = in % 10;
in = in / 10;
System.out.println(in); // 65
System.out.println(digit1); // 7
int digit2 = in % 10;
in = in / 10;
System.out.println(in); // 6
System.out.println(digit1); // 5
int digit3 = in;
Related