异常线程“main” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

问题描述:

我想创造出摄氏度转换一个小控制台程序 - 飞轮海,反之亦然,这是我试过的代码:异常线程“main” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

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之后,它给了我这个错误:

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)

这是输出:

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) 
+1

什么是line33? – kosa

+1

我认为如果不是那么......混乱,回答你的问题会容易得多。你有这么多的 - while循环,老实说你可以在一个循环中完成。如果你能向我们展示第33行,那会更好。 – Austin

+0

这是行33: ''''回复= Character.toUpperCase(s.nextLine()。charAt(0));' –

nextDouble()离开输入流中的换行,所以当你再调用

nextLine().charAt(0) 

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

+0

我试过,但我有这个错误: '线程“main”java.lang中的异常。的StringIndexOutOfBoundsException:字符串索引超出范围:0 \t在java.lang.String.charAt(未知来源) \t在CelsiusFahrenheit.main(CelsiusFahrenheit.java:31)' 线31:'s.nextLine()的charAt (0);' –

+0

你尝试了什么?当然,即使你删除换行符,如果扫描器下一行是空的,你也会得到相同的异常。 –

+0

我这样做了:'double temp = s.nextDouble(); \t \t \t \t s.nextLine()。charAt(0);' –

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

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

我这样做了,但是我有同样的错误 –

+0

@SuSha在同一行?它现在应该来自不同的路线。 –

+0

我的问题已被解决问题是nextLine()我应该使用下一个() –

这里有几个问题..

nextLine().charAt(0)得到空字符串,因为新的生产线不被nextDouble()消耗

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

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

另外,转换计算中的数学运算不正确。使用5.0/9.0而不是5/9

最后,您正在阅读的choixConvert字符作为char,但随后将它作为一个int到你的方法,所以else块总会执行。

+0

Okey谢谢我解决了所有这些问题,但主要问题并未解决 –