-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
c66845c
commit 4e785fd
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
core-java/src/main/java/com/baeldung/console/ConsoleConsoleClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
|
||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
core-java/src/main/java/com/baeldung/console/ConsoleScannerClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
|
||
} |