Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java input and output common class Scanner     - Python, and / or (Programming)

- Java8 Lambda expressions and flow operations (Programming)

- Scope of variables in Object-C (Programming)

- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)

- HA-Federation-HDFS + Yarn cluster deployment (Server)

- Linux - Common process the command (Linux)

- After Pydev installation, Eclipse does not display solutions (Linux)

- Linux virtual machine settings network, hostname ssh access (Linux)

- Android project using the command to create and install the package (Programming)

- The ORA-01113 error is handled with BBED without archiving (Database)

- SendMail version of Java implementation with attachments (Programming)

- SQLite3 simple operation (Database)

- CentOS cross compiler core Raspberry Pi 2 (Linux)

- Use Python automatically cleared Android Engineering excess resources (Programming)

- mysql_config_editor encryption and decryption of the new features of MySQL realization (Database)

- Linux System Getting Started Learning: Using yum to download the RPM package without installing (Linux)

- Nginx server load balancing dual-system availability (Server)

- Ubuntu Tutorial: How to Upgrade a New Linux Kernel 3.12.7 on Ubuntu (Linux)

- Linux shell string interception and stitching (Linux)

- Use MySQL optimization of security to prevent misuse of aliases (Database)

 
         
  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:

1

2

red

blue

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));

S.CLOSE ();

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.
     
         
       
         
  More:      
 
- JITwatch installation under Linux (Linux)
- Linux centos rm directory does not prompt (Linux)
- Deepin Tutorial: Depth Description Installer expert mode (Linux)
- MySQL use benchmarking tool sysbench (Database)
- Tor Browser: under Linux for anonymous Web browsing ultimate browser (Linux)
- Java threads in the life cycle (Programming)
- Binary Packages Golang (Linux)
- struts2 completely the wrong way to capture 404 (Programming)
- Unsafe reboot start (Linux)
- Several Methods of SSH Auto - login (Linux)
- Ceph Source Analysis: Network Module (Server)
- Forgot Linux root password (Linux)
- Oracle ORA-01089 failure analysis (Database)
- Docker: Mirroring and container operations (Server)
- Json Applications of FastJson (Programming)
- To_explore Linux system boot process (Linux)
- How to use the Vault secure password storage and API key (Linux)
- Different between Linux file path and the windows (Linux)
- PostgreSQL export data dictionary documents (Database)
- Mac OS X 10.9 build Nginx + MySQL + php-fpm environment (Server)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.