2016-11-12 20 views
1

我不知道我在做什么错,分配是创建一个代码,将温度从C转换为F或从F转换为C,直到用户决定完成为止,我也是应该在出现无效字符时打印错误消息,并让用户在不再次询问数字部分的情况下更正错误消息。Java temp转换程序。正确的输出,但错误

该程序似乎运行良好,直到我输入'c''C''f'或'F'以外的值。在这一点上,我仍然得到所需的输出,但比我得到一个错误,这里是我的代码。

import java.util.Scanner; 

public class ProjectThree 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Please enter a temperature to be converted followed" 
      + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
      + "\nfinished converting temperatures enter done."); 
     String userInput, intString; 
     userInput = keyboard.nextLine(); 

     while (!(userInput.equalsIgnoreCase("done"))) 
     { 
      int length, temp, degreesC, degreesF; 
      length = userInput.length(); 
      intString = userInput.substring(0,length - 1); 
      temp = Integer.parseInt(intString); 
      char scale = userInput.charAt(length - 1); 

      while (!((scale == 'c') || (scale == 'C') || (scale =='f') || 
       (scale == 'F'))) 
      { 
       System.out.println("Error: Invalid temperature unit. Enter a C or c" 
        + " for Celsius or an F or f for Fahrenheit."); 
       String errorInput = keyboard.next(); 
       scale = errorInput.charAt(0); 
       userInput = intString + errorInput; 
      } 
      switch (scale) 
      { 
       case 'C': 
       case 'c': 
        degreesF = (9 * (temp/5) + 32); 
        System.out.println(userInput + " is equal to " + degreesF 
         + "F"); 
        break; 
       case 'F': 
       case 'f': 
        degreesC = (5 * (temp - 32))/9; 
        System.out.println(userInput + " is equal to " + degreesC 
         + "C"); 
        break; 
      } 
      System.out.println("\nPlease enter a temperature to be converted followed" 
       + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
       + "\nfinished converting temperatures enter done."); 
      userInput = keyboard.nextLine(); 
     } 
    } 
} 

,误差

Please enter a temperture to be converted followed 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 
by a C or c for Celsius or an F or f for Farenheit. If 
finished converting tempertures enter done. 
    at java.lang.String.substring(String.java:1955) 
    at ChapterFour.ProjectThree.main(ProjectThree.java:33) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 5 seconds) 
+0

您应该将代码放在try catch语句中 –

+0

哪个是生成此异常的输入? –

+0

使用调试器,你会发现为什么你会得到这个异常 – Jens

回答

0

一般情况下,有很多的事情,“可能”去错了错误的输入,因为你没有任何错误检查做。这里有一些我注意到的事情...

1)正如我认为别人提到的,你没有检查任何输入的长度,然后才行事。如果有人在没有其他事情的情况下进入,您可能会遇到异常情况。相同,如果他们输入无效输入。

2)switch语句缺少默认子句。你应该添加一些打印出“出错了,请再试一次”的东西,以便知道它发生了。

3)当你只询问字符时,我首先看到你使用keyboard.nextLine()和keyboard.next()。如果在字符后面输入回车,则换行符可能会被下一个nextLine()拾取,导致打印错误,如上面1中所述。虽然谷歌搜索未来的)行为(以确认这一点,我碰到下面的(其他人似乎也有类似的问题):

Issues with nextLine();

+0

我实际上已将默认条款从交换机中取出,因为它只能在适当的情况下激活。关于增加检查长度的好处!我没有考虑到这些输入错误 – OnlinePseudonym

0

为空字符串或长度one你没有处理任何事情的串所以它throwexception。你需要用户try catch块或做我喜欢的事情。我简单地忽略输入字符串的长度zeroone

Scanner keyboard = new Scanner(System.in); 

     System.out.println("Please enter a temperature to be converted followed" 
       + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
       + "\nfinished converting temperatures enter done."); 

     String userInput, intString; 
     userInput = keyboard.nextLine(); 

     while (!(userInput.equalsIgnoreCase("done"))) { 
      int length, temp, degreesC, degreesF; 
      length = userInput.length(); 
      System.out.println(length); 
      if (length > 1) { 
       intString = userInput.substring(0, length - 1); 
       temp = Integer.parseInt(intString); 
       // System.out.println("Temp = " + temp); 
       char scale = userInput.charAt(length - 1); 
       // System.out.println("scale" + scale); 

       while (!((scale == 'c') || (scale == 'C') || (scale == 'f') || (scale == 'F'))) { 
        System.out.println("Error: Invalid temperature unit. Enter a C or c" 
          + " for Celsius or an F or f for Fahrenheit."); 
        String errorInput = keyboard.next(); 
        scale = errorInput.charAt(0); 
        userInput = intString + errorInput; 
       } 
       switch (scale) { 
       case 'C': 
       case 'c': 
        degreesF = (9 * (temp/5) + 32); 
        System.out.println(userInput + " is equal to " + degreesF + "F"); 
        break; 
       case 'F': 
       case 'f': 
        degreesC = (5 * (temp - 32))/9; 
        System.out.println(userInput + " is equal to " + degreesC + "C"); 
        break; 
       } 
      } 

      System.out.println("\nPlease enter a temperature to be converted followed" 
        + "\nby a C or c for Celsius or an F or f for Fahrenheit. If " 
        + "\nfinished converting temperatures enter done."); 
      userInput = keyboard.nextLine(); 
     } 

注意String substring(int beginIndex, int endIndex):返回substring从给定的index(beginIndex)直至成为规定index(endIndex)开始。对于例如“塔尼亚”。 substring(2,5)将返回“ait”。它抛出IndexOutOfBoundsException如果beginIndex小于零或beginIndex > endIndexendIndex大于Stringlength

在你的情况,如果你只是erter cC lenght-1成为zero这是你的last index。还有你的first index也是zero。那就是为什么你得到了IndexOutOfBoundsException

希望你明白了。