不要用System.in.read();获取输入
给你一个小例子,读取键盘输入的真实字符串
import java.io.*;
public class Test {
  public static void main(String[] args) {
    try {
      //输入5个字符串,用str[]数组存放
      String[] str = new String[5];
      for(int i=0 ; i<str.length ; i++) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        str[i] = br.readLine();
      }
      //输入结果
       System.out.println(str[0]+str[1]+str[2]+str[3]+str[4]);
    }
    catch(Exception e) {
      System.out.println(e);
    }
  }
}

解决方案 »

  1.   

    从标准输入中读取数据
    以Unix首先倡导的“标准输入”、“标准输出”以及“标准错误输出”概念为基础,Java提供了相应的System.in,System.out以及System.err。贯这一整本书,大家都会接触到如何用System.out进行标准输出,它已预封装成一个PrintStream对象。System.err同样是一个PrintStream,但System.in是一个原始的InputStream,未进行任何封装处理。这意味着尽管能直接使用System.out和System.err,但必须事先封装System.in,否则不能从中读取数据。
    典型情况下,我们希望用readLine()每次读取一行输入信息,所以需要将System.in封装到一个DataInputStream中。这是Java 1.0进行行输入时采取的“老”办法。在本章稍后,大家还会看到Java 1.1的解决方案。下面是个简单的例子,作用是回应我们键入的每一行内容://: Echo.java
    // How to read from standard input
    import java.io.*;public class Echo {
      public static void main(String[] args) {
        DataInputStream in =
          new DataInputStream(
            new BufferedInputStream(System.in));
        String s;
        try {
          while((s = in.readLine()).length() != 0)
            System.out.println(s);
          // An empty line terminates the program
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
    } ///:~之所以要使用try块,是由于readLine()可能“掷”出一个IOException。注意同其他大多数流一样,也应对System.in进行缓冲。
    由于在每个程序中都要将System.in封装到一个DataInputStream内,所以显得有点不方便。但采用这种设计方案,可以获得最大的灵活性。以上文字摘自《Thinking in Java》第10章java IO系统