• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with array  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Im new here and need help with array, I have to shuffle different numbers, and then store the numbers are between 0 and 499 in a array and those between 500-999 in another vector, I have come so far

the numbers randomly selected how can I put them in a array?

thanks for help
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:
the numbers randomly selected how can I put them in a array?


You're already putting numbers into an array. I'm not sure where your trouble is.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are in an array. What is your question again?
 
milad yalda
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:They are in an array. What is your question again?

I have random numbers and then put them in another array and sort the numbers in excess of 499 put them in a array and those less than 499 in a different vector and print out.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:
Ralph Cook wrote:They are in an array. What is your question again?

I have random numbers and then put them in another array and sort the numbers in excess of 499 put them in a array and those less than 499 in a different vector and print out.

Well, you've got past step 1, so what do you think you need to do next? The policy here is to ShowSomeEffort and come back if you have problems.

BTW, since I presume that 'tal' is the number of integers you're inputting, you might as well just put
int[] nummber = new int[tal];

Winston
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note I don't know your exact requirement so below code may be help to find your required solution:

(Full solution deleted)
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjay. Can you avoid posting full solutions to problems, please? Especially in the Beginning Java forum. We think it's more effective to help beginners learn by finding their own solution (with guidance where needed) rather than handing out a solution.

Thanks!
 
sanjay kumar gupta
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure Matthew,
 
milad yalda
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you enter 4, the program will print 4 random numbers so my problem is how can I add the 4 random numbers in an new array? I do not want the whole solution
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:When you enter 4, the program will print 4 random numbers so my problem is how can I add the 4 random numbers in an new array? I do not want the whole solution

I'm still confused. You know how to create an array - you do it on line 9. You know how to add a number to an array - you do it on line 15. So what exactly are you having a problem with ?
 
milad yalda
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
milad yalda wrote:When you enter 4, the program will print 4 random numbers so my problem is how can I add the 4 random numbers in an new array? I do not want the whole solution

I'm still confused. You know how to create an array - you do it on line 9. You know how to add a number to an array - you do it on line 15. So what exactly are you having a problem with ?


I have to sort those, the numbers that are less than 499 and the nummber that are more than 500


now I've solved half, the problem is that I get lot of zeroes when I run why?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:now I've solved half, the problem is that I get lot of zeroes when I run why?

Because you haven't taken the advice in my first post (see above).

BTW, strictly speaking, what you're doing is not a sort; it's a filter.

Winston
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read through this thread twice, and I still don't understand what it is you are trying to do. Can you give a complete explanation of your program?
 
milad yalda
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
milad yalda wrote:now I've solved half, the problem is that I get lot of zeroes when I run why?

Because you haven't taken the advice in my first post (see above).

BTW, strictly speaking, what you're doing is not a sort; it's a filter.


Winston


hi wiston, when i put "int[] nummber =new int[tal];" så i get this message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Vek.main(Vek.java:18)"
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:hi wiston, when i put "int[] nummber =new int[tal];" så i get this message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Vek.main(Vek.java:18)"

Because your for loop is still wrong.

If 'tal' is 3 and you create an array of 3 numbers with
double[] v = new double[tal];
what is the largest valid index for 'v'?

Winston
 
milad yalda
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
milad yalda wrote:hi wiston, when i put "int[] nummber =new int[tal];" så i get this message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Vek.main(Vek.java:18)"

Because your for loop is still wrong.

If 'tal' is 3 and you create an array of 3 numbers with
double[] v = new double[tal];
what is the largest valid index for 'v'?

Winston


for(int i=0; i<=v.length; i++) so i have change the loop but i still get the message !!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad yalda wrote:
Winston Gutkowski wrote:
milad yalda wrote:hi wiston, when i put "int[] nummber =new int[tal];" så i get this message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Vek.main(Vek.java:18)"

Because your for loop is still wrong.

If 'tal' is 3 and you create an array of 3 numbers with
double[] v = new double[tal];
what is the largest valid index for 'v'?

Winston


for(int i=0; i<=v.length; i++) so i have change the loop but i still get the message !!!



If v.length is 3, then v's indices are 0, 1, 2. That is, for any array, A, the valid indices for A are 0..A.length-1. The canonical way to iterate over an array is


Or, if you don't need to actually use the index, just the elements (and you're not changing the array), then you can use a foreach loop
 
Greenhorn
Posts: 10
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milad,

You only created one array.

You need to create two arrays:
1. for ints between 0 and 499
2. for ints between 500 and 999

You have:
int[] nummber =new int[1000];

Change to:
int[] number0and499 = new int[1000];
int[] number500and999 = new int[1000];

Now inside your for loop:

Change the code so that your random number gets stored inside an int variable.
Then write an if statement that decides which of the two arrays the random number goes to.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!