How Do We Get User Input in Java? - Full Tutorial

Поділитися
Вставка
  • Опубліковано 13 жов 2024
  • Complete Java course: codingwithjohn...
    All about using the Scanner class in Java to get user keyboard input, with examples for a String, int, and float.
    Watch this Java lesson to learn how to use the built-in Scanner class to retrieve input from the user!
    Learn or improve your Java programming by watching it being coded live in tutorials!
    Hey, I'm John! I'm a Lead Java Software Engineer who has been in the industry for over a decade, and I love teaching what I know.
    Tell me what else you want to see!
    Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
    📕 THE best book to learn Java, Effective Java by Joshua Bloch
    amzn.to/36AfdUu
    📕 One of my favorite programming books, Clean Code by Robert Martin
    amzn.to/3GTPVhf
    🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
    www.audibletria...
    🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
    bit.ly/3QPNGko
    📹Phone I use for recording:
    amzn.to/3HepYJu
    🎙️Microphone I use (classy, I know):
    amzn.to/3AYGdbz
    Donate with PayPal (Thank you so much!)
    www.paypal.com...
    ☕Complete Java course:
    codingwithjohn...
    codingwithjohn...

КОМЕНТАРІ • 58

  • @thaimilamonegabolander8338
    @thaimilamonegabolander8338 Рік тому +2

    Thanx so much John! I'm on my third week for Java dev. Yesterday I cried in class, and yelled; I just don't understand anything! Today I'm back up on the horse. My teacher guided me to your chanel. XOXO thai

  • @ruantristancarlinsky3851
    @ruantristancarlinsky3851 2 роки тому +8

    You are so amazing, Please continue making more videos on Java. Will be greatly appreciated!

  • @aliyyah.u
    @aliyyah.u Рік тому +1

    Super helpful and easy to understand!

  • @AlexKelly-mk8py
    @AlexKelly-mk8py Місяць тому

    Ty so much this helped me print some basic code.

  • @raphaelferrazza4412
    @raphaelferrazza4412 2 роки тому

    Im surprised so many people make videos or go on forums despite how wrong they are most of the time! its really amazing

  • @etipeti555
    @etipeti555 6 місяців тому

    In chapter 8 I was taught something called casting (not working with the scanner class). Are both what I mentioned and what you tought us equaly valid or is there a difference?

  • @kristinaz7046
    @kristinaz7046 2 роки тому +1

    Cheese sanwidch is not garbage.
    Video is very helpful!

  • @tushartomar3586
    @tushartomar3586 2 роки тому

    buddy you're doing a great job , i really want to thankyou for this, could you plz make a video about oops but in advance way, but each of the four concept should be explained in the separate video

  • @higiniofuentes2551
    @higiniofuentes2551 7 місяців тому

    Thank you for this very useful video!

  • @theconsul8452
    @theconsul8452 2 роки тому +2

    At 6:36 , how did you delete the whole line so quickly without selecting the characters ? What is that keyboard shortcut ?

    • @CodingWithJohn
      @CodingWithJohn  2 роки тому +5

      Control + D, works in many programs. One of my most used shortcuts for sure!

  • @Arfifias
    @Arfifias Рік тому +1

    I would like smaller and easier tutorials like constructor tutorial you made that was pretty good

  • @vic17.
    @vic17. Рік тому +1

    i have a question at around 11:05, I saw you assigned floatAge the value inside of the try block, but declared it outside of it. Is this necessary, or could you declare floatAge and initialize both inside of the try block?

    • @piikayy7511
      @piikayy7511 Рік тому +2

      If you declare and initialize the floatAge variable inside of the try block you will only be able to use it inside(!) of the try block, as it only exists within that block.
      So if you are looking to, for example print out the value of floatAge later on (like John did in the video), you will have to declare and initialize it outside of the try block.

    • @vic17.
      @vic17. Рік тому

      @@piikayy7511 oh ok that makes sense thank you

  • @Nelfa183
    @Nelfa183 Рік тому

    Wao you made it so simple . Thanks 🙌

  • @ensarthewicked
    @ensarthewicked 6 місяців тому

    love you john, I really do

  • @chandulasamarasinghe3088
    @chandulasamarasinghe3088 Рік тому

    love your videos

  • @ibnearabi3640
    @ibnearabi3640 2 роки тому

    Great explanation. Thank you.

  • @szaronaangel7725
    @szaronaangel7725 2 роки тому

    It's a veeery good tutorial, thank you!

  • @briankwala7923
    @briankwala7923 Рік тому

    great job sir!

  • @mithunbiswas5307
    @mithunbiswas5307 2 роки тому

    Great explanation, thanks

  • @dypsking3414
    @dypsking3414 2 роки тому

    Love all your videos...
    You're truly awesome 👍💪
    Please could you do a video on the different types of ways to get user inputs?
    Be keeping an eye out for this
    ✊✊

    • @saarza9991
      @saarza9991 7 місяців тому

      Scanner and BufferedReader

  • @johnnyjohn6645
    @johnnyjohn6645 2 роки тому

    thanks a lot John !

  • @Pacvalham
    @Pacvalham 2 роки тому

    I can't make a cheese sandwich into a float, but I can make ice cream into a float by pouring soda on it.

  • @harshvaviya1467
    @harshvaviya1467 2 місяці тому

    great video

  • @jakesegal
    @jakesegal 2 роки тому

    Johns the best!

  • @Ahmadzai-15
    @Ahmadzai-15 Рік тому

    Great method how to teach
    Respect from Afghanistan 🇦🇫…

  • @vaibhavhanegave256
    @vaibhavhanegave256 2 роки тому

    nice and helpfull thank you bro.

  • @anyahayes3601
    @anyahayes3601 2 роки тому

    Can you explain why you can’t reopen a Scanner after you call close?

  • @javidaghayarli8252
    @javidaghayarli8252 2 роки тому

    Thanks boss 👍

  • @SPXCVIII
    @SPXCVIII 2 роки тому

    Thank You

  • @ntphiep
    @ntphiep 2 роки тому

    Great video , but can I have a question ? Why my scanner nextFloat read float from keyboard with comma instead of dot ? , can you help me . Thanks

  • @unbelievable699
    @unbelievable699 3 роки тому +1

    thaanks alot

  • @thariqdeyis140
    @thariqdeyis140 Рік тому

    How to make the scanner inputs are beside the question??

  • @kebroonabebayehu2559
    @kebroonabebayehu2559 2 роки тому

    You're awesome!

  • @Mrexecexec
    @Mrexecexec 2 роки тому

    Hi John. I have a strange issue. I expect code below to allow me to enter 3 string elements from keyboard but unfortunately after entering two strings, immediately I see the output of myStrings array which looks odd: [FirstString, ,SecondString]. There is an empty element between myStrings[0] and myStrings[2]? Why? It is expecting that each scanner.nextLine() will consume the new line including
    ; but the situation is not as such?
    import java.util.Arrays;
    import java.util.Scanner;
    public class Main {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
    String[] myStrings = getStrings(3);
    System.out.println(Arrays.toString(myStrings));
    }
    public static String[] getStrings(int number){
    System.out.println("Enter "+number+" strings
    ");
    String[] values = new String[number];
    for (int i=0;i< values.length;i++){
    values[i]=scanner.nextLine();
    }
    return values;
    }
    }

    • @Mrexecexec
      @Mrexecexec 2 роки тому

      My real output is "C:\Program Files\Amazon Corretto\jdk11.0.15_9\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.1\lib\idea_rt.jar=49409:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\ARCHITECT\IdeaProjects\ArrayVsArrayList\out\production\ArrayVsArrayList Main
      Enter 3 strings.
      First
      Second
      [First, , Second]
      Process finished with exit code 0

    • @Mrexecexec
      @Mrexecexec 2 роки тому

      Another code peace which is odd too. The problem is with scanner.nextLine() but why?
      import java.util.Arrays;
      import java.util.Scanner;
      public class Main {
      private static Scanner scanner = new Scanner(System.in);
      public static void main(String[] args) {
      String[] myStrings = new String[3];
      myStrings[0]=scanner.nextLine();
      myStrings[1]=scanner.nextLine();
      myStrings[2]=scanner.nextLine();
      System.out.println("End entering. But problem is still there");
      System.out.println(Arrays.asList(myStrings));
      }
      }

  • @jeremyunger9806
    @jeremyunger9806 8 місяців тому

    So we're just ignoring his shoe size? (Great video)

  • @mhdmk8245
    @mhdmk8245 2 роки тому

    Ty sir

  • @ndk8111
    @ndk8111 2 роки тому

    13:38. Is this bad practise to evaluate booleans as booleanName == false
    How about (!booleanName) in your example.

  • @inoobbeyt1031
    @inoobbeyt1031 Рік тому

    What happened in while condition i didn't get

  • @Pacvalham
    @Pacvalham 2 роки тому +1

    I prefer a bang instead of "== false".

  • @hackcracker7691
    @hackcracker7691 2 роки тому

    Jhonny sins 😂😂 brother im joking

  • @murtajiz545
    @murtajiz545 2 роки тому

    Thanks for the video mate but are we gonna ignore this man having a size 14 foot? 💀

  • @bloomin1819
    @bloomin1819 2 роки тому

    With that shoe size, you ever play any basketball??

  • @eternalflames911
    @eternalflames911 Рік тому

    But i dont wanna be a good boy scout...im a girl

  • @kIllera911
    @kIllera911 2 роки тому

    Java is really verbose