• 0

Some Java Help Needed :)


Question

Hey, I'm new the the world of programming, and my class uses Java (no pun)... Anyway, I've finished the assignment, but I'm trying to delve a little deeper than the requirements... I have been running my program just fine, but I need some if statements that will report an error and show a pop-up if a letter is typed rather than a number. Here is what I have so far for that part...

 // Get the text from the fields
        String lStr = loanField.getText();
        String iStr = interestField.getText();
        String mStr = startField.getText();
        String eStr = endField.getText();
        // Now let's check the strings to make sure someone typed something!
        int checkpoint = 0;
        if (lStr == null || lStr.equals(""))
        {
            JOptionPane.showMessageDialog(this, "You must input a loan amount.");
            checkpoint ++;
        }
        if (iStr == null || iStr.equals(""))
        {
            JOptionPane.showMessageDialog(this, "You must input an interest rate.");
            checkpoint ++;
        }
        if (mStr == null || mStr.equals(""))
        {
            JOptionPane.showMessageDialog(this, "You must input a starting value.");
            checkpoint ++;
        }
        if (eStr == null || eStr.equals(""))
        {
            JOptionPane.showMessageDialog(this, "You must input an ending value.");
            checkpoint ++;
        }
        if (checkpoint  > 0)
            return;
        // Parse text
        double L = Double.parseDouble(lStr);
        double i = Double.parseDouble(iStr);
        double m = Double.parseDouble(mStr);
        double e = Double.parseDouble(eStr);
        // Check each value
        if (L < 0 || L > 1000000)
        {
            JOptionPane.showMessageDialog(this,"Loan amount must be between 0 and 1,000,000.");
            checkpoint ++;
        }
        if (i < 0 || i > 100)
        {
            JOptionPane.showMessageDialog(this,"Interest rate must be greater than 0 and less than 100.");
            checkpoint ++;
        }
        if (m <= 0)
        {
            JOptionPane.showMessageDialog(this,"Starting months must be greater than 0. ");
            checkpoint ++;
        }
        if (m > e)
        {
            JOptionPane.showMessageDialog(this,"Starting months must not be greater than ending months.");
            checkpoint ++;
        }
        if (e <= 0)
        {
            JOptionPane.showMessageDialog(this,"Ending months must be greater than 0. ");
            checkpoint ++;
        }
        if (checkpoint  > 0)
        return;

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Construct a regular expression (regex) that will match the pattern of a sequence of letters. If this is typed, you return an error specifying the user can only type digits.

Some introduction to regex's: http://java.sun.com/docs/books/tutorial/essential/regex/

i.e. if your inputstring is iStr, you can use iStr.match("\\D") which will match non-digits

Link to comment
Share on other sites

  • 0

Construct a regular expression (regex) that will match the pattern of a sequence of letters. If this is typed, you return an error specifying the user can only type digits.

Some introduction to regex's: http://java.sun.com/docs/books/tutorial/essential/regex/

i.e. if your inputstring is iStr, you can use iStr.match("\\D") which will match non-digits

Awesome, thanks... We haven't learned that yet, but it will be a good thing to learn before going to class tomorrow.

Link to comment
Share on other sites

  • 0

Construct a regular expression (regex) that will match the pattern of a sequence of letters. If this is typed, you return an error specifying the user can only type digits.

Some introduction to regex's: http://java.sun.com/docs/books/tutorial/essential/regex/

i.e. if your inputstring is iStr, you can use iStr.match("\\D") which will match non-digits

That is going WAY overboard. You do not need to use regex. This isn't perl or php. Most of the cases if you think a regex is the solution, you're doing it wrong. You want to use the Double.parseDouble method which will generate an exception if the string cannot be formatted as a double (contains letters or any non-numerical character).

You already have that to convert your numbers. You just need to add the try { ... } catch { ... } block similar to the following. :)

public boolean isNum(String s) {
    try {
        Double.parseDouble(s);
    }
    catch (NumberFormatException e) {
        return false;
    }
    return true;
}

Link to comment
Share on other sites

  • 0

That is going WAY overboard. You do not need to use regex. This isn't perl or php. Most of the cases if you think a regex is the solution, you're doing it wrong. You want to use the Double.parseDouble method which will generate an exception if the string cannot be formatted as a double (contains letters or any non-numerical character).

You already have that to convert your numbers. You just need to add the try { ... } catch { ... } block similar to the following. :)

public boolean isNum(String s) {
    try {
        Double.parseDouble(s);
    }
    catch (NumberFormatException e) {
        return false;
    }
    return true;
}

Hey, thanks that's awesome. I was trying the regex way, and I felt that it was a little overboard, so I emailed my professor. He said to do it exactly like you just did. Thanks again :)

Link to comment
Share on other sites

  • 0

That is going WAY overboard. You do not need to use regex. This isn't perl or php. Most of the cases if you think a regex is the solution, you're doing it wrong. You want to use the Double.parseDouble method which will generate an exception if the string cannot be formatted as a double (contains letters or any non-numerical character).

You already have that to convert your numbers. You just need to add the try { ... } catch { ... } block similar to the following. :)

public boolean isNum(String s) {
    try {
        Double.parseDouble(s);
    }
    catch (NumberFormatException e) {
        return false;
    }
    return true;
}

I'm actually still having a little trouble with this... Where do I actually put this? Do I need to change anything in the code I originally posted?

Link to comment
Share on other sites

  • 0

Give this a whirl.

try {
    double L = Double.parseDouble(lStr);
    double i = Double.parseDouble(iStr);
    double m = Double.parseDouble(mStr);
    double e = Double.parseDouble(eStr);
}
catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(this, "Only real numbers are valid.");
    checkpoint++;
}

Btw, did you intentionally name your variables to spell out Lime? :p

Link to comment
Share on other sites

  • 0

Give this a whirl.

try {
    double L = Double.parseDouble(lStr);
    double i = Double.parseDouble(iStr);
    double m = Double.parseDouble(mStr);
    double e = Double.parseDouble(eStr);
}
catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(this, "Only real numbers are valid.");
    checkpoint++;
}

Btw, did you intentionally name your variables to spell out Lime? :p

Lol... NO I didn't but I noticed that when I posted it and thought it was awesome... I'll try this and let you know :)

EDIT: I just tried it, but when I add that, It tells me it can't find any of my other variables...

Link to comment
Share on other sites

  • 0

Ah, sorry bout that.

double L, i, m, e;
try {
    L = Double.parseDouble(lStr);
    i = Double.parseDouble(iStr);
    m = Double.parseDouble(mStr);
    e = Double.parseDouble(eStr);
}
catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(this, "Only real numbers are valid.");
    return;
}

I just realized should return out of the exception as well. If this exception happens, you really don't want the other stuff to trigger.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.