日期:2014-05-20  浏览次数:20681 次

我想输入一个简单的字符串,但是编译时总说我有错,希望能够帮我看一下。谢谢
import java.io.*;

public class IoStream {

public static InputStreamReader in = new InputStreamReader(System.in);
public static BufferedReader br = new BufferedReader(in);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = br.readLine();
System.out.println(str);
}
}

------解决方案--------------------
“public static InputStreamReader in = new InputStreamReader(System.in);”
最后的;” 是中文的,是不是你在这敲错了,反正要是英文半角下写的。
“String str = br.readLine();”这句需要捕获异常。
改为就没问题了:
Java code
package com.com.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IoStream {
    public static InputStreamReader in = new InputStreamReader(System.in);
    public static BufferedReader br = new BufferedReader(in);

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String str = br.readLine();
        System.out.println(str);
    }
}

------解决方案--------------------
public static InputStreamReader in = new InputStreamReader(System.in); //这个分号不对

另外, br.readLine(); 可能会抛出异常,要做异常捕捉处理或者继续往上抛出异常,所以,可以修改为
Java code
try {
    String str = br.readLine(); 
    System.out.println(str);
} catch (Exception e) {
    e.printStackTrace();
}

------解决方案--------------------
Java code

public static InputStreamReader in = new InputStreamReader(System.in);
    public static BufferedReader br = new BufferedReader(in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(str);
    }