Java 检查数值是否为有效整数
我在下面写了一些基本代码,可以让我获得用户对他们玩过的游戏的输入。用户将输入如下“GameName:Score:Time”。一旦用户输入了这个,我就将时间和分数转换成整数,因为它们被输入到字符串中。从这一点,我需要确保用户输入了一个有效的整数,我不知道如何做到这一点Java 检查数值是否为有效整数,java,Java,我在下面写了一些基本代码,可以让我获得用户对他们玩过的游戏的输入。用户将输入如下“GameName:Score:Time”。一旦用户输入了这个,我就将时间和分数转换成整数,因为它们被输入到字符串中。从这一点,我需要确保用户输入了一个有效的整数,我不知道如何做到这一点 import java.util.Scanner; import java.io.IOException; import java.text.ParseException; public class REQ2 { p
import java.util.Scanner;
import java.io.IOException;
import java.text.ParseException;
public class REQ2
{
public static void main (String[] args) throws ParseException
{
String playername;
String line;
String[] list = new String[100];
int count = 0;
int score;
int time;
int InvalidEntries;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your name");
playername = sc.nextLine();
if(playername.equals(""))
{
System.out.println("Player name was not entered please try again");
System.exit(0);
}
System.out.println("Please enter your game achivements (Game name:score:time played) E.g. Minecraft:14:2332");
while (count < 100){
line = sc.nextLine();
if(line.equals("quit")){
break;
}
if(!(line.contains(":"))){
System.out.println("Please enter achivements with the proper \":\" sepration\n");
break;
}
list[count]=line;
System.out.println("list[count]" + list[count]);
count++;
for (int i=0; i<count; i++){
line=list[i];
String[] elements =line.split(":");
if (elements.length !=3){
System.out.println("Error please try again, Please enter in the following format:\nGame name:score:timeplayed");
break;
}
score = Integer.parseInt(elements[1].trim());
time=Integer.parseInt(elements[2].trim());
}
}
}}
import java.util.Scanner;
导入java.io.IOException;
导入java.text.ParseException;
公共类需求2
{
公共静态void main(字符串[]args)引发异常
{
弦乐演奏者姓名;
弦线;
字符串[]列表=新字符串[100];
整数计数=0;
智力得分;
整数时间;
残疾人士;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“请输入您的姓名”);
playername=sc.nextLine();
if(playername.equals(“”)
{
System.out.println(“未输入玩家名称,请重试”);
系统出口(0);
}
System.out.println(“请输入您的游戏成绩(游戏名称:分数:玩过的时间),例如Minecraft:14:2332”);
同时(计数<100){
line=sc.nextLine();
如果(行等于(“退出”)){
打破
}
如果(!(line.contains(“:”){
System.out.println(“请用正确的\”输入成绩):\“sepretion\n”);
打破
}
列表[计数]=行;
System.out.println(“列表[计数]”+列表[计数]);
计数++;
对于(int i=0;i来说,最强大、最灵活的方法可能是使用正则表达式:
final Pattern inputPattern = Pattern.compile("^(?<gameName>[^:]++):(?<score>\\d++):(?<time>\\d++)$")
final String line = sc.nextLine();
final Matcher matcher = inputPattern.matcher(line);
if(!matcher.matches()) {
throw new IllegalArgumentException("Invalid input") //or whatever
}
final String gameName = matcher.group("gameName");
final int score = Integer.parseInt(matcher.group("score"));
final int time = Integer.parseInt(matcher.group("time"));
最后,最简单的方法是捕获parseInt
抛出的NumberFormatException
,只需对当前代码进行最小的更改:
try {
score = Integer.parseInt(elements[1].trim());
} catch(NumberFormatException ex) {
//invalid input, emit error or exit
}
最强大、最灵活的方法可能是使用正则表达式:
final Pattern inputPattern = Pattern.compile("^(?<gameName>[^:]++):(?<score>\\d++):(?<time>\\d++)$")
final String line = sc.nextLine();
final Matcher matcher = inputPattern.matcher(line);
if(!matcher.matches()) {
throw new IllegalArgumentException("Invalid input") //or whatever
}
final String gameName = matcher.group("gameName");
final int score = Integer.parseInt(matcher.group("score"));
final int time = Integer.parseInt(matcher.group("time"));
最后,最简单的方法是捕获parseInt
抛出的NumberFormatException
,只需对当前代码进行最小的更改:
try {
score = Integer.parseInt(elements[1].trim());
} catch(NumberFormatException ex) {
//invalid input, emit error or exit
}
我这样做的方式是将解析放在try
时钟中,然后捕获NumberFormat
异常,如下所示
try{
score = Integer.parseInt(elements[1].trim());
}
catch(NumberFormatException e){
//Deal with it not being an integer here
}
你也可以用正则表达式来实现这一点,但这对我来说似乎是最简单的方法。我要做的就是将解析放在try
时钟中,然后捕获NumberFormat
异常,就像这样
try{
score = Integer.parseInt(elements[1].trim());
}
catch(NumberFormatException e){
//Deal with it not being an integer here
}
您也可以使用正则表达式来实现这一点,但对我来说,这是最简单的方法。可能的重复