加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱故事小小网_铜陵站长网 (http://www.0562zz.com/)- 视频终端、云渲染、应用安全、数据安全、安全管理!
当前位置: 首页 > 教程 > 正文

Java从控制台读入数据的几种技巧

发布时间:2021-12-18 14:50:56 所属栏目:教程 来源:互联网
导读:根据东莞站长网 Www.0769Zz.Com报道 这里记录Java中从控制台读入信息的几种方式,已备后查! (1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容这种方法) public class TestConsole1 { public static void main(String[] args) { String str = readDataFromConsole(Pleas
根据东莞站长网 Www.0769Zz.Com报道


这里记录Java中从控制台读入信息的几种方式,已备后查!
 
(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容这种方法)
 
 
public class TestConsole1 {  
    public static void main(String[] args) {  
        String str = readDataFromConsole("Please input string:);  
        System.out.println("The information from console: + str);  
    }  
  
    /**
     * Use InputStreamReader and System.in to read data from console
     *  
     * @param prompt
     *             
     * @return input string
     */  
    private static String readDataFromConsole(String prompt) {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        String str = null;  
        try {  
            System.out.print(prompt);  
            str = br.readLine();  
  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return str;  
    }  
}  
 
 
 
(2)JDK 1.5(利用Scanner进行读取)
 
 
public class TestConsole2 {  
    public static void main(String[] args) {  
        String str = readDataFromConsole("Please input string:");  
        System.out.println("The information from console:" + str);  
    }  
  
    /**
     * Use  java.util.Scanner to read data from console
     *  
     * @param prompt
     *  
     * @return input string
     */  
    private static String readDataFromConsole(String prompt) {  
        Scanner scanner = new Scanner(System.in);  
        System.out.print(prompt);  
        return scanner.nextLine();  
    }  
}  
 
 
 
Scanner还可以很方便的扫描文件,读取里面的信息并转换成你要的类型,比如对“2 2.2 3.3 3.33 4.5 done”这样的数据求和,见如下代码:
 
 
 
public class TestConsole4 {  
  
    public static void main(String[] args) throws IOException {  
        FileWriter fw = new FileWriter("num.txt");  
        fw.write("2 2.2 3.3 3.33 4.5 done");  
        fw.close();  
  
        System.out.println("Sum is "+scanFileForSum("num.txt"));  
    }  
  
    public static double scanFileForSum(String fileName) throws IOException {  
        double sum = 0.0;  
        FileReader fr = null;  
        try {  
            fr = new FileReader(fileName);  
            Scanner scanner = new Scanner(fr);  
              
            while (scanner.hasNext()) {  
                if (scanner.hasNextDouble()) {  
                    sum = sum + scanner.nextDouble();  
  
                } else {  
                    String str = scanner.next();  
  
                    if (str.equals("done")) {  
                        break;  
                    } else {  
                        throw new RuntimeException("File Format is wrong!");  
                    }  
  
                }  
            }  
  
        } catch (FileNotFoundException e) {  
            throw new RuntimeException("File " + fileName + " not found!");  
        } finally {  
            if (fr != null)  
                fr.close();  
        }  
        return sum;  
    }  
}  

(编辑:我爱故事小小网_铜陵站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读