1

机の注文データを継続的に受け取り、長さ 36 インチを超え、引き出しが 1 つ以上あるオーク材の机に関するすべての関連情報を表示するプログラムを作成することになっています。

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);
        }
    }
}

データを入力して表示することはできたのですが、2回目はループなのでうまくいきませんでした。

「スレッド「メイン」での例外 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at controlStruc.MangMaxB.main」

どうすればこれを修正できますか?

4

4 に答える 4

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() を呼び出す前に文字列を取得する必要があります

于 2013-04-12T14:59:41.840 に答える
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ます。

于 2013-04-12T15:16:01.757 に答える