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

NumberFormatException problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I manipulate a string I get from a chat room in irc using pircbot.
I search for certain text and pick out some numbers.
I try to parse those numbers, but I get NumberFormatExceptions.
Here's the output:



The weird part is, 4141 looks to me like a normal number.
Im confused.
Any Ideas what could cause this?
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isnt your number stored in a string?
i say this because in the error you can see the number as "4141".
Possibly that is the problem as you havent converted to integer or any number format.
check if you are using string or any number datatype.
 
A. Wolf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops I see I didn't make it clear in my first post, line 217 in MyBot.java contains this:



so yes "4141" is a string that I want to convert to int, but it fails to do so, and I have no idea why.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code:-



of yours works fine.
i tried it.
I declared varB and varC as strings with some number values.
and i printed out varA. it works fine.
are varB and varC strings???
if they are then it works fine.
please paste the actual code with the declaration and value assignment to varB and varC.
Then maybe we can find a solution.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect the key is that you're on IRC, and people are typing funny. It's hard to tell the difference between a "1" (numeral one) and an "l" (letter ell) in many fonts. Are you sure you don't have four ell, four ell? You could try

System.out.println((int) varB.charAt(1));

The code for "one" is 49; anything else isn't!
 
A. Wolf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably right about the jibberish characters from people in IRC. I'll try using that char to int casting and see if i can get to the cause of this. Thanks everyone for your suggestions.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might have tried this - did you do a varB.trim() and varC.trim() before parsing it? and also you can wrap it with a try catch and look out for number format exception - and if it occurs set your varA to spaces or some error value.
 
A. Wolf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow this is amazing. after casting each character to an integer and printing it out I can see what the problem was. since I was using the indexOf function looking for " " <-spaces to divide my text, there was no way it was going to correctly pick out the number I wanted. Take for example 3087
The way it appeared: 3087
After putting "|" between each character: | ||3|0|8|7|| |
as Integers: 32|2|51|48|56|55|2|32
Notice the pipes "|" are only there to separate the integers one from another.
you will notice the 2's after, and at the end before the 32's ( space ) dont really belong in there,
so the 2's - whatever character that is? ascii table says "start of text" was causing the NumberFormatException because I included them in the text to be parsed.

Weird problem, glad its solved. Thanks everyone.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic