File tree Expand file tree Collapse file tree 5 files changed +101
-0
lines changed
core-java-modules/core-java-26/src/main/java/baeldung/scannerinput Expand file tree Collapse file tree 5 files changed +101
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .scannerinput ;
2+
3+ import java .util .Scanner ;
4+
5+ public class DoWhileScanner {
6+ public static void main (String [] args ) {
7+ Scanner sc = new Scanner (System .in );
8+ String input ;
9+
10+ do {
11+ input = sc .nextLine ();
12+ System .out .println (input );
13+ } while (!input .equals ("exit" ));
14+
15+ sc .close ();
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scannerinput ;
2+
3+ import java .util .Scanner ;
4+
5+ public class EOFExample {
6+
7+ public static void main (String [] args ) {
8+ Scanner scan = new Scanner (System .in );
9+
10+ try {
11+ System .out .println ("Enter text (press CTRL+D on Unix/Mac or CTRL+Z on Windows to end):" );
12+
13+ while (scan .hasNextLine ()) {
14+ String line = scan .nextLine ();
15+ System .out .println ("You entered: " + line );
16+ }
17+
18+ System .out .println ("End of input detected. Program terminated." );
19+ } finally {
20+ scan .close ();
21+ }
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scannerinput ;
2+
3+ import java .util .Scanner ;
4+
5+ public class SampleScanner {
6+
7+ public static void main (String [] args ) {
8+ Scanner scan = new Scanner (System .in );
9+
10+ try {
11+ while (scan .hasNextLine ()) {
12+ String line = scan .nextLine ().toLowerCase ();
13+ System .out .println (line );
14+ }
15+ } finally {
16+ scan .close ();
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scannerinput ;
2+
3+ import java .util .Scanner ;
4+
5+ public class SampleScannerScan {
6+ public static void main (String [] args ) {
7+ Scanner scan = new Scanner (System .in );
8+ try {
9+ while (scan .hasNextLine ()) {
10+ String line = scan .nextLine ();
11+ if (line == null ) {
12+ System .out .println ("Exiting program (null check)..." );
13+ System .exit (0 );
14+ }
15+ System .out .println ("Input was: " + line );
16+ }
17+ } finally {
18+ scan .close ();
19+ }
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .scannerinput ;
2+
3+ import java .util .Scanner ;
4+
5+ public class SampleScannerSentinel {
6+ public static void main (String [] args ) {
7+ Scanner scan = new Scanner (System .in );
8+ try {
9+ while (scan .hasNextLine ()) {
10+ String line = scan .nextLine ().toLowerCase ();
11+ if (line .equals ("exit" )) {
12+ System .out .println ("Exiting program..." );
13+ break ;
14+ }
15+ System .out .println (line );
16+ }
17+ } finally {
18+ scan .close ();
19+ }
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments