Cmd 窗口挂起且 JOption show Msg 不显示

发布于 2025-01-04 07:01:20 字数 1680 浏览 1 评论 0原文

我试图要求用户输入全名,我将从第一个名字和第二个名字中提取第一个字符。我还将询问用户他们的街道地址,我将从该地址中提取住房单元号码。

该计划是让用户输入类似以下内容: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-01-11 07:01:20

当第一个字符不是空格时,您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文