{"id":508,"date":"2004-03-13T07:00:00","date_gmt":"2004-03-12T22:00:00","guid":{"rendered":"\/?p=508"},"modified":"2004-03-13T07:00:00","modified_gmt":"2004-03-12T22:00:00","slug":"java2-sdk-15-tiger-formatted-inputoutput","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/508.html","title":{"rendered":"Java2 SDK 1.5 (Tiger) [Formatted Input\/Output]"},"content":{"rendered":"<p>\nRef: <a href=\"http:\/\/java.sun.com\/developer\/technicalArticles\/releases\/j2se15\/\">J2SE 1.5 in a Nutshell<\/a>\n<\/p>\n<p>\nNutshell\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u6b21\u306e\u5185\u5bb9\u306b\u3064\u3044\u3066\u307f\u3066\u307f\u3088\u3046\u3002<br \/>\n\u30fbFormatted Input<br \/>\n\u30fbFormatted Output\n<\/p>\n<p>\n\u307e\u305f\u30011.5 \u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f\u65b0\u3057\u3044\u30af\u30e9\u30b9 java.awt.MouseInfo \u306b\u3064\u3044\u3066\u3082\u307f\u3066\u307f\u3088\u3046\u3002\n<\/p>\n<p><!--more--><\/p>\n<h2>Formatted Input<\/h2>\n<p>\njava.util.Scanner\u3092\u4f7f\u3046\u3068\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u5165\u529b\u3092\u3046\u3051\u3064\u3051\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u7c21\u5358\u306b\u3064\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u3053\u308c\u304c\u3042\u308b\u3068\u3001\u521d\u5b66\u8005\u304c java.io.BufferedReader\u306a\u3069\u3092\u4f7f\u308f\u306a\u304f\u3066\u3059\u3080\u306e\u3067\u3001\u6559\u3048\u308b\u306e\u304c\u697d\u306b\u306a\u308a\u305d\u3046\u3060\u3002java.util.Formatter\u30af\u30e9\u30b9\u3092\u7528\u3044\u305f\u30d1\u30bf\u30fc\u30f3\u30de\u30c3\u30c1\u306b\u3088\u308b\u5165\u529b\u53d7\u3051\u3064\u3051\u6587\u5b57\u5217\u306e\u5236\u5fa1\u3082\u3067\u304d\u308b\u306e\u3067\u3001\u8208\u5473\u304c\u3042\u308b\u4eba\u306f\u8abf\u3079\u3066\u307f\u308b\u3068\u826f\u3044\u3060\u308d\u3046\u3002\n<\/p>\n<div class=\"code\">import java.util.Scanner;<br \/>\npublic class FormattedInputTest {<br \/>\n    public static void main(String[] args) {<br \/>\n\tScanner s = Scanner.create(System.in);<br \/>\n\tSystem.out.print(&#8220;Input user name: &#8220;); String user = s.next();<br \/>\n\tSystem.out.print(&#8220;Input user age : &#8220;); int age = s.nextInt();<br \/>\n        s.close();<br \/>\n\tSystem.out.println(&#8220;user:&#8221;+user+&#8221;, age:&#8221;+age);<br \/>\n    }<br \/>\n}<\/div>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u6b21\u306e\u901a\u308a\u3002\n<\/p>\n<div class=\"code\">$ javac -source 1.5 FormattedInputTest.java<br \/>\n$ java FormattedInputTest<br \/>\nInput user name: hiro345<br \/>\nInput user age : 20<br \/>\nuser:hiro345, age:20<\/div>\n<h2>Formatted Output<\/h2>\n<p>\n\u3053\u308c\u307e\u3067\u3001Java\u3067\u306fC\u306e\u3088\u3046\u306a\u66f8\u5f0f\u4ed8\u304d\u51fa\u529b\u304c\u3067\u304d\u306a\u3044\u305f\u3081\u306b\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u3078\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u51fa\u529b\u3059\u308b\u306e\u306f\u7d50\u69cb\u9762\u5012\u3060\u3063\u305f\u30021.5\u304b\u3089\u306f\u3001System.out.printf\u30e1\u30bd\u30c3\u30c9\u3067\u66f8\u5f0f\u4ed8\u304d\u51fa\u529b\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u306e\u3067\u3001\u4eca\u307e\u3067\u3088\u308a\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u7c21\u5358\u306b\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u66f8\u5f0f\u306e\u8a73\u7d30\u306b\u3064\u3044\u3066\u306f\u3001java.util.Formatter\u30af\u30e9\u30b9\u3092\u53c2\u7167\u3057\u3088\u3046\u3002\n<\/p>\n<div class=\"code\">import java.text.DecimalFormat;<br \/>\nimport java.text.NumberFormat;<br \/>\npublic class FormattedOutputTest {<br \/>\n    public static void main(String[] args) {<br \/>\n\tString user = &#8220;hiro345&#8221;;<br \/>\n\tint point = 1000;<br \/>\n\t\/\/ 1.4 code<br \/>\n\tSystem.out.print(&#8220;new line\\n&#8221;);<br \/>\n\tNumberFormat f = new DecimalFormat(&#8220;####0&#8221;);<br \/>\n\tf.setMinimumIntegerDigits(5);<br \/>\n\tSystem.out.print(&#8220;user:&#8221; + user + &#8220;, point:&#8221; + f.format(point) + &#8220;\\n&#8221;);<br \/>\n\t\/\/ 1.5 code<br \/>\n\tSystem.out.printf(&#8220;new line\\n&#8221;);<br \/>\n\tSystem.out.printf(&#8220;user:%s, point:%05d\\n&#8221;, user, point);<br \/>\n    }<br \/>\n}<\/div>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u6b21\u306e\u901a\u308a\u3002\n<\/p>\n<div class=\"code\">$ javac -source 1.5 FormattedOutputTest.java<br \/>\n$ java FormattedOutputTest<br \/>\nnew line<br \/>\nuser:hiro345, point:01000<br \/>\nnew line<br \/>\nuser:hiro345, point:01000\n<\/div>\n<h2>java.awt.MouseInfo<\/h2>\n<p>\njava.awt.MouseInfo\u30af\u30e9\u30b9\u3092\u4f7f\u3046\u3068\u3001\u753b\u9762\u4e0a\u306e\u30de\u30a6\u30b9\u5ea7\u6a19\u4f4d\u7f6e\u3084\u63a5\u7d9a\u3055\u308c\u3066\u3044\u308b\u30de\u30a6\u30b9\u306e\u30dc\u30bf\u30f3\u6570\u306a\u3069\u304c\u308f\u304b\u308b\u3002\u30dd\u30fc\u30ea\u30f3\u30b0\u3092\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u304c\u3001\u3042\u308b\u5834\u6240\u306b\u30de\u30a6\u30b9\u30dd\u30a4\u30f3\u30bf\u304c\u304a\u304b\u308c\u305f\u3089\u753b\u9762\u3092\u8868\u793a\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u304b\u3001Xeyes\u306e\u3088\u3046\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002\u6b21\u306ejava.awt.MouseInfo\u52d5\u4f5c\u78ba\u8a8d\u7528\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u3053\u3053\u307e\u3067\u7d39\u4ecb\u3057\u305f\u30af\u30e9\u30b9\u3092\u5229\u7528\u3057\u3066\u4f5c\u6210\u3057\u305f\u3002\n<\/p>\n<div class=\"code\">import java.awt.PointerInfo;<br \/>\nimport java.awt.MouseInfo;<br \/>\nimport java.util.Scanner;<br \/>\npublic class MouseInfoTest implements Runnable {<br \/>\n    private volatile Thread runner = null;<br \/>\n    private int INTERVAL = 1000;<br \/>\n    public void start() {<br \/>\n\tif (runner == null) {<br \/>\n\t    runner = new Thread(this);<br \/>\n\t}<br \/>\n\trunner.start();<br \/>\n    }<br \/>\n    public void run() {<br \/>\n\tThread current = Thread.currentThread();<br \/>\n        while (current == runner) {<br \/>\n\t    PointerInfo p = MouseInfo.getPointerInfo();<br \/>\n\t    int x = p.getLocation().x;<br \/>\n\t    int y = p.getLocation().y;<br \/>\n\t    System.out.printf(&#8220;(x, y):(%2d, %2d)\\n&#8221;, x, y);<br \/>\n\t    try {<br \/>\n\t\tThread.sleep(INTERVAL);<br \/>\n\t    } catch(InterruptedException e) {<br \/>\n\t    }<br \/>\n\t}<br \/>\n    }<br \/>\n    public void stop() {<br \/>\n\trunner = null;<br \/>\n    }<br \/>\n    public static void main(String[] args) {<br \/>\n\tMouseInfoTest info = new MouseInfoTest();<br \/>\n\tinfo.start();<br \/>\n\tSystem.out.println(&#8220;Please enter quit to stop this program.&#8221;);<br \/>\n\tScanner s = Scanner.create(System.in);<br \/>\n\tString param = s.next();<br \/>\n\tinfo.stop();<br \/>\n\ts.close();<br \/>\n    }<br \/>\n}<\/div>\n<p>\u52d5\u4f5c\u3055\u305b\u308b\u306b\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u3059\u308b\u3002Ctrl-C\u3067\u505c\u6b62\u3055\u305b\u3066\u3082\u826f\u3044\u306e\u3060\u304c\u3001\u3044\u3061\u304a\u3046\u505c\u6b62\u3059\u308b\u624b\u6bb5\u3092\u63d0\u4f9b\u3059\u308b\u3068\u8a00\u3046\u3053\u3068\u3067\u3001quit\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u5165\u529b\u3057\u3066[Enter]\u30ad\u30fc\u3092\u3044\u308c\u308b\u3068\u505c\u6b62\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u305f\u3002\u305f\u3060\u3001\u30de\u30a6\u30b9\u306e\u60c5\u5831\u304c\u3053\u307e\u3081\u306b\u51fa\u529b\u3055\u308c\u3066\u304f\u308b\u306e\u3067\u3061\u3087\u3063\u3068\u308f\u304b\u308a\u306b\u304f\u3044\u3002\u30de\u30a6\u30b9\u306e\u60c5\u5831\u306f\u30a6\u30a3\u30f3\u30c9\u30a6\u8868\u793a\u306e\u65b9\u304c\u3088\u304b\u3063\u305f\u304b\u3082\u3002(^^;\n<\/p>\n<div class=\"code\">$ javac -source 1.5 MouseInfoTest.java<br \/>\n$ java MouseInfoTest<br \/>\nPlease enter quit to stop this program.<br \/>\n(x, y):(569, 47)<br \/>\n(x, y):(569, 47)<br \/>\nquit<br \/>\n$<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ref: J2SE 1.5 in a Nutshell Nutshell\u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u6b21\u306e\u5185\u5bb9\u306b\u3064\u3044\u3066\u307f\u3066\u307f\u3088\u3046\u3002 \u30fbFormatted Input \u30fbFormatted Output \u307e\u305f\u30011.5 \u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f\u65b0\u3057 &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/508.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-508","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":0,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}