Skip to content

Instantly share code, notes, and snippets.

@cassiusvm
Last active April 23, 2021 18:22
Show Gist options
  • Select an option

  • Save cassiusvm/9879f7021812bd25fc5cbf93a0988d03 to your computer and use it in GitHub Desktop.

Select an option

Save cassiusvm/9879f7021812bd25fc5cbf93a0988d03 to your computer and use it in GitHub Desktop.

Revisions

  1. cassiusvm revised this gist Sep 16, 2018. 1 changed file with 40 additions and 20 deletions.
    60 changes: 40 additions & 20 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,45 +1,65 @@
    import java.io.Console;
    import java.util.Scanner;

    public class App {
    public void perform() {
    try (Scanner sc = new Scanner(System.in)) {

    firstScanner(sc);
    firstScanner();

    secondScanner(sc);
    secondScanner();

    } catch (Exception e) {
    System.out.println("Exception message: " + e.getMessage());
    }
    }

    public void firstScanner(Scanner sc) {
    public void firstScanner() {

    String str = "";

    System.out.println("Enter a string: ");
    Console con = System.console();
    if (con != null) {

    if (sc.hasNext()) {
    str = sc.next();
    sc.nextLine();
    }
    try (Scanner sc = new Scanner(con.reader())) {

    System.out.println("Enter a string: ");

    System.out.println("str = " + str);
    if (sc.hasNext()) {
    str = sc.next();
    sc.nextLine();
    }

    System.out.println("str = " + str);

    } catch (Exception e) {
    System.out.println("Exception message: " + e.getMessage());
    }
    } else {
    System.out.println("System console not available");
    }
    }

    public void secondScanner(Scanner sc) {
    public void secondScanner() {

    int num = 0;

    System.out.println("Enter a number: ");
    Console con = System.console();
    if (con != null) {

    if (sc.hasNextInt()) {
    num = sc.nextInt();
    sc.nextLine();
    }
    try (Scanner sc = new Scanner(con.reader())) {

    System.out.println("Enter a number: ");

    if (sc.hasNextInt()) {
    num = sc.nextInt();
    sc.nextLine();
    }

    System.out.println("num = " + num);
    System.out.println("num = " + num);

    } catch (Exception e) {
    System.out.println("Exception message: " + e.getMessage());
    }
    } else {
    System.out.println("System console not available");
    }
    }

    public static void main(String[] args) {
  2. cassiusvm revised this gist Sep 16, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,7 @@ public class App {

    if (sc.hasNextInt()) {
    num = sc.nextInt();
    sc.nextLine();
    }

    System.out.println("num = " + num);
  3. cassiusvm created this gist Sep 16, 2018.
    51 changes: 51 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import java.util.Scanner;

    public class App {
    public void perform() {
    try (Scanner sc = new Scanner(System.in)) {

    firstScanner(sc);

    secondScanner(sc);

    } catch (Exception e) {
    System.out.println("Exception message: " + e.getMessage());
    }
    }

    public void firstScanner(Scanner sc) {

    String str = "";

    System.out.println("Enter a string: ");

    if (sc.hasNext()) {
    str = sc.next();
    sc.nextLine();
    }

    System.out.println("str = " + str);

    }

    public void secondScanner(Scanner sc) {

    int num = 0;

    System.out.println("Enter a number: ");

    if (sc.hasNextInt()) {
    num = sc.nextInt();
    }

    System.out.println("num = " + num);
    }

    public static void main(String[] args) {
    App app = new App();

    app.perform();

    System.exit(0);
    }
    }