Skip to content

Commit

Permalink
BAEL-1760 Console I/O in Java (#4379)
Browse files Browse the repository at this point in the history
* 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
IvanLjubicic authored and KevinGilmore committed Jun 30, 2018
1 parent c66845c commit 4e785fd
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
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.");

}
}
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();

}

}

0 comments on commit 4e785fd

Please sign in to comment.