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

    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) Use the ____ data type for columns that contain letters and special characters and for columns containing numbers that will not be used in calculations.…

    • 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

    • 827 Words
    • 4 Pages

    9. The withholding tax on a weekly salary (let us assume) is computed as follows: 14% of the difference between a person's gross pay and $13 times the number of dependents he or she claims. Read the values for the gross pay and the number of dependents; print the withholding tax.…

    • 827 Words
    • 4 Pages
    Good Essays
  • Satisfactory Essays

    Filler Pic

    • 734 Words
    • 3 Pages

    02 FILLER PIC X(2) VALUE SPACES. 02 FILLER PIC X(8) VALUE 'Examinee'. 02 FILLER PIC X(5) VALUE SPACES. 02 FILLER PIC X(8) VALUE 'Examinee'. 02 FILLER PIC X(4) VALUE SPACES.…

    • 734 Words
    • 3 Pages
    Satisfactory 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

    java

    • 312 Words
    • 2 Pages

    AllocationStrategy.java import java.util. ArrayList; import java.util. Collection; import java.util. Iterator; import java.util. List; import java.util.…

    • 312 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Essay of favorite place

    • 516 Words
    • 3 Pages

    Describes the combined set of all letters in the alphabet and the numbers 0 through 9. It is useful to group letters and numbers together because many programs treat them identically, and differently from punctuation characters. For example, most operating systems allow you to use any letters or numbers in filenames but prohibit many punctuation characters. Your computermanual would express this rule by stating: "Filenames may be composed of alphanumeric characters."…

    • 516 Words
    • 3 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

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

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

Related Topics