Definere maximum antal bogstaver?
Hej,Jeg er ved at lave et program i java, hvortil jeg skal lave en High score. I denne high score, kunne jeg godt tænke mig, at når folk indtaster deres navn, de kun kan indtaste max. 20 bogstaver.
Dette vil jeg gøre både for, at high scoren ser overskuelig ud og for at folk ikke kan ødelægge den, ved at indtaste rigtig mange bogstaver. Er der nogen der kan hjælpe mig med det?
Min klasse ser sådan ud:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/*
* This parser reads user input and tries to interpret it as an "Adventure"
* command. Every time it is called it reads a line from the terminal and
* tries to interpret the line as a two word command. It returns the command
* as an object of class Command.
*
* The parser has a set of known command words. It checks user input against
* the known commands, and if the input is not one of the known commands, it
* returns a command object that is marked as an unknown command.
*
*/
public class Parser
{
private CommandWords commands; // holds all valid command words
public Parser()
{
commands = new CommandWords();
}
public Command getCommand(String inputFromClient)
{
String inputLine = inputFromClient; // will hold the full input line
String word1;
String word2;
String word3;
String word4;
StringTokenizer tokenizer = new StringTokenizer(inputLine, ";");
if(tokenizer.hasMoreTokens())
word1 = tokenizer.nextToken(); // get first word
else
word1 = null;
if(tokenizer.hasMoreTokens())
word2 = tokenizer.nextToken(); // get second word
else
word2 = null;
if(tokenizer.hasMoreTokens())
word3 = tokenizer.nextToken(); // get third word
else
word3 = null;
if(tokenizer.hasMoreTokens())
word4 = tokenizer.nextToken(); // get fourth word
else
word4 = null;
// note: we just ignore the rest of the input line.
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1))
return new Command(word1, word2, word3, word4);
else
return new Command(null, word2, word3, word4);
}
}
Men jeg er faktisk ikke helt sikker på, at det er i denne klasse det skal gøres, så jeg kan godt paste nogle af de andre ind, hvis det gør det nemmere.
Mvh. Pernille.
