• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use Integer.parseInt()?

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I add whitespace, what does that change?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic