Cmd 窗口挂起且 JOption show Msg 不显示
我试图要求用户输入全名,我将从第一个名字和第二个名字中提取第一个字符。我还将询问用户他们的街道地址,我将从该地址中提取住房单元号码。
该计划是让用户输入类似以下内容:John Smith。地址:主街1234号
处理后输出为JS1234。
我通过前两个对话框正确编译并执行 框,但命令窗口将挂起,并且不会生成最终的 显示具有预期输出的消息对话框。我必须按 Ctrl + C 来中断执行。我想我需要 以某种方式涉及 StringBuilder 但不确定如何使用 JOption。
谢谢,
杰里米
import java.util.*;
import java.lang.*;
import javax.swing.*;
public class ConstructID
{
public static void main(String[] args)
{
String name;
String aInitial = "";
String bInitial = "";
String sAdd;
String unit = "";
int i;
int j;
name = JOptionPane.showInputDialog(null,
"Please enter your full name. ");
sAdd = JOptionPane.showInputDialog(null,
"Please enter your street address. ");
i = 0;
while(i < name.length())
{
if(name.charAt(i) == ' ')
{
aInitial = name.substring(0, i);
bInitial = name.substring(i + 1, name.length());
i = name.length();
++i;
}
}
j = 0;
while(j < sAdd.length())
{
if(sAdd.charAt(j) == ' ')
{
unit = sAdd.substring(0, j);
j = sAdd.length();
++j;
}
}
JOptionPane.showMessageDialog(null, "Your unique ID is " +
aInitial + bInitial + unit);
}
}
I am trying to ask the user for a full name input, which I'll extract the first character from the first name, and the second name. I will also ask the user for their street address, which I'll be extracting the housing unit number from the address.
The plan is for user to input something like: John Smith. Address: 1234 Main St.
And after processing, the output would be JS1234.
I compile and execute properly through the first two dialog
boxes, but the command window will hang, and not produce the final
showMessageDialog box with the intended output. I have to Ctrl + C
to break the execution. I think I need to
involve StringBuilder somehow but not sure how to with JOption.
Thanks,
Jeremy
import java.util.*;
import java.lang.*;
import javax.swing.*;
public class ConstructID
{
public static void main(String[] args)
{
String name;
String aInitial = "";
String bInitial = "";
String sAdd;
String unit = "";
int i;
int j;
name = JOptionPane.showInputDialog(null,
"Please enter your full name. ");
sAdd = JOptionPane.showInputDialog(null,
"Please enter your street address. ");
i = 0;
while(i < name.length())
{
if(name.charAt(i) == ' ')
{
aInitial = name.substring(0, i);
bInitial = name.substring(i + 1, name.length());
i = name.length();
++i;
}
}
j = 0;
while(j < sAdd.length())
{
if(sAdd.charAt(j) == ' ')
{
unit = sAdd.substring(0, j);
j = sAdd.length();
++j;
}
}
JOptionPane.showMessageDialog(null, "Your unique ID is " +
aInitial + bInitial + unit);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当第一个字符不是空格时,您的 while 循环会导致无限循环。除非获得空格字符,否则永远不会增加循环计数器,因此它将挂在任何无空格字符上。
对于这种解析,您也可以使用 String.split() 或使用正则表达式。
Your while loops are causing infinite loop when first character is not space. You never increment the loop counters unless you get a space character so it will hang on any no space character.
Also for this kind of parsing you could use String.split() or use regexp.