You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于使用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); when fullname is null. 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 to name + surname (no space between them) before even handling the fullname input, 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 StringTokenizer with a valid, non-null string—never pass null to its constructor.
  • Handle edge cases (like users entering only one name) to avoid NoSuchElementException from calling nextToken() when no tokens are left.
  • Use BufferedReader safely: either declare throws IOException in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:43:22