Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java input and output common class Scanner     - Setting Linux desktop environment, achieve HiDPI display support (Linux)

- Big Data Common Glossary (Linux)

- Use Ganglia to monitor your cluster (Server)

- iOS9 new feature - stacked view UIStackView (Programming)

- GAMIT learning materials finishing (Linux)

- open V switch port mirror in OpenStack neutron (Server)

- Execute command sentence can result in equipment permanently bricked in Linux laptop (Linux)

- CentOS 6.7 compile and install LAMP (Server)

- CentOS 6.5 install VNC-Server (Linux)

- How to install Linux Kernel 4.4 on Ubuntu (Linux)

- Debian 8.1 (amd64) deployed Memcached (Server)

- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)

- Java thread lifecycle (Programming)

- Linux common network tools: hping Advanced Host Scan (Linux)

- How to use scientific data Docker quickly configure the development environment (Server)

- Fedora10 use Git version Configuration Management (Linux)

- Android Studio and Git Git configuration file status (Linux)

- About Nginx 7 (Server)

- Python script running in the background (Programming)

- Java MVC CRUD examples (Programming)

 
         
  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:      
 
- Kibana use installation (Linux)
- Linux system security configuration (Linux)
- Analysis of Java exception (Programming)
- How to use secure FTP file transfer (Server)
- Upload the project to GitHub, synchronous remote repository Github (Linux)
- iOS persistence of data archiving NSKeyedArchiver (Programming)
- Linux tmux tcpdump summary (Linux)
- Android graphic introduction NDK installation and simple jni demo implementation (Linux)
- The Java development environment to build under Ubuntu 14.04 (Linux)
- Linux system on how to use rsync to synchronize data (Server)
- Android realize RippleEffect water (Programming)
- Linux use logs to troubleshoot (Linux)
- Linux shell script debugging (Linux)
- Let OS X support NTFS write file (Linux)
- CentOS 6.6 install JDK7 (Linux)
- Ubuntu 12.04 64-bit installation Redmine + Git + ReviewBoard (Linux)
- Ubuntu 14.04 kernel after the restart boot black screen to solve (Linux)
- How to upgrade to Ubuntu 14.04 Ubuntu 14.10 (Linux)
- Singleton (Linux)
- These days have been tossing in the Linux under the ASP.NET 5, on the next in the other operating systems in the ASP.NET 5 or. NET applications, in order to complete the MS VM (CoreCLR) run is not far Reach, the effect of the application.

Cur
(Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.