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

    Output will consist of a series of lines, one for each line of the input. Each line will consist of the position of the string in its…

    • 525 Words
    • 3 Pages
    Good Essays
  • Good Essays

    Programming

    • 3038 Words
    • 13 Pages

    2. Each of the flowchart segments in Figure 3-35 is unstructured. Redraw each flowchart segment so that it does the same thing but is structured.…

    • 3038 Words
    • 13 Pages
    Good Essays
  • Good Essays

    Java

    • 5076 Words
    • 21 Pages

    Explanation: B) Programs are classified as software to differentiate them from the mechanisms of the computer (hardware). Storage and the processor are two forms of hardware while input is the information that the program processes.…

    • 5076 Words
    • 21 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
  • 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

    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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Good Essays

    Char D1

    • 599 Words
    • 3 Pages

    int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);…

    • 599 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    C# String Functions

    • 325 Words
    • 2 Pages

    14. Substring - Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.…

    • 325 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    It 210

    • 2960 Words
    • 12 Pages

    Although the value of a variable may change during execution of a program, in all our programs so far, a single value has been associated with each variable name at any given time. In this chapter, we will discuss the concept of an array—a collection of variables of the same type and referenced by the same name. We will discuss one-dimensional arrays (lists) at length and focus briefly on twodimensional arrays (tables). You will learn how to set up and use arrays to accomplish various tasks.…

    • 2960 Words
    • 12 Pages
    Powerful Essays

Related Topics