Back Array User Input using For Loop | Java Programming Tutorial

5-10
Created By: LearningLad
Published At: 2014-07-08
Search Query: Java User Input
Tags: array user input array java Array Data Structure arrays java array programs java arrays examples Java (Programming Language) java programming video tutorial java programming java video tutorials for beginners
In this video tutorial for beginners you will learn how to read / receive user input for the array in java programming language with example.

You will learn how to create an array, how to read input for the all array elements from user, how to display all the all elements using a loop in detail with example.

Get the source code of this tutorial at our website
http://www.LearningLad.com

Watch Video tutorials in HINDI at
http://www.youtube.com/user/LearningLadHindi

Catch us on SocialMedia
facebook http://www.facebook.com/LearningLad
twitter http://www.twitter.com/LearningLadEdu
Google+ http://www.google.com/+LearningLad
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@usamaabrar22
2017-03-12

Good job, what makes me subscribe your channel, is your simplicity.
Suggestion: It would be great if you provide the direct link of your source code, exploring it on your website is kinda time taking.
Thanks.

@LearningLad

Thank you.
for the future video's i will consider your suggestion about source code.

VidTutorAssistant (GPT-4)

Hi @usamaabrar22, thank you so much for your kind words and the suggestion. It's wonderful to know that the simplicity of our content has helped you in some way. As per your suggestion, we will definitely consider providing direct links to the source code in our future videos. We always appreciate user feedback to improve the viewer experience at LearningLad channel. Keep coding and happy learning!


@Manojkumar-hl2xg
2015-12-30

output:
enter the number friends you gonna enter
2
enter you friends name
enter your friend name 1
manoj kumar
enter your friend name 2
your friends names are
manoj
kumar


this is what happening when we give space between the name ,it is not allowing to write second friend name if we give space between the name. how to prevent this problem

@LearningLad

+manoj kumar
use
input.nextLine();
instead of
input.next();

you will be able to read string values with space :)

VidTutorAssistant (GPT-4)

Hello @Manojkumar-hl2xg, I understand your issue. The problem is due to the 'next()' method of the Scanner class. It only reads the input until it finds a space. If you want to read the full name with spaces, you should use the 'nextLine()' method instead of 'next()'. Just replace 'input.next()' with 'input.nextLine()' in the given code and that should solve the issue. Hope this helps! Be sure to keep coding and learning.


@durgeshmishra5902
2015-07-31

what if i want my user to directly enter names without specyfying number of names????

@LearningLad

Durgesh Mishra
you can use ArrayList.
just a bit advanced topic considering this video series.

VidTutorAssistant (GPT-4)

Hey @durgeshmishra5902! Good question. If you want your user to enter names without specifying the number of names beforehand, you could consider utilizing an ArrayList instead of an array. ArrayLists in Java are dynamic, which means they can grow in size as needed when you add elements to them. You can prompt your user to enter names until they decide to quit (for example, by entering a specific keyword like "done" or "exit"). You'd use a loop that repeatedly asks for input until this keyword is received. Hope this helps, and happy coding! Don't forget to subscribe for more Java tips and tutorials!


@napoleonb55
2017-01-23

Would like to ask is there a significant difference between input.next() and input.nextLine() for strings? Thanks anyone !

@LearningLad

with next() you cant read a string value with space and with nextline() you can.

VidTutorAssistant (GPT-4)

Hi @napoleonb55! That's a great question. Yes, there is a difference between input.next() and input.nextLine() when reading strings in Java.

The method input.next() reads the input until it finds a space, that's why it can only read single words. It's unable to read two words separated by space.

On the other hand, input.nextLine() reads the entire line of string input. It reads input including space until the end of line.

That's why in our tutorial where we're reading names, we used input.next(). If we had used input.nextLine() and a user entered their first and last name, it would have taken it as separate inputs.

I hope this helps. Like and subscribe for more tutorials like this!


@ritikapandit3647
2017-12-01

in array we have to always run the loop for the simple program also???

@LearningLad

you don't have to use the loop with arrays.
but here in all these tutorials we need to display the array elements.
so we can do that easily using the for loop.

VidTutorAssistant (GPT-4)

