Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

“线程中的异常”;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0,java,Java,我想创建一个小型控制台程序,它可以从摄氏温度转换为华氏温度,反之亦然,这是我尝试的代码: 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', cho

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

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,然后它会给出以下错误:

线程“main”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)
nextDouble()
在输入流中保留换行符,因此当您调用

nextLine().charAt(0)

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

更新此命令:
reponse=Character.toUpperCase(s.nextLine().charAt(0))带有

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

而(line.length()这里有几个问题

nextLine().charAt(0)
获取空字符串,因为nextDouble()未使用新行

只需添加一个

String continueString = s.next();
response = Character.toUpperCase(continueString.charAt(0));
此外,转换计算中的数学不正确。请使用
5.0/9.0
而不是
5/9


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

我认为如果不是如此……混乱,回答您的问题会容易得多。您有很多do-while循环,老实说,您可能会遇到请在一个循环中完成。如果您可以只显示第33行,那会更好。这是第33行:
reponse=Character.toUpperCase(s.nextLine().charAt(0));
我尝试过,但出现了以下错误:
线程“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 at java.lang.String.charAt(未知来源)在CelsiusFahrenheit.main(CelsiusFahrenheit.java:31)
第31行:
s.nextLine().charAt(0);
你尝试了什么?当然,即使你删除了换行符,如果扫描仪得到的下一行是空的,你也会得到同样的异常。我这样做了:
double temp=s.nextDouble();s.nextLine().charAt(0)
这根本不会从流中删除换行符。我强烈建议您使用
next()
而不是
nextLine()
以确保不会出现空行。好的,我的问题已经解决了,我将double
s.nextLine()替换为
s.next();
我做了这件事,但我也做了同样的事error@SuSha在同一行?它现在应该来自不同的行。我的问题已经解决了。问题是我应该使用next()的nextLine()@SuSha很高兴知道。希望您也了解这些差异。
next
读取下一个字符串,而
nextList
读取整行。我希望您也可以使用nextLine修复您的程序。好的,谢谢我已经纠正了所有这些问题,但主要问题没有解决