aspose file tools
The moose likes Beginning Java and the fly likes NumberFormatException problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "NumberFormatException problem" Watch "NumberFormatException problem" New topic
Author

NumberFormatException problem

A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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?
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
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.


Life called,so here I am.<br />Cheers<br />Niki.:-)
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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

Joined: Mar 20, 2005
Posts: 256
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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!


[Jess in Action][AskingGoodQuestions]
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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.
Venkatraman Kandaswamy
Ranch Hand

Joined: Jul 07, 2004
Posts: 120
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.


--Venkatraman<br />SCJP 1.4<br /><a href="http://kvrlogs.blogspot.com" target="_blank" rel="nofollow">blog</a>
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: NumberFormatException problem
 
Similar Threads
Integer automatically assumed from a long string of numbers in a URL parameter?
Help with number guessing game
My way for multi-client failed?
problematic run-time errors
Lost, need help