【问题标题】:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 [closed]线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:0 [关闭]
【发布时间】:2012-10-22 23:13:06
【问题描述】:

我想创建一个小型控制台程序,它可以从摄氏 - 华氏转换,反之亦然,这是我尝试过的代码:

import java.util.Scanner;

public class CelsiusFahrenheit {

    private static Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in);
        char reponse = 'N', choixConvert;

        do
        {
            do
            {
                System.out.println("Choisie le mode de converstion : ");
                System.out.println("1 - Converstisseur Celesuis - Fahrenheit");
                System.out.println("2 - Converstisseur Fahrenheit - Celesuis");

                do
                {
                    System.out.print("Votre Choix: ");
                    choixConvert = s.next().charAt(0);
                    if(choixConvert != '1' && choixConvert != '2')
                          System.out.println("Mode inconnu, veuillez réitérer votre choix.");
                }
                while(choixConvert!='1' && choixConvert!='2');

                System.out.print("Température à convertir : ");
                double temp = s.nextDouble();
                System.out.print("Resultat est: " + ConvertCelesuisFahrenheit(choixConvert,temp));
                System.out.println("\nSouhaitez-vous convertir une autre température ? (O/N)");
                reponse = Character.toUpperCase(s.nextLine().charAt(0));

            }while(reponse != 'O' && reponse != 'N');
        }while(reponse == 'O');

        System.out.println("Au revoir !");
    }

    private static Double ConvertCelesuisFahrenheit(int choixConvert,double temp )
    {
        if(choixConvert == 1)
            return (9/5)*temp+32;
        else
            return (temp-32)*(5/9);
    }
}

但我遇到了一个问题,即函数 ConvertCelesuisFahrenheit() 总是返回 0.0,然后它给了我这个错误:

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

这是输出:

Choisie le mode de converstion : 
1 - Converstisseur Celesuis - Fahrenheit
2 - Converstisseur Fahrenheit - Celesuis
Votre Choix: 2
Température à convertir : 33
Resultat est: 0.0
Souhaitez-vous convertir une autre température ? (O/N)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at CelsiusFahrenheit.main(CelsiusFahrenheit.java:33)

【问题讨论】:

  • 我认为如果不是那么……混乱的话,回答你的问题会容易得多。你有很多 do - while 循环,老实说,你可能可以在一个循环中完成它。如果你能告诉我们第 33 行,那就更好了。
  • 这是第 33 行:reponse = Character.toUpperCase(s.nextLine().charAt(0));

标签: java


【解决方案1】:

nextDouble() 在输入流中留下换行符,所以当你调用时

nextLine().charAt(0)

nextLine() 的结果是一个空字符串。从流中删除换行符,或使用next(),就像您在上面所做的那样。

【讨论】:

  • 我试过了,但出现了这个错误:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at CelsiusFahrenheit.main(CelsiusFahrenheit.java:31) 第 31 行:s.nextLine().charAt(0);
  • 你尝试了什么?当然,即使你删除了换行符,如果扫描仪得到的下一行是空的,你也会得到同样的异常。
  • 我这样做了:double temp = s.nextDouble(); s.nextLine().charAt(0);
  • 这根本不会从流中删除换行符。我强烈建议您在此处使用next() 而不是nextLine(),以确保不会出现空行。
  • 好吧,我的问题已经解决了,我用s.next();替换了double s.nextLine();
【解决方案2】:

这里有几个问题..

nextLine().charAt(0) 获取空字符串,因为新行没有被 nextDouble() 消耗

你可以通过简单地添加一个来解决这个问题

String continueString = s.next();
response = Character.toUpperCase(continueString.charAt(0));

此外,您的转化计算中的数学是不正确的。使用5.0/9.0 而不是5/9

最后,您将 choixConvert 字符作为 char 读取,然后将其作为 int 传递到您的方法中,因此 else 块将始终执行。

【讨论】:

  • 好的,谢谢我已经纠正了所有这些问题,但主要问题没有解决
【解决方案3】:

更新这个:reponse = Character.toUpperCase(s.nextLine().charAt(0));

          String line = s.nextLine();
          while(line.length() <1){
             line = s.nextLine();
          }
          reponse = Character.toUpperCase(line.charAt(0));

【讨论】:

  • 我这样做了,但我遇到了同样的错误
  • @SuSha 在同一行?它现在应该来自不同的线路。
  • 我的问题已经解决了问题是 nextLine() 我应该使用 next()
  • @SuSha 很高兴知道。希望你也能得到差异。 next 读取下一个字符串,而nextList 读取整行。我很有希望,您也可以使用 nextLine 修复您的程序。
猜你喜欢
  • 2013-01-20
  • 1970-01-01
  • 2019-03-11
  • 2014-01-27
  • 2015-06-30
  • 2012-07-27
  • 2012-11-27
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多