user3241911
user3241911

Reputation: 481

Can't verify data on Java

I have a problem in Java. I'm trying to check if the input provided only contains decimal number (Number and a ".") This code, provided by my professor, its not really evaluating. And I really can't figure out what is wrong.

import javax.swing.JOptionPane;

public class MoneyCount {

    public static void check(String s) {
        boolean decimalPoint = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '.') {
                if (!decimalPoint) {
                    decimalPoint = true;
                }
            } else {
                JOptionPane.showMessageDialog(null,
                        "You must enter an integer value");
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        //retrieve amount due
        String moneyd = JOptionPane.showInputDialog("Enter the amount due");
        check(moneyd);
        double amountd = Double.parseDouble(moneyd) * 100;

        String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
        check(moneyr);
        double amountr = Double.parseDouble(moneyr) * 100;
    }
}

Upvotes: 0

Views: 134

Answers (6)

Myth
Myth

Reputation: 446

Check this one

public class Test {

public static void main(String[] args) throws ParseException {

    String moneyd = JOptionPane.showInputDialog("Enter the amount due");
    if (check(moneyd)) {
        double amountd = Double.parseDouble(moneyd) * 100;
        System.out.println("ok");
    } else {
        System.out.println("Not a valid number");
        System.exit(0);
    }       

}

public static boolean check(String s) {
    boolean decimalPoint = false;
    for (int i = 0; i < s.length(); i = i + 1) {
        if (s.charAt(i) == '.') {
            if (!decimalPoint) {
                decimalPoint = true;
            } else {
                return (false);
            }
        }
    }
    return true;
}

}

Upvotes: 0

You can use the try and catch to verify if the string is a decimal or not like the above code:

public static void check(String s) {
    try {
        Integer.parseInt(s);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null,
                "You must enter an integer value");
        System.exit(0);
    }
}

Upvotes: 0

user3173787
user3173787

Reputation: 97

What do you mean by "its not really evaluating".

The way I understand the code :

  • It works fine if you enter a correct number (ex: "1", "24.2")
  • Shows an error dialog if you enter an incorrect number (ex: "1.5.6")
  • Throws an exception on anything else (ex: "blabla", "1,2")

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

Start with a desk check (oh dear lord, I never thought I'd say that again)...

Basically all this method does is check to see if the s contains more than one . character

Starting with s equal to 123.123

  • decimalPoint = false
  • When i = 0, s.charAt(i) will be 1.
  • When i = 1, s.charAt(i) will be 2.
  • When i = 2, s.charAt(i) will be 3.
  • When i = 3, s.charAt(i) will be ..
  • decimalPoint is false, change decimalPoint to true
  • When i = 4, s.charAt(i) will be 1.
  • When i = 5, s.charAt(i) will be 2.
  • When i = 6, s.charAt(i) will be 3.

Starting with s equal to 1.2.3

  • decimalPoint = false
  • When i = 0, s.charAt(i) will be 1.
  • When i = 1, s.charAt(i) will be ..
  • decimalPoint is false, change decimalPoint to true
  • When i = 2, s.charAt(i) will be 2.
  • When i = 3, s.charAt(i) will be ..
  • decimalPoint is true, show error message and exit

Starting with s equal to 123

  • decimalPoint = false
  • When i = 0, s.charAt(i) will be 1.
  • When i = 1, s.charAt(i) will be 2.
  • When i = 2, s.charAt(i) will be 3.
  • decimalPoint is false

So the only case which breaks is the second one...Note, this method does not take into consideration what might occur if you enter alpha characters...but that's not what it's designed for...

Upvotes: 0

BeauJest
BeauJest

Reputation: 51

Is the problem to figure out what's wrong with your professor's code or to write your own method?

If it's the former, I'd also point out the intent of the error message should be "You must enter a decimal value".

Upvotes: 1

Myth
Myth

Reputation: 446

try changing your code like this

public static void check(String s) {
    boolean decimalPoint = false;
    for (int i = 0; i < s.length();i++) {
        if (s.charAt(i) == '.') {
            if (!decimalPoint) {
                decimalPoint = true;
            }else{
               JOptionPane.showMessageDialog(null,
                    "Not a valid number (contains more than one .)");
               System.exit(0);

            }
        }
    }
    if (decimalPoint) {
        {
            JOptionPane.showMessageDialog(null,
                    "You must enter an integer value");// you need only integer as input
          System.exit(0);
        }
    }
}

you must show the message only after checking the whole string. That is after completing the loop

Upvotes: 0

Related Questions