Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem when converting String to int

0 views
Skip to first unread message

George

unread,
Feb 8, 2002, 4:45:26 PM2/8/02
to
When I compile ReadConfigFile.java, doesn't have no problem, but
everytime when I ran it, which read the whole line from console.dat
and always got the this exception.
Thanks
java.lang.NumberFormatException: 4498
at java.lang.Integer.parseInt(Integer.java:423)
at java.lang.Integer.parseInt(Integer.java:463)
at pulse.ReadConfigFile.readConfig(ReadConfigFile.java:36)
at pulse.MainPath.doPost(MainPath.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
at org.apache.tomcat.core.Handler.service(Handler.java:287)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
at .........

C:/console.dat
MESSAGE_SERVER_IP=localhost
SERVER_STATUS_PORT=4498
SERVER_CMD_PORT=4497
MAIN_REFRESH_TIMEINTERVAL=2
POOL_REFRESH_TIMEINTERVAL=2


ReadConfigFile.java
package pulse;
import java.io.*;
import java.util.*;
import java.util.StringTokenizer;

public class ReadConfigFile {
static FileReader fileReader;
static BufferedReader bd;
public static String msgServerIp = null;
public static int serverStatusPort;
public static int serverCmdPort;
public static void readConfig() throws Exception{
fileReader = new FileReader("C:/console.dat");
bd = new BufferedReader(fileReader);
String lineRead = new String();
while((lineRead = bd.readLine()) != null){
StringTokenizer stTok = new StringTokenizer(lineRead, "=");
while (stTok.hasMoreTokens()) {
String firstToken = stTok.nextToken();
System.out.println("First String Token is:" + firstToken);
if(firstToken.equals("MESSAGE_SERVER_IP")){
msgServerIp = stTok.nextToken();
System.out.println("Server IP is:" + msgServerIp);
}
else if(firstToken.equals("SERVER_STATUS_PORT")){
String ssp = stTok.nextToken();
System.out.println("Server_Status_port is:" + ssp);
serverStatusPort = Integer.parseInt(ssp);
System.out.println("Integer serverStatusPort is:" +
serverStatusPort);
}else if(firstToken.equals("SERVER_CMD_PORT"))
serverCmdPort = Integer.parseInt(stTok.nextToken());
else{
System.out.println("Second Token is:" + stTok.nextToken());
}
}
}
}
public static String getMsgServerIp() {
return msgServerIp;
}
public static int getServerCmdPort() {
return serverCmdPort;
}
public static int getServerStatusPort() {
return serverStatusPort;
}
}

Roedy Green

unread,
Feb 8, 2002, 4:59:40 PM2/8/02
to
On 8 Feb 2002 13:45:26 -0800, zzh...@hotmail.com (George) wrote or
quoted :

>java.lang.NumberFormatException: 4498

Print out the string just prior to conversion to see what it is
seeing. Chances are there are some lead/trail spaces.

See "conversion" String -> int in the Java glossary.

The java glossary is at
http://www.mindprod.com/gloss.html
or http://209.139.205.39

--
eagerly seeking telecommuting programming work.
canadian mind products, roedy green

Steve Horsley

unread,
Feb 9, 2002, 8:12:18 AM2/9/02
to
"Roedy Green" <ro...@mindprod.com> wrote in message
news:dgi86ucr5u8ct1s6q...@4ax.com...

> On 8 Feb 2002 13:45:26 -0800, zzh...@hotmail.com (George) wrote or
> quoted :
>
> >java.lang.NumberFormatException: 4498
>
> Print out the string just prior to conversion to see what it is
> seeing. Chances are there are some lead/trail spaces.
>
> See "conversion" String -> int in the Java glossary.
>
>
>
> The java glossary is at
> http://www.mindprod.com/gloss.html
> or http://209.139.205.39
>
Well spotted, Roedy. Yes, there is a single space after 4498, even in the
posted article. Don't you love it when people copy/paste their code
instead of retyping it? It's so much easier when they post the truth.

One fix would be to create the tokenizer with " =" (space and equals) as the
token list.
Another would be to trim() the tokens.

Steve.


Roedy Green

unread,
Feb 11, 2002, 10:55:01 PM2/11/02
to
On Sat, 9 Feb 2002 13:12:18 -0000, "Steve Horsley"
<steve.h...@virgin.net> wrote or quoted :

>Well spotted, Roedy. Yes, there is a single space after 4498, even in the
>posted article.

I don't spot I errors, I just remember making them myself.

--

Roedy Green

unread,
Feb 11, 2002, 11:02:30 PM2/11/02
to
On Tue, 12 Feb 2002 03:55:01 GMT, Roedy Green <ro...@mindprod.com>
wrote or quoted :

>I don't spot I errors, I just remember making them myself.

I created the file http://mindprod.com/errormessages.html
for the most part by deliberately making various errors I knew I would
make eventually and then saw what the compiler would report.

The rest are for ones I made accidentally and had to think a bit to
track them down. Some are contributions from third parties.

0 new messages