Android (java) Learning Note 97:scanner class use

Source: Internet
Author: User
<span id="Label3"></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">cn.itcast_01;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008000;"><span style="color: #008000;">* Scanner: used to receive keyboard input Data. </span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">* In front of the time:</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">* A: Guide Package</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">* B: Create object</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;">* C: Call Method</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008000;"><span style="color: #008000;">* There is a static field under the system Class:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">* public static final InputStream in; standard input stream, corresponding to keyboard Input. </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* InputStream is = system.in;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* Class Demo {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* public static final int x = ten;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* public static final Student s = new Student ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;"> * }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* int y = demo.x;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">* Student s = demo.s;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* Construction Method:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* Scanner (inputstream source)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Scanner;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Scannerdemo {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Creating Objects</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>Scanner sc =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Scanner (system.in);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>x =<span style="color: #000000;"><span style="color: #000000;">Sc.nextint ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span>System.out.println ("x:" +<span style="color: #000000;"><span style="color: #000000;">x);</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">cn.itcast_02;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.Scanner;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">* Basic Format:</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008000;"><span style="color: #008000;">* Public Boolean hasnextxxx (): determines whether a certain type of element</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">* Public Xxx nextxxx (): gets the element</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008000;"><span style="color: #008000;">* example: An int type Method Example</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #008000;"><span style="color: #008000;">* Public boolean hasnextint ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">* public int nextint ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">* Note:</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">* Inputmismatchexception: The input does not match the one you want</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Scannerdemo {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">main (string[] Args) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Creating Objects</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Scanner sc =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Scanner (system.in);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Data</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(sc.hasnextint ()) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>x =<span style="color: #000000;"><span style="color: #000000;">Sc.nextint ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>System.out.println ("x:" +<span style="color: #000000;"><span style="color: #000000;">x);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>SYSTEM.OUT.PRINTLN ("the Data you entered is wrong"<span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><p><p>Android (java) Learning Note 97:scanner class use</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.