gpt4 book ai didi

java - 如何在字符串的某个位置添加字符(Java)

转载 作者:行者123 更新时间:2023-12-02 06:17:13 25 4
gpt4 key购买 nike

我正在创建一个程序,它接受用户输入并生成一张去音乐会的门票。为此,用户在其中一个文本字段中输入他的全名。然后,程序获取文本字段的内容并将其设置为字符串 Concertgoer。为了获取用户的姓名缩写,我使用 String Tokenizer 将音乐会观众拆分为名字、中间名和姓氏字符串,然后使用子字符串从每个字符串中获取第一个字母。

我希望程序能够处理异常,如果用户在文本字段中仅输入两个由一个空格分隔的单词(例如名字和姓氏),则程序会生成一个随机字母,并将其连接到字符串 Concertgoer在名字和姓氏之间,空格所在的位置,以便 middleName 可以有一个初始值。我尝试通过处理字符串 Concertgoer 并扫描空格并尝试用字母替换空格来做到这一点。但是,它不起作用,并且程序崩溃了。程序这部分的代码如下所示,特别是 for 循环中的代码。控制台的 println 语句主要仅供引用,崩溃后显示的消息也在下面。

Random numCharacter2 = new Random();

String alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char addedLetter2 = alphabet2.charAt(numCharacter2.nextInt(alphabet2.length()));


for (int counter = 0; counter < concertgoer.length(); counter++) {

char scanner = ' ';

if (concertgoer.charAt(counter) == scanner) {

System.out.println(scanner + "\n Hello, World");

concertgoer = concertgoer.replace(concertgoer.charAt(scanner), addedLetter2);

System.out.println("New: " + concertgoer);
}

}


concertgoer = concertgoer + " " + addedLetter2;

//! Add letter in middle of string concertgoer if only two words are entered as substitute
//for middle name

StringTokenizer token = new StringTokenizer(concertgoer);
String firstName = token.nextToken();
String middleName = token.nextToken();
String lastName = token.nextToken();

String firstInitial = firstName.substring(0, 1);
String middleInitial = middleName.substring(0, 1);
String lastInitial = lastName.substring(0, 1);

firstInitial = firstInitial.toUpperCase();
middleInitial = middleInitial.toUpperCase();
lastInitial = lastInitial.toUpperCase();

System.out.println(firstInitial);
System.out.println(middleInitial);
System.out.println(lastInitial);

String nameInitials = firstInitial + middleInitial + lastInitial;
nameInitials = nameInitials.replace(" ", "");
System.out.println(nameInitials);

//double num = Math.random();
//Use random class and nextInt set range for any random number

Random num = new Random();
int randomInteger = Math.abs(num.nextInt() % 9999);
System.out.println(randomInteger);
ticketPasscode = nameInitials + randomInteger;
String message = "Your ticket passcode is: " + ticketPasscode;
messageBox(message);
System.out.println(ticketPasscode);

有人可以帮我解决这个问题,找到正确的方法吗?我感谢您的帮助。谢谢。

*崩溃

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at TicketPrinter.buttonClicked(TicketPrinter.java:138)
at BreezyGUI.GBFrameButtonListener.actionPerformed(GBFrame.java:241)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

在每次调用 nextToken() 之前使用 hasNextToken() 来检查是否确实存在下一个 token 。在此步骤中,您有 0, 1, 2, 3,... 字符串。

在这一步,如果您有 3 个字符串(名字、中间名、姓氏),并且程序继续使用第一个标记作为名字,第二个标记作为中间名,第三个标记作为名字,那么一切都很好。当然是姓氏。

另一方面,如果这一步只有两个字符串,则如您所说,为中间名分配一个随机字母,并将第一个标记分配给名字,第二个标记分配给姓氏。

从那里,您的程序继续

String firstInitial = ...

关于java - 如何在字符串的某个位置添加字符(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21343217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com