• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to populate array with user input

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ;
could you tell me how can i use the user input in an array of 20 element?
using scanner i tried a lot of codes but failed ..
I want it easy enough for me to understand
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you trying to do? If you posted some of your code, it would be easier to see what you're attempting.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we see any of that code you've tried? What type you're trying to store in an array? Please TellTheDetails.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are posting code that doesn't compile please state so and also supply the compiler error messages.
It won't compile because you can't use variable 'a' before declaring it.

BTW If you want to fill an array with user input values you will need a loop to repeatedly get the user's input and put the value into the array.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:If you are posting code that doesn't compile please state so and also supply the compiler error messages.
It won't compile because you can't use variable 'a' before declaring it.

BTW If you want to fill an array with user input values you will need a loop to repeatedly get the user's input and put the value into the array.


i can't understand you
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I declare it s a = array[i]
and the compiler said i is not found and a too :\
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you posted is not valid Java, it doesn't compile.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I declare it s a = array[i]


No, that's not declaring it that's using it. Declaring it is
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually why are you doing this anyway. The array is empty so why are you putting the value at index i into variable a.
You can get rid of that line and you need to add some curly braces so your for loop repeats more than just the following line.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't understand you could you right the code to catch the idea .. i did make a loop and the BIG idea i want to enter the the numbers in the array by myself :\
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that but i tried some stranges ideas to get what i want
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:The code you posted is not valid Java, it doesn't compile.


it's because the import statment i skipped it i think ..anyway not my main idea now
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand the difference between these two code examples:


 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Do you understand the difference between these two code examples:



the first withoutbraces and the second with braces and the second all the code is inside the loop
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so look at your code and decide what code is being executed in the for loop and then add braces so the right lines of code are executed in the for loop.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Tony ,,now look i wrote this could ok!
and when i excute it the numbers of the index is not on the same line of value..
plus , the numbers or items of the array is written by the computer i want by the me (by the keyboard);
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and when i excute it the numbers of the index is not on the same line of value..


That's because you are telling it to add a newline character after each output of 'i'.

plus , the numbers or items of the array is written by the computer i want by the me (by the keyboard);


I'm not going to solve all your problems for you. Look at your code and ask yourself why isn't it letting you to input a value.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abz,

I would like you to write the logic of what you are trying to do in simple English (or whatever communication language you prefer).

Once you are crystal clear about every step, you can start writing the code.

Abz Aka wrote: plus , the numbers or items of the array is written by the computer i want by the me (by the keyboard);


Well, that is simply because there's no code which accepts an input from user. Just look carefully at your first version of code and latest version of code.

Further, IMHO, accepting 100 numbers from user is not a good idea. How about making sure that your code works for small input (e.g. accepting 3 or 5 numbers from user)?
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for making you busy tony i swear i tried
i'll try again
thanks all
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first code was nearly correct, I supplied the two for loop code blocks so you could see the difference curly braces made as you didn't appear to understand my instructions about what you needed to do to your code. They were not for you to copy into your code they were just to show how adding curly braces changed what code was executed inside the loop.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Your first code was nearly correct, I supplied the two for loop code blocks so you could see the difference curly braces made as you didn't appear to understand my instructions about what you needed to do to your code. They were not for you to copy into your code they were just to show how adding curly braces changed what code was executed inside the loop.


i'm not that stupid to copy :\ ..it's just that i insist to enter the data by (((scanner))) and all my trys are wrong ,,there is some solutions on Google but it's advanced and new to me ..that's all
now Do can i make with scanner or i need to use ((arraylist,arrayread,and these things))))to add items??
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abz Aka wrote:now Do can i make with scanner or i need to use ((arraylist,arrayread,and these things))))to add items??


Well, I would urge you to check out Java documentation for Scanner class. Creating a Scanner object is one thing, and making it accept inputs from user(keyboard) is another thing.

Also, please make sure to have your algorithm ready - to avoid logical errors.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've told you your first code was nearly correct, and apart from the compile errors it wasn't doing the keyboard input in the for loop.
So look at your original code and take the line that used the Scanner to read in the int value and put it into your new code's for loop.
Next put that value you have read in into the array.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about you try to declare and populate an array of integers without getting any input from the user? Say you want to create an array where each element contains the value of its index:

Try with that first, forget the user input. If you can't have it done, then you should go back to Java basics and go through it again. Clearly you've lost some concepts, since the directions Tony gave you are very detailed and straightforward. Let's try small steps now.
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understand what you wrote ..it's maybe i'm little complicated and i'm not english girl
ok..i'll read the lab again
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abz Aka wrote:.it's just that i insist to enter the data by (((scanner))) and all my trys are wrong ,,there is some solutions on Google but it's advanced and new to me ..that's all
now Do can i make with scanner or i need to use ((arraylist,arrayread,and these things))))to add items??



It sounds like you're saying, "I want to populate an array using Scanner."

That is absolutely the wrong way to approach this problem. Scanner is not for populating arrays. It is for retrieving, tokenizing, and parsing user input.

You need to be thinking about two completely independent steps (which might end up being broken down into even smaller steps as you proceed).

1. Get user input.

2. Populate array.

You can work on either one of them without giving a thought to the other.

Write a tiny program that does nothing but populate an array. Just start with hardcoded values, and no looping. Directly set the values of 3 elements in sequential statements. After you have populated the array, print its contents, so that you can see that you did it correctly. Don't even think about Scanner.

Write a separate tiny program that does nothing but read from a Scanner. Have it read 3 values into 3 variables. No looping. No arrays. Just read using the Scanner and print out the results. Don't even think about arrays.

Next add loops to both programs.

Once both of these programs work perfectly, and you understand how they work and why they behave the way they do, then and only then should you combine the concepts into "Read using a Scanner and put the results into an array."
 
Abeer El-shaer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output
--------------------Configuration: <Default>--------------------
Enter the element
value index
04
13
23
35
48
23
Process complete
i MAKE it )))))
now why {4,3,3,5,8} is not under the word value
DON'T TELL ME TRY BECAUSE MY EYES HURTING ME DUE TO STUDYING >>PLEASE JUST GIVE ME THR CORRECTION ((
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic