• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Parse an int from a string

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying to get an integer from a scanned string and can't seem to get it to work. Here's my code:


The idea is to enter an item @ quantity e.g "Shirt @ 2" and put shirt into one ArrayList and qty into another.
qty holds Integers.
Here's the exception I get, it's thrown at the last line of shown code:

Exception in thread "main" java.lang.NumberFormatException: For input string: "shirt "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

Thanks in advance.
 
Greenhorn
Posts: 16
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the input is "Shirt @ 2" and you split using "@" you would have an array like this:
qty1[0] ="Shirt ";
qty1[1] =" 2";
You need to access the second value at the second index instead of the first:
Example
qty1[1]
Remember to trim() your String as your numeric value contains a space.
 
Marshal
Posts: 80128
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use the next() and nextInt() methods of Scanner to read the "Shirt" "@" and 2 separately from the input.
 
Campbell Ritchie
Marshal
Posts: 80128
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also change the delimiter on your Scanner to include whitespace and the at sign @. Beware of the nextLine() method after a nextXXX() method; there is a risk of it returning an empty line.
 
Campbell Ritchie
Marshal
Posts: 80128
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . adn welcome to the Ranch
 
Greenhorn
Posts: 4
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christopher Nortje is correct. That is the exact reason.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic