aspose file tools*
The moose likes Beginning Java and the fly likes Calculator program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculator program" Watch "Calculator program" New topic
Author

Calculator program

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I'm almost finished with a calculator program and I would like to add an exception in order to catch the user trying to compute a number with more than one decimal. An example of what i would like to catch would look like this 3..45*3. Is there a way I can create an exception that would throw out an "Error can only use single decimal"

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The solution might involve a "boolean haveSeenDecimal = false" which gets set to true at the obvious place (and which must be rest to false whenever firstInput is set to false).
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Dustin,

I guess you'd probably have to use Regex (regular expressions). Try looking it up (read the Java tutorials).

Then you can do something like encode the fact that you want (one or many digits), followed by (only one decimal point), followed by (one or more digits), followed by (one or more spaces), followed by the operator (*, /, +, -), followed by (one or many digits, only one decimal point, and again, one or more digits).

I'm not sure but something like: \d+([\.]\d+)?\s*[\*\+\-\/]\s*\d+([.]\d+)?

Note, that since Java uses the '\' character as an escape character, the regex will end up like this in Java:

\\d+([\\.]\\d+)?\\s*[\\*\\+\\-\\/]\\s*\\d+([.]\\d+)?

i.e. double every backslash.

Anyway, here's the English version:

1 or more digits, one or no ". followed by 1 or more digits", 0 or more spaces, either *, +, -, or /, 0 or more spaces, and the rest is similar to the first bit.

Then you'd have to compile this pattern and match it with the users' input. Read the Java tutorials for more detail.

Regards,

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Note that regex would permit the unusual entry of 0000 + 0123.45

It is not usual to start numbers with a zero or use multiple zeroes like that.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Campbell,

Well yes, you'd have to play around with it a bit (regex is like that unfortunately).

This one's perhaps an improvement, then if you find another hole in it, you'd have to stay patching it up of course...

[1-9]\d*([\.]\d+)?\s*[\*\+\-\/]\s*[1-9]\d*([.]\d+)?

Regards,

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
I think that would not allow 0.123. How about (0)|([1-9]\d*)(\.\d*)? . . . ?

That will allow 0. 123. but not .123. The latter can be permitted with a strategically-placed ? and maybe another pair of ()
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Campbell,

You're absolutely right, that was another hole lol.

((0)|([1-9]\d*))([\.]\d*)?

matches 0.123

Actually, here's the output of a small program which tests the stuff:

Enter regex: ((0)|([1-9]\d*))([\.]\d*)?
Enter string: 0.123
Match.
Enter string:
Another? (Y or N) y

Enter regex: ((0)|([1-9]\d*))([\.]\d*)?\s*[\*\+\-\/]\s*((0)|([1-9]\d*))(\.\d+)?
Enter string: 0.123 * 7.666
Match.
Enter string: 88.65 / 0.433
Match.
Enter string:

And so,

((0)|([1-9]\d*))(\.\d+)?\s*[\*\+\-\/]\s*((0)|([1-9]\d*))(\.\d+)?

seems to be it.

Regards,

Justin

btw, the code for the test program is from All in one Java (a for dummies book). I'm not sure if I can just copy paste the code here, but just so you know, I believe the code is available on-line, google for dummies all in one or something.

oh another btw, you don't need to make a class out of .

i.e. [.]
or
[\.]

can just be . or \.

Anyway, small stuff
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Agree about small stuff . . . and while I was cycling through the cemetery realised that my regex with the ? in could match an empty string.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Campbell,

I'm not sure what you were referring to by the "match an empty String" part, but that's probably no problem in this context right? I didn't read Dustin's code but from what I can tell the user is inputting values through a graphical interface... there's no room for entering a "" or empty String, is there?

btw, if you were referring to the extra grouping i.e. ( ) which I added to your:

(0)|([1-9]\d*)(\.\d*)?

to make it:

((0)|([1-9]\d*))([\.]\d*)?

that's because I wanted: Either 0 or 1-9 followed by 0 or more digits, AND THEN (i.e. after either or has been decided/matched) 0 or 1 . followed by 0 or many digits

Without the extra pair of parenthesis, it would be:

0 OR 1-9 followed by 0 or more digits followed by 0 or 1 occurrence of a . followed by 0 or more digits.

Something like that. Anyway, I don't see the matching of an empty String, but I don't think that can affect the calculator program.

So Dustin, I know this might be complicating your program a bit, but regex are pretty powerful. You can use this program you're making to learn a bit about them. I don't know much about regex myself, but with a bit of the basics under your belt you'll be able to do some pretty flexible String matching.

Regards,

Justin
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ugh.

Disallow multiple decimal point entry in the first place, just like a calculator.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
@David,

hmmm lol yeah that would work
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Agree about the extra ().

If you have (. . .)?(. . .)? then both options can be 0 times, so it would match an empty String. Or am I mistaken?
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Campbell,

hmm I'm not sure. It makes sense that an empty String would match I guess... but I'm not sure. You'd have to check out how the ? modifier works exactly...

But still, I don't see how matching an empty String would be a bad thing. The user needs to give in a String in the form of:

((0)|([1-9]\d*))(\.\d+)?\s*[\*\+\-\/]\s*((0)|([1-9]\d*))(\.\d+)?

so an empty String would obviously not match.

Can you give me an example of when you think matching an empty String would cause the regex to match something it shouldn't match? Because, I'm not understanding the problem here...

Note that (. . .)?(. . .)? is not the case above:

Above, the case is something like:

(this | that)(...)?(blah blah)(this | that)(...)?

so in other words, you can or you cannot have the "dot followed by 1 or more digits" part, but not you can or you cannot have the whole number and so a "" will not match the operand part.

operands:

143 -> match
12.8 -> match
0.87 -> match
.98 -> does not match
037.8 -> does not match

Sorry if I re-stated the obvious but I'm not sure what we're talking about here.

Regards,

Justin
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The problem would go away if you're validate input as keys are hit.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi David,

Yes, that's true. I prefer your solution to the problem actually. You'd probably use a stack or something to push in the operands with every operator the user inputs. Also, when the user is inputting operands, you allow the decimal button to input a decimal point only if the user hasn't already done so for the operand in question. Or something like that.

That's a lot easier than using regex.

Regards,

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23
Agree that what you have written would not match an empty String because your first (. . .) is not followed by a ?

That would not match .123 however. As people have already said, however, we are getting a long way away from the real point of this thread.
Justin Calleja
Greenhorn

Joined: Sep 18, 2010
Posts: 18
Hi Campbell,

lol that's true. That usually happens with regex (to me at least). You think you got it right, for sure you got it right... and then you find another hole. lol it's half way between funny and damn right annoying. Don't get me wrong. I still think regex are pretty powerful, but in cases where you can get by without them, it's probably best not to use them.

Regards,

Justin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38821
    
  23


Shall we let the original poster have his thread back?
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I think i'm understanding regex but i'm trying to implement it into my program but i'm doing it wrong i'm thinking. This is the code I have so far.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, the pattern should be in a string, for starters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculator program