我现在想要从命令行窗口读一个字符串 两个double类型的数到程序里,用流怎么做呀???本人初学,希望有Java code 贴出

解决方案 »

  1.   

    String str; 
    double h; 
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 
    str=in.readLine(); 
    h=Double.parseDouble(str); 
      

  2.   

    如何从命令行读取用户输入:
    import java.io.*;public class ReadString {   public static void main (String[] args) {      //  prompt the user to enter their name
          System.out.print("Enter your name: ");      //  open up standard input
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));      String userName = null;      //  read the username from the command-line; need to use try/catch with the
          //  readLine() method
          try {
             userName = br.readLine();
          } catch (IOException ioe) {
             System.out.println("IO error trying to read your name!");
             System.exit(1);
          }      System.out.println("Thanks for the name, " + userName);   }}  // end of ReadString class
      

  3.   

    命令行参数不是要用到args数组参数的吗?要读一个字符串,两个double数到程序中,args.length是不是应该为三?一个属字符串,两个输double数,而且,代码中应该要有判断所输入的字符串是否为double的方法吧?
    想想感觉好乱,我是新手,希望高手指教~~
      

  4.   

    import java.io.*;public class IoStream class{
      public static void main(String[] args){
        BufferedReader br = BufferedReader(new FileReader(new InputStreamReader(System.in)));
        String sb = new String();
        String[] str;
        Double d1 = 0;
        Double d2 = 0;
        try{sb = br.readLine();
        str = sb.split(" ");
        //用空格作为分隔,存入str数组中;
         sb = str[0];
        d1 = d1.parseDouble(str[1]);
        d2 = d2.parseDouble(str[2]);
        System.out.println("您输入了这样一个字符串:"+ sb);
        System.out.println("您输入的第一个double是:"+d1);
        System.out.println("您输入的第二个double是:"+d2);
        }catch(IOException e){}
    }
    }以上程序没有检验,大体是这样的。
      

  5.   

    五楼正解import java.io.*; public class IoStreamClass{ 
      public static void main(String[] args){ 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        String sb = new String(); 
        String[] str; 
        Double d1 = new Double(0); 
        Double d2 = new Double(0);  
        try{sb = br.readLine(); 
        str = sb.split(" "); 
        //用空格作为分隔,存入str数组中; 
        sb = str[0]; 
        d1 = d1.parseDouble(str[1]); 
        d2 = d2.parseDouble(str[2]); 
        System.out.println("您输入了这样一个字符串:"+ sb); 
        System.out.println("您输入的第一个double是:"+d1); 
        System.out.println("您输入的第二个double是:"+d2); 
        }catch(IOException e){} 


    测过
      

  6.   

    DataInputStream dis = new DataInputStream(System.in);
    double d = dis.readDouble();
      

  7.   

    两种输入方法Scanner 需导入import java.util.Scanner;InputStreamReader 需导入import java.io.*;import java.io.*;
    import java.util.Scanner;public class test {/**
    * @param args
    */
    public static void main(String[] args) {
      
       Scanner scan = new Scanner(System.in);
       System.out.println("数值类型输入!");
       long i = scan.nextLong();
       System.out.println(i);
       Scanner scan1 = new Scanner(System.in);
       System.out.println("字符串输入!");
       String s = scan1.nextLine();
       System.out.println(s);
       String ss =""; 
       long ii =0;
       //数值型输入 输入的必须全部是数字
       try {
        InputStreamReader isr = new InputStreamReader(System.in); 
        BufferedReader br = new BufferedReader(isr); 
        System.out.println("数值类型输入!");
        s = br.readLine();
       } catch (Exception e) {
        e.printStackTrace();
       }
       ii = Long.parseLong(s);
       System.out.println(ii);
      
       try {
        InputStreamReader isr = new InputStreamReader(System.in); 
        BufferedReader br = new BufferedReader(isr); 
        System.out.println("字符串输入!");
        ss = br.readLine();   
       } catch (Exception e) {
        e.printStackTrace();
       }
       System.out.println(ss);
      
      
    }}
      

  8.   

    字节流:package IO;import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    /**
     * test.getBytes("iso8859-1"))
     * 用OutputStream和InputStream实现文件的写入和读取(字节的写入和读取)
     *
     */
    public class InAndOut {
    String path="e:\\abc.txt";
    public static void main(String[] args) {
    InAndOut io=new InAndOut ();
    io.write("我是邪恶少年!");
    io.read();
    }
    public void write(String content)
    {
    File f=new File (path);
    boolean add=true;//是否写入
    try {
    if (f.exists()==false) {
    f.createNewFile();
    FileOutputStream os=new FileOutputStream (f);//不用判断是否写入
    //FileOutputStream os=new FileOutputStream (f,add);//要判断是否写入
    os.write(content.getBytes());
    os.flush();
    os.close();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public void read()
    {
    try {
    InputStream is=new FileInputStream(path);
    //BufferedInputStream bis=new BufferedInputStream(new FileInputStream(path));
    int temp=0;
    String test="";
    try {
    while ((temp=is.read())!=-1) {
    test+=(char)temp;
    }
    System.out.println(new String (test.getBytes("iso8859-1")));//会出现乱码
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }字符流:package IO;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    /**
     * 
     *用read和write实现文件的写入和读取(字符的写入和读取)
     *
     */
    public class ReadAndWrite {
    //定义文件的路径,一边写入和读取使用
    String path="e:\\abc.txt";

    public static void main(String[] args) {
    ReadAndWrite r=new ReadAndWrite ();
    r.writeFile("我乃邪恶少年是也!");
    r.readFile();
    }
    private void readFile() {
    // TODO Auto-generated method stub
    try {
    Reader r=new BufferedReader (new FileReader(path));
    String test="";
    int temp=0;
    try {
    while ((temp=r.read())!=-1) {
    test+=(char)temp;
    }
    System.out.println(test);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    private void writeFile(String content) {
    // TODO Auto-generated method stub
    File f=new File (path);
    if (f.exists()==false) {
    try {
    f.createNewFile();
    FileWriter fw=new FileWriter (f);
    fw.write(content);
    fw.flush();
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }else {
    System.out.println(path+"已存在,写入失败,源文件是");
    }
    }
    }
      

  9.   

    用System.in读入,再用java字符串拆分
      

  10.   

    借地问个问题
    import java.io.*;
    import java.util.*;
    public class DoubleRead{
    public static void main(String[] args){

    String str;
    double a,b;
        try{
    BufferedReader s = new BufferedReader (new InputStreamReader(System.in));
    str=s.readLine();
    a=Double.parseDouble(str.split(" ")[0]);
    b=Double.parseDouble(str.split(" ")[1]);
    a+=b;
    System.out.println(a);
    }catch(IOException e){
      return;
    }


    }
    }
    输入1.2325 1.2345
    输出的却是这个 
    2.4669999999999996
    是怎么回事呢,我是新手,轻砸
      

  11.   

    我觉得使用ObjectInputStream 要省事点儿。。
      

  12.   


    因为你最后打了个回车对不对?
    所以这就是System.in不好用的地方,它会将你键入的东西都记录!