Reputation: 1
I am new to Java and writing a program to show names, address, phone number, and birthday where name & phone number are public
, birthday is private
, and other info is protected
.
Here is my core class:
public class newd {
public int Name;
public int Phone_no;
protected String Address;
protected int Age;
private int Birth_day;
void GetData() throws IOException
{
InputStreamReader IN = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(IN);
System.out.print("Enter Name : ");
String S1 = BR.readLine();
Name = Integer.parseInt(S1);
System.out.print("Enter Phone_no : ");
String S2 = BR.readLine();
Phone_no = Integer.parseInt(S2);
System.out.print("Enter Address : ");
String S3 = BR.readLine();
Age = Integer.parseInt(S3);
System.out.print("Enter Age : ");
String S4 = BR.readLine();
Age = Integer.parseInt(S4);
System.out.print("Enter Birth_day : ");
String S5 = BR.readLine();
Birth_day = Integer.parseInt(S5);
}
void Display()
{
System.out.println("Name : " + Name);
System.out.println("Phone No : " + Phone_no);
System.out.println("Address : " + Address);
System.out.println("Age : " + Age);
System.out.println("Birth Day : " + Birth_day);
}
}
Here is how I am using it:
public class newdirectory {
public static void main(String[] args) throws IOException {
newd D = new newd();
D.GetData();
D.Display();
}
}
When I compile the program, it shows this dialog.
Enter Name : kamrul
Exception in thread "main" java.lang.NumberFormatException: For input string: "kamrul"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at newd.GetData(newd.java:21)
at newdirectory.main(newdirectory.java:9)
Can anyone help me understand what I am doing wrong?
Upvotes: 0
Views: 718
Reputation: 483
For a Conclusion the problem is you are converting Characters in Numbers, Since this is not possible, your program is throwing a Exception called Number Format Exception.
Solution is Just remove the lines of code having Integer.parseInt(S1)
& Integer.parseInt(S3)
Since these both are trying to Convert Name & Address into Numbers thus raising an exception.
Upvotes: 0
Reputation: 1575
The problem with your code is you are trying to convert a non-numeric string to a Number, in this case you will receive NumberFormatException
. To make things clear, "avadakadavra" is a non-numeric string and "1234" is numeric string which can be converted to a Integer
or a Float
, whereas the former cannot be converted.
I have included an example to work with dates using calender and included quick reference and examples.
import java.util.*;
import java.io.*;
public class newd {
public String Name;
public int Phone_no;
protected String Address;
protected int Age;
private Calendar Birth_day;
void GetData() throws IOException
{
InputStreamReader IN = new InputStreamReader(System.in);
BufferedReader BR = new BufferedReader(IN);
System.out.print("Enter Name : ");
Name = BR.readLine();
System.out.print("Enter Phone_no : ");
Phone_no = Integer.parseInt(BR.readLine());
System.out.print("Enter Address : ");
Address = BR.readLine();
System.out.print("Enter Age : ");
String S4 = BR.readLine();
Age = Integer.parseInt(S4);
System.out.print("Enter Birth_day MM/DD/YYYY: ");
String[] date = ((String)BR.readLine()).split("/");
Birth_day = new GregorianCalendar(Integer.parseInt(date[2]),Integer.parseInt(date[1]),Integer.parseInt(date[0]));
}
void Display()
{
System.out.println("Name : " + Name);
System.out.println("Phone No : " + Phone_no);
System.out.println("Address : " + Address);
System.out.println("Age : " + Age);
System.out.println("Birth Day : " + Birth_day.get(Calendar.MONTH) + "/" + Birth_day.get(Calendar.DAY_OF_MONTH) + "/" + Birth_day.get(Calendar.YEAR));
}
}
Date in java previously were managed with Date class available in JAVA.UTIL.DATE
but with the introduction of Calender, available in the same package, date handling has improved.
for quick tutorial on Calendar and Example
Upvotes: 0
Reputation: 1019
System.out.print("Enter Address : ");
String S3 = BR.readLine();
Age = Integer.parseInt(S3);
This is the problem in your code. You are trying to get convert a string data to integer and hence the exception.
Also change your public instance varaibles to protected/private. varaibles should not be exposed as public unless they are constants .
Reference on Variable Declarations: https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
Upvotes: 0
Reputation: 3739
System.out.print("Enter Name : ");
String S1 = BR.readLine();
Name = Integer.parseInt(S1);
parseInt(...)
only works if the string passed in can be parsed as an integer, or else, a NumberFormatException
will be thrown.
Refer to this link here
In your case, I suppose it should be:
public String Name;
and you will just need to do this:
System.out.print("Enter Name : ");
Name = BR.readLine();
Same for address:
System.out.print("Enter Address : ");
Address = BR.readLine();
Upvotes: 3