Java中的异常和数组问题

时间:2019-03-03 23:43:28

标签: java

我尝试用Java创建一个程序,询问用户时间(hh:mm:ss格式)。我有两个问题:

1)运行程序时,一旦我从键盘输入数据,程序就会抛出java.lang.NullPointerException

我不确定该如何解决。我尝试为数组创建全局变量,但没有解决问题。

2)如果我将code.split(":")更改为code.split("."),则程序将无限地引发MissingColonException循环。

如何停止?我没有包含异常代码,因为我认为问题不存在。

import java.util.Scanner;

public class CatchExceptionClock {   
    static String [] time;

    public static void main (String[] args) {
        String code = "";
        Scanner scan = new Scanner (System.in); 
        System.out.print ("Please enter time. The input data should have the format hh:mm:ss");
        code = scan.nextLine();

        while (!code.equals ("STOP")) {
             try {     
                 getInput(code);
                 checkHour (time [0]);
                 checkMinutes (time [1]);
                 checkSeconds (time [2]);                     
             } catch (NumberFormatException exception) {
                 System.out.println ("Number is not numeric: " + ". You entered: " + code);
                 System.out.println ("");
             } catch (MissingColonException d) {
                 System.out.println (d.getMessage()+ ". You entered: "+ code); 
                 System.out.println ("0");
             } catch (HourException o) {
                 System.out.println (o.getMessage()+ ". You entered: "+ code); 
                 System.out.println ("");
             } catch (MinutesException e) {
                 System.out.println (e.getMessage()+ ". You entered: "+ code); 
                 System.out.println ("");
             } catch (SecondsException a) {
                 System.out.println (a.getMessage()+ ". You entered: "+ code); 
                 System.out.println ("");
             }

         }
         System.out.print ("Enter  code (STOP to quit): ");
         code = scan.nextLine();
    }




    public static String[] getInput(String code) {
        String [] time = code.split(":");              
        if (time.length ==3)
            return time;
        else 
            throw new MissingColonException("The input is missing a colon.The input data should have the format hh:mm:ss ");    
    }


    public static void checkHour (String time) {
        int hour = Integer.parseInt (time);
        if (hour <0 || hour >24)
            throw new HourException("The hour should be a value between 0 and 24 ");
    }

    public static void checkMinutes (String time) {
        int minutes = Integer.parseInt (time);
        if (minutes <0 || minutes >60)
            throw new MinutesException("Minutes should be a value between 0 and 60 "); 
    }

    public static void checkSeconds (String time) {
         int seconds = Integer.parseInt (time);
         if (seconds <0 || seconds >60)
             throw new SecondsException("Seconds should be a value between 0 and 60 ");
    }   

}

1 个答案:

答案 0 :(得分:1)

在您的getInput方法中,创建一个遮盖静态版本的本地数组time。您想要:

time = code.split(":");   

代替:

String[] time = code.split(":");   

您的方法也都没有修改code,因此,除非用户在第一次运行时输入了停止条件,否则循环将永远是无限的。您需要添加

code = scan.nextLine();

循环结束