Guest User

Untitled

a guest
Nov 10th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. Console co=System.console();
  2. System.out.println(co);
  3. try{
  4. String s=co.readLine();
  5. }
  6.  
  7. System.out.print("Enter something:");
  8. String input = System.console().readLine();
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13.  
  14. public class Test {
  15. public static void main(String[] args) throws IOException {
  16. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17. System.out.print("Enter String");
  18. String s = br.readLine();
  19. System.out.print("Enter Integer:");
  20. try{
  21. int i = Integer.parseInt(br.readLine());
  22. }catch(NumberFormatException nfe){
  23. System.err.println("Invalid Format!");
  24. }
  25. }
  26. }
  27.  
  28. Scanner in = new Scanner(System.in);
  29.  
  30. int i = in.nextInt();
  31. String s = in.next();
  32.  
  33. public class ConsoleReadingDemo {
  34.  
  35. public static void main(String[] args) {
  36.  
  37. // ====
  38. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  39. System.out.print("Please enter user name : ");
  40. String username = null;
  41. try {
  42. username = reader.readLine();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. System.out.println("You entered : " + username);
  47.  
  48. // ===== In Java 5, Java.util,Scanner is used for this purpose.
  49. Scanner in = new Scanner(System.in);
  50. System.out.print("Please enter user name : ");
  51. username = in.nextLine();
  52. System.out.println("You entered : " + username);
  53.  
  54.  
  55. // ====== Java 6
  56. Console console = System.console();
  57. username = console.readLine("Please enter user name : ");
  58. System.out.println("You entered : " + username);
  59.  
  60. }
  61. }
  62.  
  63. import java.io.Console;
  64.  
  65. public class Test {
  66.  
  67. public static void main(String[] args) throws Exception {
  68. Console console = System.console();
  69. if (console == null) {
  70. System.out.println("Unable to fetch console");
  71. return;
  72. }
  73. String line = console.readLine();
  74. console.printf("I saw this line: %s", line);
  75. }
  76. }
  77.  
  78. > javac Test.java
  79. > java Test
  80. Foo <---- entered by the user
  81. I saw this line: Foo <---- program output
  82.  
  83. import java.util.Scanner;
  84. String data;
  85.  
  86. Scanner scanInput = new Scanner(System.in);
  87. data= scanInput.nextLine();
  88.  
  89. scanInput.close();
  90. System.out.println(data);
  91.  
  92. import java.io.BufferedReader;
  93. import java.io.IOException;
  94. import java.io.InputStreamReader;
  95.  
  96. public class LoopingConsoleInputExample {
  97.  
  98. public static final String EXIT_COMMAND = "exit";
  99.  
  100. public static void main(final String[] args) throws IOException {
  101. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  102. System.out.println("Enter some text, or '" + EXIT_COMMAND + "' to quit");
  103.  
  104. while (true) {
  105.  
  106. System.out.print("> ");
  107. String input = br.readLine();
  108. System.out.println(input);
  109.  
  110. if (input.length() == EXIT_COMMAND.length() && input.toLowerCase().equals(EXIT_COMMAND)) {
  111. System.out.println("Exiting.");
  112. return;
  113. }
  114.  
  115. System.out.println("...response goes here...");
  116. }
  117. }
  118. }
  119.  
  120. Enter some text, or 'exit' to quit
  121. > one
  122. one
  123. ...response goes here...
  124. > two
  125. two
  126. ...response goes here...
  127. > three
  128. three
  129. ...response goes here...
  130. > exit
  131. exit
  132. Exiting.
  133.  
  134. String cls0;
  135. String cls1;
  136.  
  137. Scanner in = new Scanner(System.in);
  138. System.out.println("Enter a string");
  139. cls0 = in.nextLine();
  140.  
  141. System.out.println("Enter a string");
  142. cls1 = in.nextLine();
  143.  
  144. TextIO textIO = TextIoFactory.getTextIO();
  145.  
  146. String user = textIO.newStringInputReader()
  147. .withDefaultValue("admin")
  148. .read("Username");
  149.  
  150. String password = textIO.newStringInputReader()
  151. .withMinLength(6)
  152. .withInputMasking(true)
  153. .read("Password");
  154.  
  155. int age = textIO.newIntInputReader()
  156. .withMinVal(13)
  157. .read("Age");
  158.  
  159. Month month = textIO.newEnumInputReader(Month.class)
  160. .read("What month were you born in?");
  161.  
  162. textIO.getTextTerminal().println("User " + user + " is " + age + " years old, " +
  163. "was born in " + month + " and has the password " + password + ".");
Add Comment
Please, Sign In to add comment