This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to use Integer.parseInt()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use Integer.parseInt()?" Watch "How to use Integer.parseInt()?" New topic
Author

How to use Integer.parseInt()?

Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I am working on a project to create a calculator, and to accept input, I am using the Scanner class to record the input given as a String. I then want to parse that String into an int. However, I don't know how to parse it properly. Here is my code.


This is the error message I get.


-DEBUG: The String calculation has just been created.
-DEBUG: The Scanner has just been created.
-DEBUG: The boolean calculateFinished has just been created.
12+6
-DEBUG: The input has just been recorded.
Exception in thread "main" java.lang.NumberFormatException: For input string: "12+6"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)
at java.lang.Integer.parseInt(Integer.java:499)
at JavaCalculatorMain.main(JavaCalculatorMain.java:17)

"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Alix Ollivier wrote:Exception in thread "main" java.lang.NumberFormatException: For input string: "12+6"

12+6 is not a valid number
You need to parse that in to tokens first.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

I thought that, because I can set up an int like this: int a =12+6, and have it print out 18, that it would work in parsing. How do I parse in tokens?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
Java has no built-in facilities for this. You can use a parser like this one that I wrote some time ago, or use a library like JEP or JEL.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38032
    
  22
If you are using Scanner, it has methods which give you an int directly. You would find it very difficult to parse a String like “12+6”, however. “12 + 6”, with spaces required, would be easier. Look here and here.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

The reason I don't want to use Scanner to scan ints directly is that if I add a graphical interface on later, I want the display panel to display an equation like this: 1+2+3/4, and follow the order of operations. If I do that with an int, it'll calculate too fast. I'm not sure if my reasoning is solid, cause I'm sleepy, but yeah.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38032
    
  22
You can pass a String to a Scanner: new Scanner("12 + 6");
But if you want to parse it, you must have something, eg whitespace, to delimit the numbers from the operators.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

If I add whitespace, what does that change?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Alix Ollivier wrote:I am working on a project to create a calculator...However, I don't know how to parse it properly...

Hmmm. Can I suggest that this might be the basic problem?

And if that sounds obvious, then why are you writing code?

You cannot solve a problem by coding; you can only solve it by thinking...and reading...and scribbling...and thinking some more.
And only when you've done all that and know what you want to do, then you open your Java editor...

Believe me, things will become an awful lot simpler when you get into that habit.

However, just to set you on your way:
1. If this expression involves exactly TWO numbers and an operator in between, then forget the rest of this list.
Otherwise:
2. The way we puny humans write mathematical expression is called "infix" notation and computers hate it, because it's horribly illogical.
3. The way computers like to deal with mathematical expressions is with something called Reverse Polish Notation (RPN).
4. A very clever Dutchman called Edsker Dijkstra invented the Shunting yard algorithm to deal with this precise problem: converting puny human expressions to RPN.

If you're only interested in point 1, then you need to look at String.split() and see how you might break up your simple expression.

If you're interested in general parsing of infix expressions (points 2-4), I suspect you're not quite ready for #4; and that's unfortunately where it ends (for the moment; but hopefully only for the moment). If you want to prove me wrong, by all means have a crack at it, and I'll try and help you if you do. But it seems a bit of a stretch at the moment to me.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18534
    
  40


This may be throwing a monkey wrench in the debate, as I agree with Winston's previous post... however....

Ulf Dittmer wrote:Java has no built-in facilities for this. You can use a parser like this one that I wrote some time ago, or use a library like JEP or JEL.


Arguably, using Java 6, it actually does. One possibility is to route the expression to the Java scripting engine -- meaning use the default (javascript) engine to parse the expression.

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Henry Wong wrote:This may be throwing a monkey wrench in the debate, as I agree with Winston's previous post... however...One possibility is to route the expression to the Java scripting engine -- meaning use the default (javascript) engine to parse the expression.

Nope, absolutely no monkey wrench; just good sense.

@Alix: Henry's post brings up a very good point (which I completely forgot about): Don't re-invent the wheel.

It has to be said that using a scripting engine is likely to be a lot slower than a pure Java-based solution, but hey, if it's fast enough, that's all you need.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use Integer.parseInt()?
 
Similar Threads
Questions about primitive types - Fibonacci sequence calculator
Complete novice question
Adding a new array element is not saving to memory
Scanner Class Error!!
Scanner is skipping