NumberFormatException
Hi
I understand that this error msg "java.lang.NumberFormatException" means that we are trying to pass a String value into a int?
However, I need to pass a String value because the value is actually a password of a database that i need to connect to
Kindly advise if there is any way to work around this?
This is my exact error msg :
java.lang.NumberFormatException: For input string:"itrade4321"
Kindly assist/advise~! Thanks :)
[498 byte] By [
peachteaa] at [2007-11-27 5:05:54]

Why are you trying to parse the String into a number ?
check ur storing variable is in int... if yes change that datatype to String..ex:String itrade4321;not to int itrade4321;
> Kindly advise if there is any way to work around> this?I would kindly advise you to change the code (?) that expects a number as input, while passwords are not limited to numbers.
Maybe what you want to do is calculate an Integer value based on your String?However your String "itrade4321" is 10 bytes long, and can not be stored as a single Integer value without some data loss. You could store it in three integers or a byte array.
yup... i've declared the variable as String
btw this is the part of my code where it caught that exception
public static void main(String args[]) {
FillAlerter ma;
try {
ma = new FillAlerter("config.xml");
ma.poll();
} catch (Exception ex) {
System.out.println ("Exception error! error msg: "+ex.toString());
}
}
btw the config.xml file is actually where i store 'itrade4321' and other info like username etc
Thanks
> Maybe what you want to do is calculate an Integer
> value based on your String?
> However your String "itrade4321" is 10 bytes long,
> and can not be stored as a single Integer value
> without some data loss. You could store it in three
> integers or a byte array.
I think u might be right... There is some error msg that points me to byte... i try it out 1st.... will update here again
Your exception is being thrown somewhere within the FillAlerter class. Which line causes the problem:
ma = new FillAlerter("config.xml");
or
ma.poll();
If you post the corresponding method it might be easier to understand what you're trying to do.
hi
this is poll()
public void poll(){
startPoll = true;
Thread pollThread=null;
while(startPoll){
try{
Thread.sleep(poll);
eventlog.writeln("Start poll");
trigger();
}catch(Exception e){
eventlog.writeln("[Error] "+e.toString());
e.printStackTrace(eventlog.getWriter());
exlog.writeln("[Error] "+e.toString());
e.printStackTrace(exlog.getWriter());
}
}
destroy();
}
Message was edited by:
peachtea
> Your exception is being thrown somewhere within the
> FillAlerter class. Which line causes the problem:
> ma = new FillAlerter("config.xml");
> or
> ma.poll();
> If you post the corresponding method it might be
> easier to understand what you're trying to do.
btw, i try to do a try... catch at each of the statementd... apparently both statements also return the same exception msg
hth
Peachtea,You have to drill further down into your code to find the piece that tries to make the number conversion.
Hi GJosef
It seems that the fault lies in the FillAlerter()
i have added this small segment into FillAlerter()
String password = "itrade4321";
byte[] pass = password.getBytes("itrade4321");
byte[] defaultByte = password.getBytes();
and now it produces java.io.UnsupportedEncodingException: itrade4321
erm... is that the right way to convert the "itrade4321 into a byte array? o.o
Thanks again for your help :)
try {
String password = "itrade4321";
byte[] pass = password.getBytes("UTF-8");
byte[] defaultByte = password.getBytes();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Alter your code to above.It will work.
One more thing the out put of second and third lines will be same.
the mistake which you did is that instead of of giving charset as input to the second line you gave the password string again
the signature is of that method is
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException
Where charset values may be
US-ASCII,ISO-8859-1 ,UTF-8,UTF-16BE,UTF-16LE,UTF-16
Hi there
I put that source code at the place where it is before creating the connection to database (main java file)
I also tried putting in it another java file (that take cares of database connection with info like userid n password)
either way, i still got the same problem :(
> One more thing the out put of second and third lines
> will be same.
> the mistake which you did is that instead of of
> giving charset as input to the second line you gave
> the password string again
>
> the signature is of that method is
>
> public byte[] getBytes(String charsetName)
>throws UnsupportedEncodingException
Sorry can u explain more on the mistake i made? I googled form somewhere that charset is actually a jar file that needs to be copied n paste to classpath right? What do u mean by signaure of method? How do i use it?
Many thanks in advance for your reply!
Are you getting UnsupportedEncodingException now?
signature defines how that method has to be called like
the name of the method,input arguments,argument type etc.
Can you share the code by which you are trying to open the database connection.the way you are passing the password,username,sid,etc and what databse are you using
at first, yup i did get "unsupportedEncodingException"
now i got back "numberFormatException" again
This is my main java file
public FillAlerter(String configFile) throws Exception{
mac = new MsgAlertConfig(configFile);
eventlog= new Logger( mac.getTranLog() );
exlog= new Logger( mac.getExLog() );
userlog= new Logger( mac.getUserLog() );
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
eventlog.writeln("*******************************");
eventlog.writeln("*Fill Alerter Start*");
eventlog.writeln("*******************************");
try {
String password = "itrade4321";
byte[] pass = password.getBytes("UTF-8");
byte[] defaultByte = password.getBytes();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
cp = new ConnectionPooling(configFile);
udc = new UserDetailsConnection(configFile);
tndg = new TradeNotifyDocGenerator();
threadIndex = 0;
startPoll = false;
poll = Integer.parseInt( mac.getPoll() );
conn = cp.getConnection();
udConn = udc.getConnection();
outFileName = mac.getOutFileName();
outFilePath = mac.getOutFilePath();
xchgID = mac.getXchgID();
System.out.println("chg_id: " + xchgID + ", from mac: "+ mac.getXchgID());
csPartialFillSp = conn.prepareCall(SQL_RUN_SP);
csPartialFillSp.setString(1,xchgID);
eventlog.writeln(SQL_RUN_SP+", xchg_id=["+xchgID+"]");
psFillAlert = conn.prepareStatement(SQL_TRADE_ALERT);
eventlog.writeln(SQL_TRADE_ALERT);
psFillUpdate = conn.prepareStatement(SQL_UPDATE_TRADE_ALERTED);
eventlog.writeln(SQL_UPDATE_TRADE_ALERTED);
psFillUserDetails = udConn.prepareStatement(SQL_GET_USER_DETAILS);
eventlog.writeln(SQL_GET_USER_DETAILS);
This is my codes where the method only take cares of the database connection:
public ConnectionPooling(String configFile)throws Exception{
mac = new MsgAlertConfig(configFile);
eventlog = new Logger(mac.getTranLog());
exlog = new Logger(mac.getExLog());
threadIndex = 0;
host= mac.getHost();
port= mac.getPort();
sid= mac.getSid();
username = mac.getUsername();
password = mac.getPassword();
try {
String password = "itrade4321";
byte[] pass = password.getBytes("UTF-8");
byte[] defaultByte = password.getBytes();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
pber pbe = new pber();
password = pbe.decrypt(password);
sqlStat="";
createConnectionPool();
}
Thanks once again....
HiCan someone help me pls?thanks alot :)
Try the below code for ConnectionPooling
public ConnectionPooling(String configFile)throws Exception{
mac = new MsgAlertConfig(configFile);
eventlog = new Logger(mac.getTranLog());
exlog = new Logger(mac.getExLog());
threadIndex = 0;
host= mac.getHost();
port= mac.getPort();
sid= mac.getSid();
username = mac.getUsername();
password = mac.getPassword();
byte[] pass = null;
try {
String password = "itrade4321";
pass = password.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
pber pbe = new pber();
try{
password = pbe.decrypt(pass);
}catch(Exception e){
System.out.println("Error while decrypting");
e.printStackTrace();
}
sqlStat="";
try{
createConnectionPool();
}catch(Exception e){
System.out.println("Error while createConnectionPool");
e.printStackTrace();
}
}
If you getting any exception pls post the exception with printStackTrace();
ok thanks for your replyi had found out why i keep having that exception.... it is becos i was given the wrong xml fileoh well the problem is sorta solved here..... anyway thank you so much for all your replies, FJALOOR.....Message was edited by: peachtea