循环不执行多次

时间:2013-04-12 14:26:28

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

我应该制作一个程序,不断接受桌面订单数据,并显示超过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“

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:6)

nextInt()不读取行的结尾 - 只是下一个整数。如果您想要阅读该行的剩余部分,则需要在nextLine()之后致电nextInt()

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

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

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

答案 1 :(得分:0)

你只是按回车吗?

ans = input.nextLine().charAt(0);
当它是空字符串时,

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

答案 2 :(得分:0)

请使用如下,

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

beacuse input.nextLine()返回空字符串,因此当尝试获取第0个字符时,它返回StringIndexOutOfBoundsException 在调用input.nextLine()

之前,需要获取一个String

答案 3 :(得分:0)

显然根据文件:

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返回给您。