2

I have written this piece of code below and I was wondering if it is possible to validate the code entry for "playername". I need to be able to check to see if anything has been entered into this string by the user and I was wondering if it is possible to do this in any way apart from using a break, as it is not in a loop I cannot use a break. Also when I am getting input from the user I am using a colon as my field delimiter to split the user input up into separate elements of an array. I was wondering if there is any way in checking to see if the correct field delimiter is being used by the user.

import java.util.Scanner;

public class REQ2 {

    public static void main (String[] args) {
        String playername;       
        String line;
        String[] list = new String[100];
        int count = 0;  
        int score;
        int time;
        int totalScore =0;

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter your name");

        playername = sc.nextLine();

        System.out.println("Please enter your game achievements (Game name:score:time played) E.g. Minecraft:14:2332");

        while (count < 100) { 
            line = sc.nextLine();

            if(line.equals("quit")) {
                 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 (line.length() != 3) {
                    System.out.println("Error please try again, Please enter in the following format: game name:score:timeplayed");
                    break;
                }
                score = Integer.parseInt(elements[1].trim());            
                time=Integer.parseInt(elements[2].trim());
            }
        }
    }
}

2 Answers 2

3

For the playername issue i would suggest the following dummy proof:

System.out.println("Please enter your name:");
int ErrCounter=0;
while(true)
{
    playername = sc.nextLine();
    if(playername!="")
        breake;
    System.out.println("No name was entered.\n Please enter your name:");
    ErrCounter++;
    if(ErrCounter>=2/*or any other count you choose*/)
        System.err.println("No name wase entered. Exiting...");
}

As for the delimiter problem you can use the same system for proofing. In order to verify the proper delimiter usage use:

if(!(line.contains(':')))
    System.out.println("Please enter achivements with the proper \":\" sepration\n"); 
Sign up to request clarification or add additional context in comments.

3 Comments

Firstly would it be possible for me to use ErrCounter to later display all the incorrect entries made by the user or would it be best using a try catch exepction, and also my program does not recognise the contains I get this error "The method contains(CharSequence) in the type String is not applicable for the arguments (char)"
@A.hussain to use contains you need import java.lang.*;
I'm not sure try and catch will do the work for you. The way nextLine() method works is, it is looking for a line separator (i.e. "\n") from the user while buffering the line up until that separator. it will than return the line in the buffer. if the buffer is empty it will return the string "", an empty string. In other words there will be no error thrown to catch.
1

After playername = sc.nextLine(); use this code.

 while(playername.equals(""))
 {
     playername = sc.nextLine();
 }

This will not let the program continue until the variable playername has a value string value which isn't blank

Edit

If you want to exit the program if nothing is entered use

 if(playername.equals(""))
 {
     System.exit(0);
 }

Edit 2

As for checking if a : has been used in the string try adding this code after line = sc.nextLine();. (Remember to use this you will have to call this line of code, import java.util.regex.*;, at the top of your Java file)

Pattern pattern = Pattern.compile(":");
Matcher  matcher = pattern.matcher(line);

int patternMatch = 0;
while (matcher.find())
{
    patternMatch++;
}

while(patternMatch!=2)
{
    patternMatch=0;

    line = sc.nextLine();

    if(line.equals("quit")){
        break;
    }

    Pattern pattern = Pattern.compile(":");
    Matcher  matcher = pattern.matcher(line);

    while (matcher.find())
    {
        patternMatch++;
    }
}

2 Comments

Thank you, what if I need the program to be terminated if nothing is entered?
@A.hussain I believe that covers everything you asked. If it does would you be able to click on the tick next to my answer to accept it thanks :) If it doesn't let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.