Fateh A.
Fateh A.

Reputation: 372

Why won't an IF statement work with rounded floats and doubles?

I am trying to check if a user has inputted a double between 3.5 and 7. I have tried to use the actual double and Math.round();, as well as StrictMath.round(). I have also tried to parse the string inputted as a float but it didn't change anything. Here is the basic code I used:

import java.util.Scanner;

public class IfStatement {
    public static void main(String[] args) {
        //create a Scanner
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first double: ");
        double number = input.nextDouble();
        if (isDouble(number)==true) {
            double x = Double.parseDouble(number);
            if (3.5 <= x <= 7) {
                System.out.println("good");
            } else {
                System.out.println("incorrect input");
            }
        } else {
           System.out.println("incorroct input");
        }
    }
    public static booleen isDouble(String str) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        if (length == 0) {
            return false;
        }
        int i = 0;
        if (str.charAt(0) == '-') {
            if (length == 1) {
                return false;
            }
            ++i;
        }
        int integerPartSize = 0;
        int exponentPartSize = -1;
        while (i < length) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
                    exponentPartSize = 0;
                } else {
                    return false;
                }
            } else if (exponentPartSize > -1) {
                ++exponentPartSize;
            } else {
                ++integerPartSize;
            }
            ++i;
        }
        if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1)
                || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
            return false;
        }
        return true;
    }
}

I have also tried making 3.5 3 in the if statement but got no dice. I just need a loose explanation and not a full solution.

Upvotes: 0

Views: 51

Answers (1)

Ankit Sharma
Ankit Sharma

Reputation: 1654

I've edited your code.

  1. boolean spelling
  2. isDouble() takes a String
  3. a <= x <= b chained expressions are not allowed in Java

Also,

if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1) || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
            return false;
}
return true;

can be simplified to

return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
                && exponentPartSize != 0 && (str.charAt(length - 1) != '.');

Here's the full code.

class Test {
    public static boolean isDouble(String str) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        if (length == 0) {
            return false;
        }
        int i = 0;
        if (str.charAt(0) == '-') {
            if (length == 1) {
                return false;
            }
            ++i;
        }
        int integerPartSize = 0;
        int exponentPartSize = -1;
        while (i < length) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
                    exponentPartSize = 0;
                } else {
                    return false;
                }
            } else if (exponentPartSize > -1) {
                ++exponentPartSize;
            } else {
                ++integerPartSize;
            }
            ++i;
        }
        return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
                && exponentPartSize != 0 && (str.charAt(length - 1) != '.');
    }

    public static void main(String[] args) {
        //create a Scanner
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first double: ");
        String number = input.nextLine();
        if (isDouble(number)) {
            double x = Double.parseDouble(number);
            if (3.5 <= x && x <= 7) {
                System.out.println("good");
            } else {
                System.out.println("incorrect input");
            }
        } else {
            System.out.println("incorroct input");
        }
    }
}

Upvotes: 1

Related Questions