Advertisement
Guest User

DoubleLong

a guest
Feb 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. package Console;
  2.  
  3. import java.io.IOException;
  4.  
  5. public class Console {
  6.  
  7.     public static void printPrompt(String prompt) {
  8.     System.out.print(prompt+" ");
  9.     System.out.flush();
  10.     }
  11.     public static String readLine() {
  12.         int ch;
  13.         boolean done = false;
  14.         String r = "";
  15.         while(!done) {
  16.             try {
  17.                 ch = System.in.read();
  18.                 if((char) ch == '\n') done = true;
  19.                 else if (ch !='\r') r= r+(char)ch;
  20.             }
  21.             catch(IOException e) {
  22.                 done = true;
  23.             }
  24.            
  25.         }
  26.         return r;
  27.     }
  28.    
  29.     public static String readLine(String prompt) {
  30.         printPrompt(prompt);
  31.         return readLine();
  32.     }
  33.     public static int readInt(String prompt) {
  34.         while (true) {
  35.             printPrompt(prompt);
  36.             try {
  37.                 return Integer.parseInt(readLine().trim());
  38.             }
  39.             catch(NumberFormatException e) {
  40.                 System.out.println("Not an integer!");
  41.                
  42.             }
  43.         }
  44.     }
  45.    
  46.     public static long readLong(String prompt) {
  47.         while (true) {
  48.             printPrompt(prompt);
  49.             try {
  50.                 return Long.parseLong(readLine().trim());
  51.             }
  52.             catch(NumberFormatException e) {
  53.                 System.out.println("Not a long!");
  54.                
  55.             }
  56.         }
  57.     }
  58.    
  59.     public static double readDouble(String prompt) {
  60.         while (true) {
  61.             printPrompt(prompt);
  62.             try {
  63.                 return Double.parseDouble(readLine().trim());
  64.             }
  65.             catch(NumberFormatException e) {
  66.                 System.out.println("Not a double!");
  67.                
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement