我想输入一个字符串,然后将其输出,但下面象输入字符或整数的方法却不行了,请问这是怎么回事?我如果想输入一个字符串,该如何写才最简单?谢谢!import java.io.*;class test_inputString
{
 public static void main(String args[]) throws IOException
 {
  String str;
  
  str=System.in.read();  //这句在编译时出错:不兼容的类型。我应该怎样写啊?谢谢大家了!  System.out.println("str="+str);    
  
 }
}

解决方案 »

  1.   

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    str=br.readLine();
      

  2.   

    System.in.read()只能读取一个字节但返回的是整数,不能赋给String类型的呢,
      

  3.   

    只能用piaopiao11() 提供的方法吗?
      

  4.   

    System.in.read();  只能读单个字符
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      

  5.   

    读取字符串不同于输出
    从Console读取要用BufferedReader
    如果是GUI的 可以用JOptionPane的方法
      

  6.   

    再类型转换一下就好了啊!
    import java.io.*;class test_inputString
    {
     public static void main(String args[]) throws IOException
     {
      int str;
      str=System.in.read();  
      System.out.println("str="+(char)str);  
      
     }
    }
      

  7.   

    楼主看你的样子好像是玩c++过来的,
    java i/o跟c++的cin,cout比起来当然会觉得烦琐一点,
    一般输入整数都是这样的
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    str=br.readLine();
      

  8.   

    Nuage(无痕) :你的程序键盘输入字符串时,运行后每次只能输入一个字符!
      

  9.   

    import java.io.*;class test_inputString
    {
     public static void main(String args[]) throws IOException
     {
      String str;
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      str=br.readLine();
      
     // str=System.in.read();  //这句在编译时出错:不兼容的类型。我应该怎样写啊?谢谢大家了!  System.out.println("str="+str);    
      
     }
    }
      

  10.   

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    str=br.readLine();
    就行了
      

  11.   

    public static void main(String[] args) {
        String str = args[0];
        System.out.println(str);
      }