I have written some basic code below which allows me to get user input for games they have played. The user will input as follows "GameName:Score:Time". Once the user has inputted this I then convert the time and score to integers as they are inputted to a string. From this I need to ensure that the user has inputted a valid integer and I'm not to sure on how to do this.
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());
}
}
}}