Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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编译时错误中的Readline_Java_Ioexception_Bufferedreader - Fatal编程技术网

java编译时错误中的Readline

java编译时错误中的Readline,java,ioexception,bufferedreader,Java,Ioexception,Bufferedreader,我知道我在这里做了一些非常错误的事情,但坦率地说,我对java的知识非常薄弱。每当我调用dataIn.readLine()时,我都会得到这个编译时错误 unreported exception java.io.IOException; must be caught or declared to be thrown 这是代码,我知道命名约定很糟糕,而且几乎什么都不做 import java.io.*; public class money { public static void ma

我知道我在这里做了一些非常错误的事情,但坦率地说,我对java的知识非常薄弱。每当我调用dataIn.readLine()时,我都会得到这个编译时错误

unreported exception java.io.IOException; must be caught or declared to be thrown
这是代码,我知道命名约定很糟糕,而且几乎什么都不做

import java.io.*; 
public class money {
    public static void main( String[]args ){
        String quarters; 
        String dimes; 
        String nickels; 
        String pennies; 
        int iquarters; 
        int idimes;
        int inickels; 
        int ipennies; 
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 

        System.out.println( "Enter the number of quarters. " ); 
        quarters = dataIn.readLine(); 
        System.out.println( "Enter the number of dimes" ); 
        dimes = dataIn.readLine(); 
        System.out.println( "Enter the number of nickels" ); 
        nickels = dataIn.readLine(); 
        System.out.println( "Enter the number of pennies" ); 
        pennies = dataIn.readLine(); 

        iquarters = Integer.parseInt( quarters ); 
        idimes = Integer.parseInt( dimes ); 
        inickels = Integer.parseInt( nickels ); 
        ipennies = Integer.parseInt( pennies ); 

    }
}
在这里编译,结果也是一样的

改变这一点:

public static void main( String[]args ){
致:

要理解为什么需要这样做,请阅读以下内容:

readLine()可以引发IOException。您需要将其包装在一个try-catch块中,该块在异常出现时捕获该异常,然后以一种适合您所做工作的方式对其进行处理。如果readLine()引发异常,控件将立即从try块流出并流入catch块


这两个建议都有效,但因为你的答案是最正确的,所以你得到了公认的答案。
public static void main( String[]args ) throws IOException {
try
{
    dataIn.readLine();
    // ... etc
}
catch(IOException e)
{
    // handle it. Display an error message to the user?
}