我现在想要从命令行窗口读一个字符串 两个double类型的数到程序里,用流怎么做呀???本人初学,希望有Java code 贴出
解决方案 »
- 菜鸟求助点击按钮显示文字
- java搭建SSL服务器的问题
- 怎么用java mail disposition-notification;邮件?
- 如何判断label是否隐藏??
- 怎么理解“实例”(intance),不要和object混淆。
- 问什么csdn上的小软件下载不了?
- java中调用top命令的问题 迷惑中..................
- ***菜鸟的疑惑***(郁闷中…………)
- 如何升级win2000自带的java包?(高手请进)
- 求SQLServer的JDBC驱动程序
- JOptionPane.showMessageDialog和 showInputDialog的参数
- 笔试题:有多少种情况会引发IllegalMonitorStateException抛出
double h;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
str=in.readLine();
h=Double.parseDouble(str);
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
想想感觉好乱,我是新手,希望高手指教~~
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){}
}
}以上程序没有检验,大体是这样的。
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){}
}
}
测过
double d = dis.readDouble();
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);
}}
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+"已存在,写入失败,源文件是");
}
}
}
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
是怎么回事呢,我是新手,轻砸
因为你最后打了个回车对不对?
所以这就是System.in不好用的地方,它会将你键入的东西都记录!