From 4e785fdc152de8601917a13bfe897ad3ec8f5426 Mon Sep 17 00:00:00 2001 From: IvanLjubicic Date: Sat, 30 Jun 2018 05:27:36 +0200 Subject: [PATCH] BAEL-1760 Console I/O in Java (#4379) * ivan.ljubicic.app.developer@gmail.com * Added unit tests, configuration class and minor adjustments * primefaces intro module added * deleted primefaces old module * deleted different bean injection types sample project * deleted addition different bean injection types file * Renaming archetype in web.xml * Added primefaces in jsf module * Primefaces improvements * Added commandButton and dialog * Added PFM * Code formatting * Update pom.xml * Formatting changes * ConsoleDemo initial version * Added new classes, renamed ConsoleDemo class * Removed System.in class, renamed Scanner class and reorganized * Added more method examples --- .../baeldung/console/ConsoleConsoleClass.java | 26 +++++++ .../baeldung/console/ConsoleScannerClass.java | 76 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/console/ConsoleConsoleClass.java create mode 100644 core-java/src/main/java/com/baeldung/console/ConsoleScannerClass.java diff --git a/core-java/src/main/java/com/baeldung/console/ConsoleConsoleClass.java b/core-java/src/main/java/com/baeldung/console/ConsoleConsoleClass.java new file mode 100644 index 0000000000..a5c704345f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/console/ConsoleConsoleClass.java @@ -0,0 +1,26 @@ +package com.baeldung.console; + +import java.io.Console; + +public class ConsoleConsoleClass { + + public static void main(String[] args) { + Console console = System.console(); + + if (console == null) { + System.out.print("No console available"); + return; + } + + String progLanguauge = console.readLine("Enter your favourite programming language: "); + console.printf(progLanguauge + " is very interesting!"); + + char[] pass = console.readPassword("To finish, enter password: "); + + if ("BAELDUNG".equals(pass.toString().toUpperCase())) + console.printf("Good! Regards!"); + else + console.printf("Nice try. Regards."); + + } +} diff --git a/core-java/src/main/java/com/baeldung/console/ConsoleScannerClass.java b/core-java/src/main/java/com/baeldung/console/ConsoleScannerClass.java new file mode 100644 index 0000000000..7b7a7a4ade --- /dev/null +++ b/core-java/src/main/java/com/baeldung/console/ConsoleScannerClass.java @@ -0,0 +1,76 @@ +package com.baeldung.console; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; +import java.util.regex.Pattern; + +public class ConsoleScannerClass { + + public static void main(String[] args) { + System.out.println("Please enter your name and surname: "); + + Scanner scanner = new Scanner(System.in); + + String nameSurname = scanner.nextLine(); + + System.out.println("Please enter your gender: "); + + char gender = scanner.next().charAt(0); + + System.out.println("Please enter your age: "); + + int age = scanner.nextInt(); + + System.out.println("Please enter your height in meters: "); + + double height = scanner.nextDouble(); + + System.out.println(nameSurname + ", " + age + ", is a great " + (gender == 'm' ? "guy" : "girl") + " with " + height + " meters height" + " and " + (gender == 'm' ? "he" : "she") + " reads Baeldung."); + + System.out.print("Have a good"); + System.out.print(" one!"); + + System.out.println("\nPlease enter number of years of experience as a developer: "); + + BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); + + int i = 0; + + try { + i = Integer.parseInt(buffReader.readLine()); + } catch (NumberFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + System.out.println("You are a " + (i > 5 ? "great" : "good") + " developer!"); + + int sum = 0, count = 0; + + System.out.println("Please enter your college degrees. To finish, enter baeldung website url"); + + while (scanner.hasNextInt()) { + int nmbr = scanner.nextInt(); + sum += nmbr; + count++; + } + int mean = sum / count; + + System.out.println("Your average degree is " + mean); + + if (scanner.hasNext(Pattern.compile("www.baeldung.com"))) + System.out.println("Correct!"); + else + System.out.println("Baeldung website url is www.baeldung.com"); + + if (scanner != null) + scanner.close(); + + } + +}