Learn Java Tutorial for Beginners, Part 7: Getting User Input

///Learn Java Tutorial for Beginners, Part 7: Getting User Input

Learn Java Tutorial for Beginners, Part 7: Getting User Input

FavoriteLoadingAdd to favorites

Get complete courses at
A tutorial on how to get user input in your program using the Scanner class.

source

Java468x60
By |2019-05-28T19:17:08+00:00May 28th, 2019|Java Video Tutorials|40 Comments

40 Comments

  1. Shana Duffy May 28, 2019 at 7:17 pm - Reply

    Thank you so much for this! I just got a 0% on my last quiz and this is helping me more completely understand what I've learned so far. Unfortunately, we don't have any review days built into our schedule so I feel behind.

  2. JAHMAD JAMES May 28, 2019 at 7:17 pm - Reply

    7 years later and this sill still super helpful.

  3. Not Gay May 28, 2019 at 7:17 pm - Reply

    I wish all my teachers were this guy.

  4. ComandanteJ May 28, 2019 at 7:17 pm - Reply

    To control the type of data the user inputs i used input.hasNextInt() as a condition:

    import java.util.Scanner;

    public class App7 {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Write an integer: ");

    if(input.hasNextInt()) {

    int num = input.nextInt();
    System.out.println("The integer you wrote is " + num + ".");
    input.close();
    }
    else {

    System.out.println("ERROR: Input must be integer ONLY.");
    System.exit(0);
    }
    }
    }

  5. Akshay Sharma May 28, 2019 at 7:17 pm - Reply

    Hi John, how do I check if the number passed is double or not? I mean how do I verify the data type of the number so that if something else is entered, the program can recognize it

  6. Peace May 28, 2019 at 7:17 pm - Reply

    John what is wrong with this line please?
    System.out.printf("You entered : %sn" , line + " and value : %dn" , value + " and float value: %fn" , myFloat);

  7. mohibullah jalalzai May 28, 2019 at 7:17 pm - Reply

    sir why the it doesn't work ?
    import java.util.Scanner;

    public class scanerTutorial {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter your age");
    int age = input.nextInt();
    if(age < 18){
    System.out.println("you are not allowed to open this page");
    }
    {
    System.out.println("you can open this page");

    System.out.println("m/f");
    String gender = input.nextLine();
    if(gender == "f"){
    System.out.println("click next");

    }

    }

    }

    }
    the output is:

    enter your age
    30
    you can open this page
    male/fmlae

  8. mohibullah jalalzai May 28, 2019 at 7:17 pm - Reply

    very very nice tutorial sir its really helpful for me i m learning so well i love your tutorial and for all guys my message is that don't wast your time for others just join this tutorial….

  9. Arpit Dutt Dixit May 28, 2019 at 7:17 pm - Reply

    I have to write something like this plz plz help

    if I will enter a particular string then a will get a number .

    I am not able do this with if

  10. Mateusz Liziniewicz May 28, 2019 at 7:17 pm - Reply

    what to do with the message "Resource leak" "input" is never closed

  11. Senthamil V M May 28, 2019 at 7:17 pm - Reply

    You are using a variable name while using string and the datatype for others in String Line=Input.nextLine(); and for others, Int value=Input.nextInt(); Why is that so??

  12. Dara May 28, 2019 at 7:17 pm - Reply

    sigh here we go again relearning basic java for the umpteenth time

  13. Heat4Life May 28, 2019 at 7:17 pm - Reply

    You making me smart and you making me Bad-ass in School! Thank you so much!

  14. Rightist Libtard May 28, 2019 at 7:17 pm - Reply

    can you use multiple scanner variables in one System.out.print()?

  15. Di Lung Move it Ya Fool! May 28, 2019 at 7:17 pm - Reply

    public class codeagain {

    /**
    * @param args
    */
    public static void main(String[] args) {

    // TODO Auto-generated method stub
    /This is beginner code for Java./

    //Create Scanner object.
    Scanner input = new Scanner(System.in);
    //Output the prompt.
    System.out.println("Enter Hello World: ");
    //Waiting for user input
    String helloWorld = input.nextLine();
    //User input.
    System.out.println("You did it! You did! You did it, hooray! " +helloWorld);
    }

    }

    What can I do to make sure the user typed Hello World?

  16. Di Lung Move it Ya Fool! May 28, 2019 at 7:17 pm - Reply

    I have a suggestion; can you reupload videos with links to https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
    since lots have changed about Java within the last four years? It will be very helpful for new programmers.

  17. bunc11 May 28, 2019 at 7:17 pm - Reply

    Hi John! How can we protect from wrong user input… i mean if we expect int and user inputs some text….
    I tried this:

    import java.util.Scanner;

    public class App {

        public static void main(String[] args) {

            int pass = 12345;
            Scanner scanner = new Scanner(System.in);
            int input = 0;

            do {
                

                try {
                    System.out.println("Enter password: ");
                    input = scanner.nextInt();
                } catch (java.util.InputMismatchException e) {
                    System.out.println("Must enter number!");
                    
                }

            } while (pass != input);

            System.out.println("Correct pass!");
            scanner.close();

    Now if i enter e.g. Java
    program goes in indefinite loop. Btw sorry if my eng is bad 😛

  18. Tom Simpson May 28, 2019 at 7:17 pm - Reply

    Not disagreeing or anything like that, Love the videos. but could you not increment the loop in the ("Looping " + loop); bit by putting ("Looping " + loop++); and I'm just wondering. what is the point of the system.out.println("Running"); bit. is that just for looks if you know what I mean or does it have an integral purpose in the program? The tutorials are great!

  19. Giant Gosling May 28, 2019 at 7:17 pm - Reply

    You are a lifesaver… I'm so fucking happy with these tutorials. I've been wandering in and about trying to find a good tutorial vid for JAVA, and finally.. FINALLY I've found the one. Thank you so much for these. All I can say is.. You're the best, and I love you mr +Cave of Programming (no homo)

  20. AngryPotatoGamer May 28, 2019 at 7:17 pm - Reply

    Nice tutorials 😉
    Great job!

  21. Junaid Tanoli May 28, 2019 at 7:17 pm - Reply

    plz make a video for keyboard shortcuts, thanks for video bro 🙂

  22. Junaid Tanoli May 28, 2019 at 7:17 pm - Reply

    sir can you zoom your tutorials a bit , hard to read the text, thanks.

  23. meesie1 May 28, 2019 at 7:17 pm - Reply

    Are you dutch?

  24. Emer L May 28, 2019 at 7:17 pm - Reply

    I know I comment a lot but this is a real question. on my code in the first time I typed input ( even called it different things). I get a yellow warning saying a system leak that 'input' is not closed. It runs but its bugging me. Do you know why that is? Here's my code

    import java.util.Scanner;

    public class Userinput {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter your things:");

    String line = input.nextLine();

    System.out.println("That's all u can come up with ?" + line );

    }

    }
     

  25. Cave of Programming May 28, 2019 at 7:17 pm - Reply

    Sack of worthless meat! Nice. The option pane thing pops up a little dialog whereas Scanner with System.in gets input from the console, no dialog. You can take your pick really. Usually it's probably better to use the console in a console program, and only use option panes if you're writing a GUI program with a proper window and everything.

  26. NaN May 28, 2019 at 7:17 pm - Reply

    As a non native English application developer I only use English software and make sure my number settings are English as well. My date setting is actually ISO formatted (yyyy-mm-dd). Why? Because some software may not be available in non English languages and having programs mixing up languages only complicates stuff. Users should adapt, not the software. It would save a lot of time/money.

  27. Ramprasad Naik May 28, 2019 at 7:17 pm - Reply

    i can t the value of arrrays through bufferreader class for ex:-
    int [] num;
    System.out.println("Enter the values of array of 2 values");//promt for entering values by taking the values of array by input
    num[0]=Integer.parseInt(in.readLine());
    num[1]=Integer.parseInt(in.readLine());
    System.out.println("The value of the first array="+num[0]);
    System.out.println("The value of the second array="+num[1]);

  28. Fl4shbang May 28, 2019 at 7:17 pm - Reply

    ok, thanks, you solved my problem 🙂

  29. Cave of Programming May 28, 2019 at 7:17 pm - Reply

    You need to compare it to another String with .equals(). A String by itself is not true or false.

  30. Fl4shbang May 28, 2019 at 7:17 pm - Reply

    tell me something, isn't it possible to transform a string into a boolean so you can use the "if" statement?

  31. Cave of Programming May 28, 2019 at 7:17 pm - Reply

    Scanner is really much more convenient for getting user input.

  32. xaxaxaxaxaxax323 May 28, 2019 at 7:17 pm - Reply

    What would be better to use, the scanner or buffered reader class?

  33. 123japanuser May 28, 2019 at 7:17 pm - Reply

    Mucho Gracias.

    Entrada stime tomar tiempo para acostumbrarse

  34. Cave of Programming May 28, 2019 at 7:17 pm - Reply

    Make sure your file is syntactically correct (no red errors anywhere); if shortcuts still don't work, try switching your workspace to an empty folder (File -> Switch workscape). I don't know why, but often shortcuts stop working because of something saved in the workspace folder. Probably you could alternatively just delete/edit some file or other, but I haven't investigated further.

  35. adam May 28, 2019 at 7:17 pm - Reply

    I had the class name itself called Scanner I guess that messed it up the import itself wanst working, the shortcuts still don't work for me though

  36. adam May 28, 2019 at 7:17 pm - Reply

    The shortcuts for scanner arnt working in my eclipse I dont understand why I tried exactly what you did with ctrl+shift and clicking on the left of it but I had a different symbol but I had exact code you have.

  37. Chitra Indalkar May 28, 2019 at 7:17 pm - Reply

    great work , Appreciate your efforts to record this. thanks

  38. Tim Alm May 28, 2019 at 7:17 pm - Reply

    I must say you do a very good job teaching.

  39. Tony Blackett May 28, 2019 at 7:17 pm - Reply

    thanks, going to look at your other vids too

  40. hairypotty342 May 28, 2019 at 7:17 pm - Reply

    I just love these tutorials! Once I'm done, I'm gonna start scripting my game 😀

Leave A Comment

*