473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java's NumberFormatException

Guys this a java program in which i've encountered Java's NUMBERFORMATEXCEPTION error can you please help me out with it.......thanks in advance :)
Question: Find Kaprekar number between 1-5000.
A kaprekar number example:
Example 1:
9 = 81(square of entered number) = 8+1(sum of digits of square)=9(original number)

Example 2:
45 = 2025(square of entered number) = 20+25(sum of digits of square)=45(original number)

Example 3:
297 = 88209(square of entered number) = 88+209(sum of digits of square)=297(original number)


[import java.io.*;
public class Kaprekar
{
BufferedReader inp=new BufferedReader(new InputStreamReader(System.in));

int number,sqr,sum,count;
int lim1,lim2;
int len1,len2,len3;
String str1;
String sum1,sum2;

void enter_limits()throws IOException
{
System.out.print("Enter the 1st limit:");
lim1=Integer.parseInt(inp.readLine());
System.out.print("Enter the 2nd limit:");
lim2=Integer.parseInt(inp.readLine());
System.out.println("Limit is from "+lim1+" -- "+lim2);
}

void calc()
{
for(int i=lim1;i<=lim2;i++)
{
count=0;
sqr=i*i;
str1=Integer.toString(sqr);
len1=str1.length();

if(i%2==0)
{
len2=len1/2;
sum1=str1.substring(0,len2);
sum2=str1.substring(len2,len1);
int s1=Integer.parseInt(sum1);//NumberFormatxception
int s2=Integer.parseInt(sum2);//NumberFormatxception
sum=s1+s2;
if(sum==i)
{
System.out.print(i+" is a Kaprekar number.");
count++;
}
}
else if(i%2!=0&&i>0)
{
len3=len1-1;
len2=len3/2;
sum1=str1.substring(0,len2);
sum2=str1.substring(len2,len1);
int s1=Integer.parseInt(sum1);//NumberFormatxception
int s2=Integer.parseInt(sum2);//NumberFormatxception
sum=s1+s2;
if(sum==i)
{
System.out.print(i+" is a Kaprekar number.");
count++;
}
}
}
}

public static void main()throws IOException
{
Kaprekar k=new Kaprekar();
k.enter_limits();
k.calc();
}
}]

Output:

java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.valueOf(Integer.java:553)
at Kaprekar.calc(Kaprekar.java:50)
at Kaprekar.main(Kaprekar.java:66)
Feb 19 '12 #1
2 2985
r035198x
13,262 8TB
When you split a single digit number you end up with one number and the empty string. For your purposes an empty string should be interpreted as zero so add

Expand|Select|Wrap|Line Numbers
  1. if(sum1.equals("")) {
  2.             sum1 = "0";
  3.         }
  4.         if(sum2.equals("")) {
  5.             sum2 = "0";
  6.         }
Also, your algorithm will probably miss some numbers because you are not searching through all possible substrings.
Feb 20 '12 #2
r035198x
13,262 8TB
Actually you may find it easier to treat the one digit cases as special cases so they don't mess up with your algorithm structure that much:
Expand|Select|Wrap|Line Numbers
  1. public class Kaprekar {
  2.     public static void kaprekar() {
  3.     int lim1 = 1;
  4.     int lim2 = 1000;
  5.     for (int i = lim1; i <= lim2; i++) {
  6.         if (i == 1) {
  7.         System.out.println(i + " is a Kaprekar number.");
  8.         }
  9.         int ilength = Integer.toString(i).length();
  10.         if (ilength == 1) {
  11.         continue;
  12.         }
  13.         int sqr = i * i;
  14.         String str1 = Integer.toString(sqr);
  15.         int len1 = str1.length();
  16.         String sum1 = str1.substring(0, len1 - ilength);
  17.         String sum2 = str1.substring(len1 - ilength, len1);
  18.         int s1 = Integer.parseInt(sum1);
  19.         int s2 = Integer.parseInt(sum2);
  20.         int sum = s1 + s2;
  21.         if (sum == i) {
  22.         System.out.println(i + " is a Kaprekar number.");
  23.         }
  24.     }
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.     kaprekar();
  29.     }
  30. }
  31.  
  32.  
  33.  
Feb 20 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: scjp | last post by:
Hey all, I recently wrote a few Java web-services. The first requires no parameters and works fine when I call it with SOAP::Lite. The second requires two Java String parameters. When I call...
0
by: Joe | last post by:
Hi, I am trying to connect to a MySQL database through a Java Servlet. I think I have correctly installed Tomcat 4.1.30 and the MySQL Database along with ConnectorJ. I am getting a...
0
by: NoaGross | last post by:
Hi, I'm relly new in java and I have a problem. I'm using java applet. When using http all ok, but when trying to use https i get: Java Plug-in 1.5.0_10 Using JRE version 1.5.0_10 Java...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
3
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
1
by: Jromero | last post by:
Hi, Here is the code - The program does not provide any message to the user when no argument is pass public class Hexnumbers{ public static int conv1(String s) throws...
0
by: adbasque | last post by:
Hello, I am running a chat server with a java Applet unfortunately I don't have access to the source code, all I have are jars and classes. When I run the applet with FF2, IE, Safari and Opera...
7
by: om anand giri | last post by:
Hello to all, I have a problem about NumberFormatException and i m very sad because i m not able to solve this. So please help. The code is---------- <code> <%@page...
1
by: AnandhaMurugan | last post by:
I got a numberformatException when i convert from string to decimal how to resolve it java.lang.NumberFormatException at java.math.BigDecimal.<init>(BigDecimal.java:455) at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.