试图在特定的行上输入字符

时间:2014-07-07 03:11:46

标签: java char do-while

我正在研究一个最终计算货币转移的计划,但我已经陷入困境。我得到了“线程中的异常”主要“

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:658)
        at Currency.main(Currency.java:31)". 

所以我知道它发生在第(31)行,我对这个问题有些了解。我想这是因为当我要求欧元,英镑和日元的汇率时,我已经得到了用户的意见。我的老师有点把它扔给我们而没有真正教我们字符输入。我的问题是如何让程序读取程序的第4个输入以进入循环。提前谢谢!

    import java.io.*;
    import java.util.Scanner;
    public class Currency
    {
     public static void main(String[]args)
     {
        Scanner input = new Scanner(System.in);

        int quitInput = 0;

        System.out.println("How many Euros a dollar will buy?");
        double euros = input.nextDouble();

        System.out.println("How many Pound Sterling a dollar will buy?");
        double pound = input.nextDouble();

        System.out.println("How many Yen a dollar will buy?");
        double yen = input.nextDouble();

        do {
                System.out.println("Enter 'E' to buy Euros, 'P' to buy Pound Sterling, 'Y' to buy yen, or 'Q' to quit");
                String decisionString = input.nextLine();
                char decision = decisionString.charAt(0);

                if (decision == 'E' || decision == 'e')
                {
                 System.out.println("Euros");
                }
                else if (decision == 'P' || decision == 'p')
                {
                 System.out.println("Pound Sterling");
                }
                else if (decision == 'Y' || decision == 'y')
                {
                 System.out.println("Yen");
                }
                else if (decision == 'Q' || decision == 'q')
                {
                 System.out.println("Are you sure you want to quit?");
                 String quit = input.nextLine();
                 char exit = quit.charAt(0);
                 if (exit == 'Y' || exit == 'y')
                 {
                  quitInput = -1;
                 }
                 else
                 {
                  quitInput = 1;
                 }
                }
                else
                {
                 System.out.println("Your input was invalid, please try again \n");
                }
         }      while (quitInput >= 0);

                System.out.println("End");
 }
}

3 个答案:

答案 0 :(得分:0)

<强>问题:

double yen = input.nextDouble();

它只会消耗double而不是new line,所以当你调用String decisionString = input.nextLine();时,它会消耗新行,从而给你错误。

<强>溶液

nextLine之后调用nextDouble以使用newLine

double yen = input.nextDouble();
input.nextLine();

答案 1 :(得分:0)

你得到一个空行,因为nextDouble()只读取double而不是后面的输入。我能想到的最简单的解决方案是在该行之后调用input.nextLine()

double yen = input.nextDouble();
input.nextLine();

答案 2 :(得分:0)

您需要跳过nextXXX()调用的回车。

nextDouble()不会读取下一行。因此,当您将决策输入(货币和退出字符)时,您将面临错误。这是你的程序的修复程序。

  

将nextLine()替换为next()。

import java.io. *;     import java.util.Scanner;     公共类货币     {      public static void main(String [] args)      {         扫描仪输入=新扫描仪(System.in);

    int quitInput = 0;

    System.out.println("How many Euros a dollar will buy?");
    double euros = input.nextDouble();

    System.out.println("How many Pound Sterling a dollar will buy?");
    double pound = input.nextDouble();

    System.out.println("How many Yen a dollar will buy?");
    double yen = input.nextDouble();

    do {
            System.out.println("Enter 'E' to buy Euros, 'P' to buy Pound Sterling, 'Y' to buy yen, or 'Q' to quit");
            String decisionString = input.next();
            char decision = decisionString.charAt(0);

            if (decision == 'E' || decision == 'e')
            {
             System.out.println("Euros");
            }
            else if (decision == 'P' || decision == 'p')
            {
             System.out.println("Pound Sterling");
            }
            else if (decision == 'Y' || decision == 'y')
            {
             System.out.println("Yen");
            }
            else if (decision == 'Q' || decision == 'q')
            {
             System.out.println("Are you sure you want to quit?");
             String quit = input.next();
             char exit = quit.charAt(0);
             if (exit == 'Y' || exit == 'y')
             {
              quitInput = -1;
             }
             else
             {
              quitInput = 1;
             }
            }
            else
            {
             System.out.println("Your input was invalid, please try again \n");
            }
     }      while (quitInput >= 0);

            System.out.println("End");
   } 
}