JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) (#14361)

* JAVA-22516 Split or move core-java-io-apis-2 module (moved-1)

* JAVA-22516 Adding back the file after fixing the conflicts

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-07-14 20:30:39 +03:00
committed by GitHub
parent 346418a266
commit f2ab4c2aa8
33 changed files with 461 additions and 369 deletions
@@ -0,0 +1,36 @@
package com.baeldung.multinput;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MultiInputs {
public void UsingSpaceDelimiter(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2);
}
public void UsingREDelimiter(){
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("[\\s,]+");
System.out.print("Enter two numbers separated by a space or a comma: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2);
}
public void UsingCustomDelimiter(){
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter(";");
System.out.print("Enter two numbers separated by a semicolon: ");
try { int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("You entered " + num1 + " and " + num2); }
catch (InputMismatchException e)
{ System.out.println("Invalid input. Please enter two integers separated by a semicolon."); }
}
}
@@ -1,96 +0,0 @@
package com.baeldung.scanner;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.LogManager;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;
@Log4j
public class HasNextVsHasNextLineDemo {
private static final String LINE = "----------------------------";
private static final String END_LINE = "--------OUTPUT--END---------\n";
private static final String INPUT = new StringBuilder()
.append("magic\tproject\n")
.append(" database: oracle\n")
.append("dependencies:\n")
.append("spring:foo:bar\n")
.append("\n").toString();
private static void hasNextBasic() {
printHeader("hasNext() Basic");
Scanner scanner = new Scanner(INPUT);
while (scanner.hasNext()) {
log.info(scanner.next());
}
log.info(END_LINE);
scanner.close();
}
private static void hasNextWithDelimiter() {
printHeader("hasNext() with delimiter");
Scanner scanner = new Scanner(INPUT);
while (scanner.hasNext()) {
String token = scanner.next();
if ("dependencies:".equals(token)) {
scanner.useDelimiter(":");
}
log.info(token);
}
log.info(END_LINE);
scanner.close();
}
private static void hasNextWithDelimiterFixed() {
printHeader("hasNext() with delimiter FIX");
Scanner scanner = new Scanner(INPUT);
while (scanner.hasNext()) {
String token = scanner.next();
if ("dependencies:".equals(token)) {
scanner.useDelimiter(":|\\s+");
}
log.info(token);
}
log.info(END_LINE);
scanner.close();
}
private static void addLineNumber() {
printHeader("add line number by hasNextLine() ");
Scanner scanner = new Scanner(INPUT);
int i = 0;
while (scanner.hasNextLine()) {
log.info(String.format("%d|%s", ++i, scanner.nextLine()));
}
log.info(END_LINE);
scanner.close();
}
private static void printHeader(String title) {
log.info(LINE);
log.info(title);
log.info(LINE);
}
public static void main(String[] args) throws IOException {
setLogger();
hasNextBasic();
hasNextWithDelimiter();
hasNextWithDelimiterFixed();
addLineNumber();
}
//overwrite the logger config
private static void setLogger() throws IOException {
InputStream is = HasNextVsHasNextLineDemo.class.getResourceAsStream("/scanner/log4j.properties");
Properties props = new Properties();
props.load(is);
LogManager.resetConfiguration();
PropertyConfigurator.configure(props);
}
}
@@ -1,56 +0,0 @@
package com.baeldung.scanner;
import java.util.Scanner;
public class NextLineAfterNextMethods {
private static void produceSkippingNextLineMethod() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine(); // Skipped because it reads the remaining newline character
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV1() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV2() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = Integer.parseInt(scanner.nextLine());
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
private static void fixSkippingNextLineMethodV3() {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
scanner.skip("\r\n");
System.out.print("Enter your first name: ");
String firstName = scanner.nextLine();
System.out.println(age + ":" + firstName);
}
}
public static void main(String[] args) {
produceSkippingNextLineMethod();
fixSkippingNextLineMethodV1();
fixSkippingNextLineMethodV2();
fixSkippingNextLineMethodV3();
}
}