Skip to content

Commit 4e785fd

Browse files
IvanLjubicicKevinGilmore
authored andcommitted
BAEL-1760 Console I/O in Java (eugenp#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
1 parent c66845c commit 4e785fd

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.console;
2+
3+
import java.io.Console;
4+
5+
public class ConsoleConsoleClass {
6+
7+
public static void main(String[] args) {
8+
Console console = System.console();
9+
10+
if (console == null) {
11+
System.out.print("No console available");
12+
return;
13+
}
14+
15+
String progLanguauge = console.readLine("Enter your favourite programming language: ");
16+
console.printf(progLanguauge + " is very interesting!");
17+
18+
char[] pass = console.readPassword("To finish, enter password: ");
19+
20+
if ("BAELDUNG".equals(pass.toString().toUpperCase()))
21+
console.printf("Good! Regards!");
22+
else
23+
console.printf("Nice try. Regards.");
24+
25+
}
26+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.console;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Scanner;
7+
import java.util.regex.Pattern;
8+
9+
public class ConsoleScannerClass {
10+
11+
public static void main(String[] args) {
12+
System.out.println("Please enter your name and surname: ");
13+
14+
Scanner scanner = new Scanner(System.in);
15+
16+
String nameSurname = scanner.nextLine();
17+
18+
System.out.println("Please enter your gender: ");
19+
20+
char gender = scanner.next().charAt(0);
21+
22+
System.out.println("Please enter your age: ");
23+
24+
int age = scanner.nextInt();
25+
26+
System.out.println("Please enter your height in meters: ");
27+
28+
double height = scanner.nextDouble();
29+
30+
System.out.println(nameSurname + ", " + age + ", is a great " + (gender == 'm' ? "guy" : "girl") + " with " + height + " meters height" + " and " + (gender == 'm' ? "he" : "she") + " reads Baeldung.");
31+
32+
System.out.print("Have a good");
33+
System.out.print(" one!");
34+
35+
System.out.println("\nPlease enter number of years of experience as a developer: ");
36+
37+
BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
38+
39+
int i = 0;
40+
41+
try {
42+
i = Integer.parseInt(buffReader.readLine());
43+
} catch (NumberFormatException e) {
44+
// TODO Auto-generated catch block
45+
e.printStackTrace();
46+
} catch (IOException e) {
47+
// TODO Auto-generated catch block
48+
e.printStackTrace();
49+
}
50+
51+
System.out.println("You are a " + (i > 5 ? "great" : "good") + " developer!");
52+
53+
int sum = 0, count = 0;
54+
55+
System.out.println("Please enter your college degrees. To finish, enter baeldung website url");
56+
57+
while (scanner.hasNextInt()) {
58+
int nmbr = scanner.nextInt();
59+
sum += nmbr;
60+
count++;
61+
}
62+
int mean = sum / count;
63+
64+
System.out.println("Your average degree is " + mean);
65+
66+
if (scanner.hasNext(Pattern.compile("www.baeldung.com")))
67+
System.out.println("Correct!");
68+
else
69+
System.out.println("Baeldung website url is www.baeldung.com");
70+
71+
if (scanner != null)
72+
scanner.close();
73+
74+
}
75+
76+
}

0 commit comments

Comments
 (0)