Preview

java

Satisfactory Essays
Open Document
Open Document
258 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
java
OverviewPackageClassUseTreeDeprecatedIndexHelp Java™ Platform
Standard Ed. 7 Prev ClassNext ClassFramesNo FramesAll ClassesSummary: Nested | Field | Constr | MethodDetail: Field | Constr | Method java.util Class Scanner

java.lang.Object java.util.Scanner All Implemented Interfaces:
Closeable, AutoCloseable, Iterator

public final class Scanner extends Object implements Iterator, Closeable
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:

Scanner sc = new Scanner(System.in); int i = sc.nextInt(); As another example, this code allows long types to be assigned from entries in a file myNumbers:

Scanner sc = new Scanner(new File("myNumbers")); while (sc.hasNextLong()) { long aLong = sc.nextLong(); }
The scanner can also use delimiters other than whitespace. This example reads several items in 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(); prints the following output:

1 2 red blue
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:

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;

You May Also Find These Documents Helpful

  • Good Essays

    Week 1 Lab_ CIS_115

    • 439 Words
    • 2 Pages

    List all variables you will use (use valid variable names). Indicate whether the data type is string, integer, or decimal, and so on.…

    • 439 Words
    • 2 Pages
    Good Essays
  • Satisfactory Essays

    Example: “ Los Angeles,” maximum 255 characters unless MAX (MAX allows 2^31 – 1 bytes)…

    • 327 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    unit 6

    • 360 Words
    • 2 Pages

    14) The LIKE keyword can be used with the '%' to search for patterns in character data.…

    • 360 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Hrm Labor

    • 363 Words
    • 2 Pages

    | (TCO 2) In SQL which of the following parameters will allow you to add a space as a valid character in your output.…

    • 363 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    cis121 chapter 2 and 3

    • 993 Words
    • 6 Pages

    A ____ read is an added statement that gets the first input value in a program.…

    • 993 Words
    • 6 Pages
    Satisfactory Essays
  • Better Essays

    E-Commerce Exercise

    • 1068 Words
    • 5 Pages

    2. In addition to the methods covered in this chapter, Java provides a number of classes used to convert and format…

    • 1068 Words
    • 5 Pages
    Better Essays
  • Satisfactory Essays

    Overall I am disappointed with the code itself as it doesn’t feature some of the main aspects that I would of liked, I am happy however that I eventually was able to be able to match terms and produce unique sayings for each number.…

    • 351 Words
    • 1 Page
    Satisfactory Essays
  • Good Essays

    Java

    • 490 Words
    • 3 Pages

    Write a Java program to demonstrate using bitmaps and bitwise operators to sort and remove duplicates from a file of random phone numbers. Do not confuse the term bitmap used for compressing data into smaller spaces with the bitmap that has come to mean a graphic image.…

    • 490 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    Informative Speech Outline

    • 1416 Words
    • 6 Pages

    2. Type 2 diabetes is caused by obesity, and according to the Diabetes Update; it is the most common form of the disease.…

    • 1416 Words
    • 6 Pages
    Powerful Essays
  • Satisfactory Essays

    Filler Pic

    • 734 Words
    • 3 Pages

    02 FILLER PIC X(4) VALUE SPACES. 02 FILLER PIC X(7) VALUE 'REMARKS'. 01 SUB2. 02 FILLER PIC X(2) VALUE SPACES. 02 FILLER PIC X(6) VALUE 'Number'.…

    • 734 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    For Loop

    • 534 Words
    • 3 Pages

    Another type of loop you can use in Java is called the while loop. While loops are a lot easier to understand than for loops. Here's what they look like:…

    • 534 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    package lab4.t1; import java.net. *; import java.io. *; public class Lab4T1 public static void main(String[] args) throws Exception { { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = "…

    • 310 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    • • • • • • String Fundamentals Library Functions Input Data Validation Formatting Strings (Optional) Case Study: Character and Word Counting Common Programming and Compiler Errors…

    • 1527 Words
    • 7 Pages
    Good Essays
  • Good Essays

    System.out.println("Welcome to Airline Services. We Provide best flights and deals from Delhi and Mumbai to many cities Domestic as well as International");…

    • 5155 Words
    • 21 Pages
    Good Essays
  • Powerful Essays

    Basic Shell Scripting

    • 33932 Words
    • 136 Pages

    Basic Control Statements 44 The if Statement 44 The test Command and Bracket Notation 46 The while Statement 48 The for Statement 50 The case statement 53 The expr Command 56 Parsing, Variable Expansion, and Quoting 59 Variable Expansion and Field Separators 60 Special Characters Explained 61 Quoting Special Characters 64 Inline Execution 66…

    • 33932 Words
    • 136 Pages
    Powerful Essays

Related Topics