【问题标题】:Loop not executing more than once循环不执行多次
【发布时间】:2013-04-05 03:11:52
【问题描述】:

我应该编写一个程序来持续接受办公桌订单数据并显示超过 36 英寸长且至少有一个抽屉的橡木办公桌的所有相关信息。

import java.util.*;

public class MangMaxB 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        char ans;
        int orderNum=0, length=0, width=0, numDrawer=0, price=1000;
        String name;

        System.out.print("Do you wish to enter Oak " +
                        "desk order data? (y/n)");
        ans = input.nextLine().charAt(0);

        while (ans != 'n')
        {  
            System.out.print("Enter customer name: ");
            name=input.nextLine();

            System.out.print("Enter order number: ");
            orderNum=input.nextInt();    

            System.out.print("Enter length and width of Oak desk" +
                        " separated by a space: ");
            length = input.nextInt();
            width = input.nextInt();

            System.out.print("Enter number of drawer/s: ");
            numDrawer=input.nextInt();

            if ((length>36)&&(numDrawer>=1))
            {
                if ((length*width)>750)
                {
                    price+= 250;
                }

                price+= (numDrawer*100);
                price+= 300;

                System.out.println("\nOak desk order information:\n" 
                        + "Order number: " + orderNum + "\n"
                        + "Customer name: " + name + "\n"
                        + "Length: " + length + ", width: "
                        + width + ", surface: " + (length*width)
                        + "\n" + "Number of drawer/s: " + numDrawer
                        + "\nPrice of the desk is P " + price);
            }

            else
            {
                System.out.println("\nOak desk order isn't over 36 " +
                        "inches long and doesn't have a drawer");
            }

            System.out.print("Any more items? (y/n) ");
            ans = input.nextLine().charAt(0);
        }
    }
}

我能够输入数据并显示它,但由于它是一个循环,所以在第二次尝试时,它不起作用。

它说“线程“主”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 在 java.lang.String.charAt(未知来源) 在 controlStruc.MangMaxB.main"

我该如何解决这个问题?

【问题讨论】:

  • 为什么不只处理String 对象呢?从String 中取出char 没有任何意义。无论如何,在我看来它应该可以工作,您确定您在询问时输入了一个字符吗?如果您只是按Enter,则String 中的位置0 处不会有char
  • 好吧,在我看来 input.nextLine() 在循环结束时是一个空字符串
  • @SamIam 为什么它是一个空字符串,尽管这是真正的问题!
  • @Quetzalcoatl 我同意使用字符没有什么意义,但你错了,因为代码不能以其当前形式工作!

标签: java loops while-loop iteration java.util.scanner


【解决方案1】:

你只是按回车吗?

ans = input.nextLine().charAt(0);

当它是一个空字符串时抛出这个错误。你的例外清楚地告诉你这一点。您需要检查“nextLine”是否为空字符串。

【讨论】:

  • 也许检查应该是一种好的做法,但这不是问题 - 问题是 nextLine() 将始终(在用户输入正确输入的情况下)立即返回,因为 nextInt() 调用不会导致扫描器解析整数所在行的末尾。
  • 是的,我按下回车键。我该如何解决?在我输入客户名称后,它会产生错误
【解决方案2】:

nextInt() 不读取行尾 - 仅读取下一个整数。如果您想阅读该行的其余部分,则需要在 nextInt() 之后调用 nextLine()

目前你还没有这样做,所以最后一个nextLine()方法只是读取整数后面的剩余空行,并立即返回一个空字符串,导致异常。

在这种情况下,在这里调用nextLine() 应该可以解决问题:

System.out.print("Enter number of drawer/s: ");
numDrawer=input.nextInt();
input.nextLine(); //Added nextLine() call

【讨论】:

  • 我要添加 input.nextLine();在 input.nextInt() 之后的所有行中; ??
  • @DohaKik 在这个例子中,我相信只需要在我指定的行上添加它。
【解决方案3】:

请像下面这样使用,

System.out.print("Any more items? (y/n) ");  
input = new Scanner(System.in);  
ans = input.nextLine().charAt(0);

因为 input.nextLine() 返回空字符串,所以当尝试获取第 0 个字符时,它返回 StringIndexOutOfBoundsException 您需要在调用 input.nextLine() 之前获取一个字符串

【讨论】:

  • -1:新扫描仪的分配是不必要的开销,并避免了问题的真正根源。
【解决方案4】:

显然根据文档:

nextLine()-Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

nextInt()-Scans the next token of the input as an int.

从今以后,您的代码中的ans = input.nextLine() 将返回您,并以''char(0) 领先于StringIndexOutOfBoundsException

【讨论】:

  • 我认为您对 nextLine() 的第二次引用实际上是 nextInt()?
猜你喜欢
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
相关资源
最近更新 更多