From 0267aa528b83be9914fee4bea8f548b8404b31f8 Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Mon, 12 Dec 2022 17:59:25 +0000 Subject: [PATCH] 8297288: Example code in Scanner class Reviewed-by: lancea, bpb, alanb --- .../share/classes/java/util/Scanner.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/java.base/share/classes/java/util/Scanner.java b/src/java.base/share/classes/java/util/Scanner.java index 9e544880ae2..f270a46cc49 100644 --- a/src/java.base/share/classes/java/util/Scanner.java +++ b/src/java.base/share/classes/java/util/Scanner.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -51,24 +51,28 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter; * various {@code next} methods. * *

For example, this code allows a user to read a number from - * {@code System.in}: - *

{@code
- *     Scanner sc = new Scanner(System.in);
- *     int i = sc.nextInt();
- * }
+ * the console. + * {@snippet : + * var con = System.console(); + * if (con != null) { + * // @link substring="reader()" target="java.io.Console#reader()" : + * Scanner sc = new Scanner(con.reader()); + * int i = sc.nextInt(); + * } + * } * *

As another example, this code allows {@code long} types to be * assigned from entries in a file {@code myNumbers}: - *

{@code
+ * {@snippet :
  *      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: - *

{@code
+ * {@snippet :
  *     String input = "1 fish 2 fish red fish blue fish";
  *     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
  *     System.out.println(s.nextInt());
@@ -76,7 +80,7 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
  *     System.out.println(s.next());
  *     System.out.println(s.next());
  *     s.close();
- * }
+ * } *

* prints the following output: *

{@code
@@ -88,7 +92,7 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
  *
  * 

The same output can be generated with this code, which uses a regular * expression to parse all four tokens at once: - *

{@code
+ * {@snippet :
  *     String input = "1 fish 2 fish red fish blue fish";
  *     Scanner s = new Scanner(input);
  *     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
@@ -96,7 +100,7 @@ import sun.util.locale.provider.ResourceBundleBasedAdapter;
  *     for (int i=1; i<=result.groupCount(); i++)
  *         System.out.println(result.group(i));
  *     s.close();
- * }
+ * } * *

The default whitespace delimiter used * by a scanner is as recognized by {@link Character#isWhitespace(char)