Java 检查输入的字符串是否为整数的步骤
在这个程序中,我想检查输入的数字是否是10位整数移动电话号码。如果我输入值为0123456789,它工作正常,但如果我输入值为5028608722,它将失败。程序是正确的,但这里缺少或错误 套装游戏Java 检查输入的字符串是否为整数的步骤,java,Java,在这个程序中,我想检查输入的数字是否是10位整数移动电话号码。如果我输入值为0123456789,它工作正常,但如果我输入值为5028608722,它将失败。程序是正确的,但这里缺少或错误 套装游戏 import java.util.Scanner; class Utility { static boolean numberOrNot(String input) { try { int i=Integer.parseIn
import java.util.Scanner;
class Utility
{
static boolean numberOrNot(String input)
{
try
{
int i=Integer.parseInt(input);
System.out.println(i);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
}
public class CheckMobileNumber
{
public static void main(String[] args)
{
System.out.println("Enter your mobile number");
Scanner sc = new Scanner(System.in);
String input = sc.next();
if(Utility.numberOrNot(input) && (input.length() == 10))
{
System.out.println("Good!!! You have entered valid mobile number");
}
else
{
System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
}
}
}
尝试使用
long
(long integer)数据类型而不是int
——我认为您的空间不足:2^32=4294967296
这里快速查看。尝试使用
long
(long integer)数据类型而不是int
——我认为您的空间不足:2^32=4294967296
下面快速查看。您的大值(5028608722)无法放入有符号整数。最大有符号整数为2^31-1==2147483647。您正在使用的号码是5028608722,太大了。为了使代码正常工作,您需要更改为使用长类型。例如:
import java.util.Scanner;
class Utility
{
static boolean numberOrNot(String input)
{
try
{
long i=Long.parseLong(input);
System.out.println(i);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
}
public class CheckMobileNumber
{
public static void main(String[] args)
{
System.out.println("Enter your mobile number");
Scanner sc = new Scanner(System.in);
String input = sc.next();
if(Utility.numberOrNot(input) && (input.length() == 10))
{
System.out.println("Good!!! You have entered valid mobile number");
}
else
{
System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
}
}
}
使用Long.parseLong()
的可选解决方案是使用正则表达式匹配您的数字。例如,类似这样的正则表达式将为10位数字返回true,否则返回false:
“0123456789”。匹配(“[0-9]{10}”)
将返回真值。任何非数值或非10位数的数值都将返回false
因此,您的代码可能如下所示:
import java.util.Scanner;
class Utility
{
static boolean numberOrNot(String input)
{
return input.matches("[0-9]{10}");
}
}
public class CheckMobileNumber
{
public static void main(String[] args)
{
System.out.println("Enter your mobile number");
Scanner sc = new Scanner(System.in);
String input = sc.next();
if(Utility.numberOrNot(input))
{
System.out.println("Good!!! You have entered valid mobile number");
}
else
{
System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
}
}
}
您的大值(5028608722)无法放入有符号整数。最大有符号整数为2^31-1==2147483647。您正在使用的号码是5028608722,太大了。为了使代码正常工作,您需要更改为使用长类型。例如:
import java.util.Scanner;
class Utility
{
static boolean numberOrNot(String input)
{
try
{
long i=Long.parseLong(input);
System.out.println(i);
}
catch(NumberFormatException ex)
{
return false;
}
return true;
}
}
public class CheckMobileNumber
{
public static void main(String[] args)
{
System.out.println("Enter your mobile number");
Scanner sc = new Scanner(System.in);
String input = sc.next();
if(Utility.numberOrNot(input) && (input.length() == 10))
{
System.out.println("Good!!! You have entered valid mobile number");
}
else
{
System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
}
}
}
使用Long.parseLong()
的可选解决方案是使用正则表达式匹配您的数字。例如,类似这样的正则表达式将为10位数字返回true,否则返回false:
“0123456789”。匹配(“[0-9]{10}”)
将返回真值。任何非数值或非10位数的数值都将返回false
因此,您的代码可能如下所示:
import java.util.Scanner;
class Utility
{
static boolean numberOrNot(String input)
{
return input.matches("[0-9]{10}");
}
}
public class CheckMobileNumber
{
public static void main(String[] args)
{
System.out.println("Enter your mobile number");
Scanner sc = new Scanner(System.in);
String input = sc.next();
if(Utility.numberOrNot(input))
{
System.out.println("Good!!! You have entered valid mobile number");
}
else
{
System.out.println("Sorry!!!! You have entered invalid mobile number. Try again...");
}
}
}
问题的直接原因是32位整数类型不足以容纳所有10位整数 这个任务最好使用正则表达式。从技术上讲,电话号码不是整数,在本例中它们看起来就像整数。最好不要将它们作为整数存储在软件中。有一天你可能需要处理电话号码中的非数字字符
Pattern phoneNumberPattern = Pattern.compile("\\d{10}");
if (phoneNumberPattern.matcher("5028608922").matches()) {
System.out.println("OK, phone number!");
} else {
System.out.println("Bad phone number!");
}
问题的直接原因是32位整数类型不足以容纳所有10位整数 这个任务最好使用正则表达式。从技术上讲,电话号码不是整数,在本例中它们看起来就像整数。最好不要将它们作为整数存储在软件中。有一天你可能需要处理电话号码中的非数字字符
Pattern phoneNumberPattern = Pattern.compile("\\d{10}");
if (phoneNumberPattern.matcher("5028608922").matches()) {
System.out.println("OK, phone number!");
} else {
System.out.println("Bad phone number!");
}
除了正确、简单的java答案之外:我建议使用lib phonenumber作为电话号码。github.com/googlei18n/libphonenumber,如果您的兴趣不纯粹是学术性的;-) 除了正确、简单的java答案之外:我建议使用lib phonenumber作为电话号码。github.com/googlei18n/libphonenumber,如果您的兴趣不纯粹是学术性的;-) 如果此处缺少或出错,程序如何正确?你需要解释你的问题,而不是仅仅说有什么不对。@如果他的程序说他输入了一个无效的数字,即使
5028608722
是有效的。是的,因为5028608722超出了int
的范围。使用long
。为@AndrewL干杯。我会为此收集一些代表。@MouseEvent我很好,只是想帮助某人-不需要IIP:)如果这里缺少或出现错误,程序如何正确?你需要解释你的问题,而不是仅仅说有什么不对。@如果他的程序说他输入了一个无效的数字,即使5028608722
是有效的。是的,因为5028608722超出了int
的范围。使用long
。为@AndrewL干杯。我会为此收集一些代表。@MouseEvent我很好,只是想帮助别人-不需要IIP:)