Great question, @ritikapandit3647! Despite its frequent use, looping is not always necessary while working with arrays in Java. Arrays are highly flexible, you can access any element directly if you know its index. However, when we want to perform operations on all elements of the array or we're uncertain of the exact index we're targeting, loops come in handy. In the tutorial, we used the loop to interact with all array elements. For simpler programs where you are directly accessing individual elements using their indices, you may not need a loop. Hope this clarifies! Stay curious and happy learning!


@jomabaste3924
2021-04-15

what if you need just 5 numbers and dont need to ask how many friends name that you are gonna enter?

@LearningLad

Instead of taking user input assign that variable with the value of 5

VidTutorAssistant (GPT-4)

Hello @jomabaste3924, good question! If you are certain you only need to input 5 numbers, you don't need to ask the user for the count. All you need to do is initiate an array with a fixed size of 5. Then, loop 5 times to input each number using the `Scanner` class, just like we did in the video. For example, you would do something like `int[] myArray = new int[5];` to create the array. Then, you would loop over this array using a `for` loop, and use the scanner input method to read the values into the array one by one. I hope this helps! Keep learning, and let me know if there are any other questions.


@ahtashamfiaz3982
2020-03-28

Bro you are amazing you resolve my problem this confusion always disturbe me whenever i am in washroom i just think about that how how i can enter the info by user in array and this thing always put me in trouble you are hero you really resolve my problem you rock !!!!!!


Thanks ....man
LOVE FORM PAKISTAN .

@LearningLad

Thank you.
i'm glad that my tutorials are helping you :)

VidTutorAssistant (GPT-4)

Thank you so much @ahtashamfiaz3982! I'm delighted to hear that you found the tutorial useful and it helped resolve your confusion about user input in arrays. That's why I'm here - to make programming easier and more accessible for everyone, anywhere! Happy coding and don't hesitate to reach out if you have any more questions. Love from LearningLad to Pakistan! Don't forget to subscribe and join us for more learning, let's keep coding!


@ozzDeveveloperOpenForWork
2019-03-19

if you dont make it plus 1 after counter and make counter in the forloop equal to 1, u will get enter name of friend 1

@LearningLad

but array indexing starts from 0
so while storing value in the array you have to make counter -1

VidTutorAssistant (GPT-4)

Hi @ozzDeveveloperOpenForWork! Thanks for your insightful comment. Indeed, if we initialize counter in the loop with 1, we will prompt the user with "enter name of friend 1" from the get-go. However, remember that arrays in Java are 0-indexed. If we start labeling our friends from index 1, our program won't take the input for the friend with index 0 and the last friend will be out of the bounds of our array. That's why I initialized the counter with 0. Always remember to align your initialization with the array's indexing while programming. Stay connected with LearningLad for more interesting facts and learning about programming!


@studytips423
2018-06-30

Thanks a lot. I stuck with a problem for a whole night. i was almost being crazy.
i search a lots. and after the whole night surfing on internet god send you to me for help me. i am very grateful to you. May god give you a long life and energy to help people.

@LearningLad

thank you.
i'm really glad that my video's are helping you.
keep learning :)

VidTutorAssistant (GPT-4)

Hi @studytips423! I'm so glad to hear that the tutorial was helpful to you and relieved your stress from the night before. That's precisely why these tutorials exist: to break down complex topics and provide solutions. Your gratitude warms my heart and further motivates me to keep teaching. The learning process can be challenging, but remember struggling is a natural part of it. I'm here to guide you, so never hesitate to reach out if you encounter any more problems in your coding journey. Keep learning and happy coding! Stay tuned for more videos.


@CashmereMercenary
2018-11-09

This was a very helpful tutorial, i wish i found it earlier. i had to figure this out through trial and error. Great job explaining it all step by step.

@LearningLad

thank you.
since you have done the trial and error, you have learnt more things right?

VidTutorAssistant (GPT-4)

Hi @CashmereMercenary, I'm really glad you found the tutorial helpful! Sometimes, trial and error is the best way to learn, but I'm happy that my step by step explanation could make things clearer for you. Keep tuning in for more Java programming tutorials. Thank you for your kind words and for being a part of our learning community!