关于使用StringTokenizer为Player对象设置姓名的技术咨询
Fixing Your Player Registration Code with StringTokenizer
Let's break down the issues in your code first, then walk through how to fix them:
Key Problems in the Current Code
- NullPointerException waiting to happen: You're initializing
StringTokenizer lineTokens = new StringTokenizer(fullname);whenfullnameisnull. StringTokenizer requires a non-null input string, so this will crash immediately. - Chaotic input flow: You prompt for "Name:" but then read three separate inputs (
name,surname,fullname) in sequence—this doesn't match what a user would expect. Plus, you set the player's name toname + surname(no space between them) before even handling thefullnameinput, making the StringTokenizer loop completely useless. - Unfinished/incorrect token logic: The
while (lineTokens.hasMoreTokens())block references a tokenizer initialized with a null value, and the code cuts off mid-implementation, leaving invalid syntax.
Corrected Implementation Options
Option 1: Read Full Name and Split with StringTokenizer
If you want users to enter their full name in one line (e.g., "John Doe"), this approach is clean and uses StringTokenizer as intended:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PlayerRegistration { private static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); public static Player newPlayer(Integer playerNum) throws IOException { Player player = new Player(); System.out.println("Player " + (playerNum + 1) + " registration"); System.out.print("Enter full name (first last): "); String fullname = input.readLine(); // Initialize StringTokenizer ONLY after we have a valid, non-null fullname StringTokenizer lineTokens = new StringTokenizer(fullname); // Handle edge cases where user might enter only one name String firstName = lineTokens.hasMoreTokens() ? lineTokens.nextToken() : ""; String lastName = lineTokens.hasMoreTokens() ? lineTokens.nextToken() : ""; // Combine with a space for proper formatting player.setName(firstName + " " + lastName); return player; } // Dummy Player class for context (replace with your actual Player class) static class Player { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } }
Option 2: Read First and Last Name Separately
If you want to prompt for first and last name individually (no need for StringTokenizer here), this is a simpler approach:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PlayerRegistration { private static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); public static Player newPlayer(Integer playerNum) throws IOException { Player player = new Player(); System.out.println("Player " + (playerNum + 1) + " registration"); System.out.print("Enter first name: "); String firstName = input.readLine(); System.out.print("Enter last name: "); String lastName = input.readLine(); player.setName(firstName + " " + lastName); return player; } // Dummy Player class for context static class Player { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } }
Additional Best Practices
- Always initialize
StringTokenizerwith a valid, non-null string—never passnullto its constructor. - Handle edge cases (like users entering only one name) to avoid
NoSuchElementExceptionfrom callingnextToken()when no tokens are left. - Use
BufferedReadersafely: either declarethrows IOExceptionin your method or wrap input calls in a try-catch block if you need to handle errors gracefully (e.g., invalid input, unexpected stream closure).
内容的提问来源于stack exchange,提问作者Caroline Mourão
相关产品推荐
相关产品推荐

