Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Console;
- import java.io.IOException;
- public class Console {
- public static void printPrompt(String prompt) {
- System.out.print(prompt+" ");
- System.out.flush();
- }
- public static String readLine() {
- int ch;
- boolean done = false;
- String r = "";
- while(!done) {
- try {
- ch = System.in.read();
- if((char) ch == '\n') done = true;
- else if (ch !='\r') r= r+(char)ch;
- }
- catch(IOException e) {
- done = true;
- }
- }
- return r;
- }
- public static String readLine(String prompt) {
- printPrompt(prompt);
- return readLine();
- }
- public static int readInt(String prompt) {
- while (true) {
- printPrompt(prompt);
- try {
- return Integer.parseInt(readLine().trim());
- }
- catch(NumberFormatException e) {
- System.out.println("Not an integer!");
- }
- }
- }
- public static long readLong(String prompt) {
- while (true) {
- printPrompt(prompt);
- try {
- return Long.parseLong(readLine().trim());
- }
- catch(NumberFormatException e) {
- System.out.println("Not a long!");
- }
- }
- }
- public static double readDouble(String prompt) {
- while (true) {
- printPrompt(prompt);
- try {
- return Double.parseDouble(readLine().trim());
- }
- catch(NumberFormatException e) {
- System.out.println("Not a double!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement