Java 尝试读取用户输入时出现NumberFormatException
我是Java新手,正在编写一个程序来显示姓名、地址、电话号码和生日,其中姓名和电话号码是Java 尝试读取用户输入时出现NumberFormatException,java,Java,我是Java新手,正在编写一个程序来显示姓名、地址、电话号码和生日,其中姓名和电话号码是公共的,生日是私人的,其他信息是受保护的 这是我的核心课程: public class newd { public int Name; public int Phone_no; protected String Address; protected int Age; private int Birth_day; void GetData() throws I
公共的
,生日是私人的
,其他信息是受保护的
这是我的核心课程:
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);
}
}
以下是我如何使用它:
public class newdirectory {
public static void main(String[] args) throws IOException {
newd D = new newd();
D.GetData();
D.Display();
}
}
当我编译程序时,它显示这个对话框
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)
谁能帮我理解我做错了什么
System.out.print("Enter Name : ");
String S1 = BR.readLine();
Name = Integer.parseInt(S1);
parseInt(…)
仅当传入的字符串可以解析为整数时才有效,否则将抛出NumberFormatException
指
就你而言,我想应该是:
public String Name;
您只需要这样做:
System.out.print("Enter Name : ");
Name = BR.readLine();
地址相同:
System.out.print("Enter Address : ");
Address = BR.readLine();
parseInt(…)
仅当传入的字符串可以解析为整数时才有效,否则将抛出NumberFormatException
指
就你而言,我想应该是:
public String Name;
您只需要这样做:
System.out.print("Enter Name : ");
Name = BR.readLine();
地址相同:
System.out.print("Enter Address : ");
Address = BR.readLine();
parseInt(…)
仅当传入的字符串可以解析为整数时才有效,否则将抛出NumberFormatException
指
就你而言,我想应该是:
public String Name;
您只需要这样做:
System.out.print("Enter Name : ");
Name = BR.readLine();
地址相同:
System.out.print("Enter Address : ");
Address = BR.readLine();
parseInt(…)
仅当传入的字符串可以解析为整数时才有效,否则将抛出NumberFormatException
指
就你而言,我想应该是:
public String Name;
您只需要这样做:
System.out.print("Enter Name : ");
Name = BR.readLine();
地址相同:
System.out.print("Enter Address : ");
Address = BR.readLine();
这就是代码中的问题。您正在尝试将字符串数据转换为整数,因此出现异常
同时将公共实例变量更改为protected/private。变量不应公开,除非它们是常量
变量声明的参考:
这就是代码中的问题。您正在尝试将字符串数据转换为整数,因此出现异常
同时将公共实例变量更改为protected/private。变量不应公开,除非它们是常量
变量声明的参考:
这就是代码中的问题。您正在尝试将字符串数据转换为整数,因此出现异常
同时将公共实例变量更改为protected/private。变量不应公开,除非它们是常量
变量声明的参考:
这就是代码中的问题。您正在尝试将字符串数据转换为整数,因此出现异常
同时将公共实例变量更改为protected/private。变量不应公开,除非它们是常量
变量声明的参考:
代码的问题是您试图将非数字字符串转换为数字,在这种情况下,您将收到
NumberFormatException
。为了清楚起见,“avadakadavra”是一个非数字字符串,“1234”是一个数字字符串,可以转换为整数或浮点值,而前者不能转换
我已经包括了一个使用日历处理日期的示例,并包括了快速参考和示例
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));
}
}
java中的Date以前是通过java.UTIL.Date
中提供的Date类来管理的,但是随着Calender的引入(在同一个包中提供),日期处理得到了改进。
并且代码的问题是您试图将非数字字符串转换为数字,在这种情况下,您将收到NumberFormatException
。为了清楚起见,“avadakadavra”是一个非数字字符串,“1234”是一个数字字符串,可以转换为整数或浮点值,而前者不能转换
我已经包括了一个使用日历处理日期的示例,并包括了快速参考和示例
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));
}
}
java中的Date以前是通过java.UTIL.Date
中提供的Date类来管理的,但是随着Calender的引入(在同一个包中提供),日期处理得到了改进。
并且代码的问题是您试图将非数字字符串转换为数字,在这种情况下,您将收到NumberFormatException
。为了清楚起见,“avadakadavra”是一个非数字字符串,“1234”是一个数字字符串,可以转换为整数或浮点值,而前者不能转换
我已经包括了一个使用日历处理日期的示例,并包括了快速参考和示例
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));
}
}
java中的Date以前是通过java.UTIL.Date
中提供的Date类来管理的,但是随着Calender的引入(在同一个包中提供),日期处理得到了改进。
并且代码的问题是您试图将非数字字符串转换为数字,在这种情况下,您将收到NumberFormatException
。为了清楚起见,“avadakadavra”是一个非数字字符串,“1234”是一个数字字符串,可以转换为整数或浮点值,而前者不能转换
我已经包括了一个使用日历处理日期的示例,并包括了快速参考和示例
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));
}
}
java中的Date以前是通过java.UTIL.Date
中提供的Date类来管理的,但是随着Calender的引入(在同一个包中提供),日期处理得到了改进。
作为结论,问题是您正在将字符转换为数字,因为这是不可能的,您的程序正在抛出一个名为数字格式异常的异常。
解决方案是删除包含Integer.parseInt(S1)
和Integer.parseInt(S3)
的代码行,因为它们都试图转换Name和Addres