master
lorenzo 2024-04-08 12:40:45 +02:00
parent f9e4108705
commit da61ba4641
2 changed files with 25 additions and 3 deletions

View File

@ -5,7 +5,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
public class Console { public class Console {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static String readString() { public static String readString() {
@ -19,7 +19,8 @@ public class Console {
public static char[] readCharArray() { public static char[] readCharArray() {
try { try {
return reader.readLine().toCharArray(); String input = reader.readLine();
return input.toCharArray();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return null; // oder eine geeignete Fehlerbehandlung return null; // oder eine geeignete Fehlerbehandlung
@ -28,7 +29,7 @@ public class Console {
public static boolean readBoolean() { public static boolean readBoolean() {
try { try {
String input = reader.readLine().toLowerCase(); String input = reader.readLine().toLowerCase().trim();
return input.equals("true") || input.equals("t") || input.equals("yes") || input.equals("y"); return input.equals("true") || input.equals("t") || input.equals("yes") || input.equals("y");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -4,10 +4,31 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import org.junit.Before;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
public class ConsoleTest { public class ConsoleTest {
private final InputStream originalSystemIn = System.in;
private final PrintStream originalSystemOut = System.out;
private ByteArrayOutputStream systemOutContent;
@Before
public void setUpStreams() {
systemOutContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(systemOutContent));
}
@After
public void restoreStreams() {
System.setIn(originalSystemIn);
System.setOut(originalSystemOut);
}
@Test @Test
public void testReadString() { public void testReadString() {