Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Console co=System.console();
- System.out.println(co);
- try{
- String s=co.readLine();
- }
- System.out.print("Enter something:");
- String input = System.console().readLine();
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Test {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Enter String");
- String s = br.readLine();
- System.out.print("Enter Integer:");
- try{
- int i = Integer.parseInt(br.readLine());
- }catch(NumberFormatException nfe){
- System.err.println("Invalid Format!");
- }
- }
- }
- Scanner in = new Scanner(System.in);
- int i = in.nextInt();
- String s = in.next();
- public class ConsoleReadingDemo {
- public static void main(String[] args) {
- // ====
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Please enter user name : ");
- String username = null;
- try {
- username = reader.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("You entered : " + username);
- // ===== In Java 5, Java.util,Scanner is used for this purpose.
- Scanner in = new Scanner(System.in);
- System.out.print("Please enter user name : ");
- username = in.nextLine();
- System.out.println("You entered : " + username);
- // ====== Java 6
- Console console = System.console();
- username = console.readLine("Please enter user name : ");
- System.out.println("You entered : " + username);
- }
- }
- import java.io.Console;
- public class Test {
- public static void main(String[] args) throws Exception {
- Console console = System.console();
- if (console == null) {
- System.out.println("Unable to fetch console");
- return;
- }
- String line = console.readLine();
- console.printf("I saw this line: %s", line);
- }
- }
- > javac Test.java
- > java Test
- Foo <---- entered by the user
- I saw this line: Foo <---- program output
- import java.util.Scanner;
- String data;
- Scanner scanInput = new Scanner(System.in);
- data= scanInput.nextLine();
- scanInput.close();
- System.out.println(data);
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class LoopingConsoleInputExample {
- public static final String EXIT_COMMAND = "exit";
- public static void main(final String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Enter some text, or '" + EXIT_COMMAND + "' to quit");
- while (true) {
- System.out.print("> ");
- String input = br.readLine();
- System.out.println(input);
- if (input.length() == EXIT_COMMAND.length() && input.toLowerCase().equals(EXIT_COMMAND)) {
- System.out.println("Exiting.");
- return;
- }
- System.out.println("...response goes here...");
- }
- }
- }
- Enter some text, or 'exit' to quit
- > one
- one
- ...response goes here...
- > two
- two
- ...response goes here...
- > three
- three
- ...response goes here...
- > exit
- exit
- Exiting.
- String cls0;
- String cls1;
- Scanner in = new Scanner(System.in);
- System.out.println("Enter a string");
- cls0 = in.nextLine();
- System.out.println("Enter a string");
- cls1 = in.nextLine();
- TextIO textIO = TextIoFactory.getTextIO();
- String user = textIO.newStringInputReader()
- .withDefaultValue("admin")
- .read("Username");
- String password = textIO.newStringInputReader()
- .withMinLength(6)
- .withInputMasking(true)
- .read("Password");
- int age = textIO.newIntInputReader()
- .withMinVal(13)
- .read("Age");
- Month month = textIO.newEnumInputReader(Month.class)
- .read("What month were you born in?");
- textIO.getTextTerminal().println("User " + user + " is " + age + " years old, " +
- "was born in " + month + " and has the password " + password + ".");
Add Comment
Please, Sign In to add comment