Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java input and output common class Scanner     - AWK principles and commands and file input (Linux)

- Several SQL Server data migration / Export Import Practice (Database)

- Ubuntu 15.04 Linux install anti-virus software Antiviral 0.2 (Linux)

- Depth understanding of JavaScript new mechanism (Programming)

- Using C ++ Container Templates in Shared Memory (Programming)

- Difference Docker mirror and containers (Server)

- Java, on the dfile.encoding Systemproperty (Programming)

- VirtualBox CentOS is configured as a local disk mirroring software source (Linux)

- EXP-00091: Exporting questionable statistics Processing Method (Database)

- Django1.8 return json json string and the string contents of the received post (Programming)

- ORA-00600 error solve one case (Database)

- Oracle 10g relations with the constraint of column properties NULLABLE (Database)

- Ubuntu 14.04 kernel after the restart boot black screen to solve (Linux)

- Iptables in Ubuntu (Linux)

- Java and Python use make way dictionary word search script (Programming)

- SolrCloud-5.2.1 cluster deployment and testing (Server)

- How to convert images, audio and video formats on Ubuntu (Linux)

- iptraf: A Practical TCP / UDP network monitoring tools (Linux)

- The three-way division of the sorting algorithm Quicksort (Programming)

- How to override the plain text files and directories soft connection in linux (Linux)

  Java input and output common class Scanner
  Add Date : 2018-11-21      
  Scaner class, use the Get keyboard input.

public boolean DemoTest () {

Scanner input = new Scanner (System.in);

System.out.print ( "Please enter the student's name");

String name = input.next ();

int score = 0;

double sum = 0;

double average = 0;

for (int i = 0; i <5; i ++) {

System.out.print ( "Please enter [" + (i + 1) + "] course accomplishments");

score = input.nextInt ();

sum + = score;


average = sum / 5;

System.out.println ( "student" + name + "grade point average is" + average);

return true;


Use JUnit3 test code.

/ **


* /

package com.ch01;

import ch01.ScannerTest;

import junit.framework.Assert;

import junit.framework.TestCase;

/ **


* /

public class ScannerTestCase extends TestCase {

public void testch01Scanner () {

ScannerTest input = new ScannerTest ();

Assert.assertEquals (true, input.DemoTest ());



Detailed Scanner

java.lang.Object - >>> java.util.Scanner

All Implemented Interfaces - >>> Iterator

You can use a regular expression to parse simple text strings and basic types of scanners.

Scanner using delimiter mode its input into tokens, the default separator pattern and matches whitespace. You can then use the tag conversion method will be different next to the value of different types.

For example, the following code enables the user to read a number from System.in:

Scanner sc = new Scanner (System.in);

int i = sc.nextInt ();

As another example, the following code allows long types can be assigned from entries myNumbers file:

Scanner sc = new Scanner (new File ( "myNumbers"));

while (sc.hasNextLong ()) {

long aLong = sc.nextLong ();


The scanner can also use different from the blank separators. Here is an example of reading a number from a string:

String input = "1 fish 2 fish red fish blue fish";

Scanner s = new Scanner (input) .useDelimiter ( "\\ s * fish \\ s *");

System.out.println (s.nextInt ());

System.out.println (s.nextInt ());

System.out.println (s.next ());

System.out.println (s.next ());

s.close (); output:





The following code uses a regular expression simultaneously parse all four markers, and produces the same output of the above example:

String input = "1 fish 2 fish red fish blue fish";

Scanner s = new Scanner (input);

s.findInLine ( "(\\ d +) fish (\\ d +) fish (\\ w +) fish (\\ w +)");

MatchResult result = s.match ();

for (int i = 1; i <= result.groupCount (); i ++)

System.out.println (result.group (i));


The default whitespace scanner used to identify by Character.isWhitespace. Regardless of whether the previous change, reset () method will scanner delimiter value is reset to the default blank separator.
- 20 Unix / Linux command skills (Linux)
- Share and show your code on GitHub (Linux)
- Use PuTTY to access the virtual machine Linux under Windows xp (Programming)
- Use Spring cache and ehcache (Programming)
- Proxmox VE implement KVM OpenVZ virtualization cloud computing (Server)
- Sublime Text 3 best features, plug-ins and settings (Linux)
- OpenStack image production in the CentOS 6.2 (Linux)
- Android WebView use layman (Programming)
- Use virt-manager to create and manage virtual machines (Linux)
- Windows Desktop use VNC remote connect Linux (Linux)
- CentOS 6.5 install Firefox (Linux)
- Ubuntu 14.04.02 LTS startup items erroneous writing / dev / sda1 (win 7 loader) Repair (Linux)
- Python 2 Chinese garbage problem solved (Linux)
- Linux shell in back quotes (`) Application (Linux)
- Add your own kernel and ramfs based on an existing Linux LiveCD (Linux)
- Present Situation and Development Trend of firewall products (Linux)
- Use Epoll develop high-performance application server on Linux (Server)
- Redhat 5 prohibit IPv6 (Linux)
- When Linux virtual machine to another copy of the operating system, a static IP NAT mode Invalid (Linux)
- Use CutyCapt to convert HTML pages to png images on Linux (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.