wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes parseInt problem 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 "parseInt problem" Watch "parseInt problem" New topic
Author

parseInt problem

Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
i seem to be having a lot of problems with two dimensional arrays today

normally, I use Integer.parseInt and it works just fine
However it throws a number format exception in these statements

for (int i=0; i<len; i++)
{
String status1 = entryfields[i][0];
statusnum = Integer.parseInt(status1);
out.print(status1);
}

when i comment out the parseInt part, it outputs status1 just fine.
how come it's not working?

please help

thanks
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
Integer.parseInt will throw an exception if the value pass is a string that is alphanumeric/alpha only or it has decimal point e.g.(1.0,1.1)


jherald
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
Originally posted by Jherald Lacambra:
Integer.parseInt will throw an exception if the value pass is a string that is alphanumeric/alpha only or it has decimal point e.g.(1.0,1.1)


The values in the String I am trying to convert are only
1,2,3,etc and other single digit numbers
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
Originally posted by shuini gustav:


The values in the String I am trying to convert are only
1,2,3,etc and other single digit numbers


can you post your code so that we can have a better understanding of your problem
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9953
    
161

Make sure that the String that is passed to parseInt in NOT null. If you still have problems, post the exception stacktrace here


[My Blog] [JavaRanch Journal]
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
yes. i'm positive that the Strings passed is NOT null

This is the stack trace

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: For input string: "
1"
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:207)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)


root cause

java.lang.NumberFormatException: For input string: "
1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:518)
at org.apache.jsp.readtextfile_jsp._jspService(readtextfile_jsp.java:122)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

There seems to be some kind of carriage return before your number.


[My Blog]
All roads lead to JavaRanch
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
the string pass is not purely numeric as the string passed should be "1" not "1
"
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Using trim() from the String class is helpful in avoiding these types of problems. Especially when you can not guarantee the data.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
gee thanks, i didn't know that

it works now
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
' ' is a character(ascii value 0x20) and " " is a string with a character in it, not an empty string. So " 123" is a string of lenghth 4. Funny how little things can trip you up.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

' ' is a character(ascii value 0x20) and " " is a string with a character in it


I think that it was actually a carriage return in the string, not a space(0x20), which trim() also removes. Otherwise, the error message would have displayed " 1", without going to the next line.
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
ok here's the thing.

so it won't error, instead of spaces or carriage returns, i populated every null entry with "x" or "xx", depending on how long the entry is, to make sure there are no extra unwanted characters or something.
so now, it doesn't like x's too??? coz it outputs this

java.lang.NumberFormatException: For input string: "x"

and then i tried and changed the "x" with an "a"
and it says this

java.lang.NumberFormatException: For input string: "a"

i'm really confused now
[ February 21, 2006: Message edited by: shuini gustav ]
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
oh ok, i get it.

it can't parse the x and a coz it can't parse it into an integer because they're characters. is that it?

so i tried replacing it with a space
but it still outputs the error.

can't it just like put null into my integer if the variable to be parsed contains non numbers?
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
i also tried this but still throws the same exception
area1 contains "x"

String area1 = entryfields[i][1];
int areanum = Integer.parseInt(area1,16);
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
this doesn't work too and its supposed to
still throws the same exception

String hexVal = "A"; //Hexadecimal
String octVal = "81"; //Octal
String binVal = "1101"; //Binary

System.out.println("Hex = "+ hexVal + ",Decimal = "+Integer.parseInt(hexVal,16));
System.out.println("Hex = "+ octVal + ",Decimal = "+Integer.parseInt(octVal,8));
System.out.println("Hex = "+ binVal + ",Decimal = "+Integer.parseInt(binVal,2));

Can someone please help me? i've been at this whole day
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9953
    
161

still throws the same exception


Please post the exception stacktrace that you are seeing when you execute the following code:

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9953
    
161

String octVal = "81"; //Octal


On taking a closer look, 81 is NOT a valid Octal number.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parseInt problem
 
Similar Threads
parseInt() method
Downloading and saving/opening a file
Difference between Exceptions thrown by JVM and thrown Programatically
NullPointerException
Type casting a character to an Integer