• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NumberFormatException: For input string: "ABC"  RSS feed

 
Karan Deep Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I wrote a program to encode an incoming string for encoding the alphabets by shifting there values by 10 places .
But i am getting the runtime exception" Exception in thread "main" java.lang.NumberFormatException: For input string: "ABC""
Most probably the error is due to the parseInt is there anything else that i should use ?Can some one suggest me something ?




public class Rot10
{
static String outStr;

public static String encode(String incomingStr)
{


for(int i=0;i<incomingStr.length();i++)
{
char[] incomingCharArr=incomingStr.toCharArray();
int n=Integer.parseInt(new String(incomingCharArr));
if(n>=65 && n<=90)//&&((n>=97 && n<=122)))
{
n=n+10;
}

if(n>90)
n=n-(n-26);

char []outCharArr=Character.toChars(n);
outStr = new String(outCharArr);
System.out.println(outStr);

}

return outStr;
}

public static void main(String a[])
{

String xx="ABC";
System.out.println(encode(xx));

}


}

 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1: Why are you using outStr as a static field?
2: Why are you using a for loop when you are doing exactly the same thing.
3: Why are you transforming a string to a char array just to build a string out of it one line later?
4: Have a look at what Integer.parseInt(String s) is exactly doing. You don't need that for a program with that description.
5: I think you need to have a look on how to access array elements.
 
Karan Deep Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Manuel Petermann Thanks man i found my error...... i shouldn't have converted the charArray to int in first place its working fine now ....
Thanks Cheers !!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!