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...
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
You are so amazing, Please continue making more videos on Java. Will be greatly appreciated!
Super helpful and easy to understand!
Ty so much this helped me print some basic code.
Im surprised so many people make videos or go on forums despite how wrong they are most of the time! its really amazing
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?
Cheese sanwidch is not garbage.
Video is very helpful!
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
Thank you for this very useful video!
At 6:36 , how did you delete the whole line so quickly without selecting the characters ? What is that keyboard shortcut ?
Control + D, works in many programs. One of my most used shortcuts for sure!
I would like smaller and easier tutorials like constructor tutorial you made that was pretty good
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?
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.
@@piikayy7511 oh ok that makes sense thank you
Wao you made it so simple . Thanks 🙌
love you john, I really do
love your videos
Great explanation. Thank you.
It's a veeery good tutorial, thank you!
great job sir!
Great explanation, thanks
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
✊✊
Scanner and BufferedReader
thanks a lot John !
I can't make a cheese sandwich into a float, but I can make ice cream into a float by pouring soda on it.
Put the sandwich in some soda
great video
Johns the best!
Great method how to teach
Respect from Afghanistan 🇦🇫…
nice and helpfull thank you bro.
Can you explain why you can’t reopen a Scanner after you call close?
Thanks boss 👍
Thank You
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
Your scanner is high
depends on your windows keyboard language probably
thaanks alot
How to make the scanner inputs are beside the question??
You're awesome!
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;
}
}
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
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));
}
}
So we're just ignoring his shoe size? (Great video)
Ty sir
13:38. Is this bad practise to evaluate booleans as booleanName == false
How about (!booleanName) in your example.
What happened in while condition i didn't get
I prefer a bang instead of "== false".
Jhonny sins 😂😂 brother im joking
Thanks for the video mate but are we gonna ignore this man having a size 14 foot? 💀
With that shoe size, you ever play any basketball??
Not since 8th grade or so 🫤
But i dont wanna be a good boy scout...im a girl
Java is really verbose