# Thread: Import data to an Array

1. Member
Join Date
Dec 2009
Posts
11
Rep Power
0

## Import data to an Array

Hello everyone. Thank you in advance for your time. I want this program to generate user defined amount of random number between 0-999 and then sort them in ascending order. It works properly without the bubble sort, but when i add the bubble sort it gives me a ridiculous output. like below

Output

Please Enter the amount of random numbers you want to generate : 2

229
Sorted array
229
891
Sorted array
891

But i need it as follows

Please Enter the amount of random numbers you want to generate : 3

229
45
251

Sorted array

45
229
251

My code so far

Java Code:

package random;

import java.util.Random;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner (System.in);
System.out.print("Please Enter the amount of random numbers you want to generate : ");

Random dice = new Random();
int number;

int input = in.nextInt();
for(int counter = 1; counter<=input; counter++){
number = dice.nextInt(999); // Randomly generated values between 0-999

{
int nums [] = {number}; // Passing randomly generated values to array

int a,b,t;// Integers for bubble sorting

int size = nums.length;

for(int i=0; i< size; i++)
System.out.println(nums[i]);// Display original array

// Sorting
for (a=1; a<size; a++)
for(b=size-1; b>=a;b--){
if(nums[b-1]> nums[b]){
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;

}
}
//Display sorted array

System.out.println("Sorted array");
for(int i =0; i<size; i++)
System.out.print(""+ nums[i]);
System.out.println();
}
}
}

}

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
8
When you begin your for loop, the statement int[] array = {number} is what's giving you problems. Basiccaly, what the program is doing, is defining a new array in every iteration of the for loop. Something like this would probably be more suited:
Java Code:
int[] array = new int[10];
for(int i = 0; i < array.length; i++) {
int number = Math.random(//some condition);
array[i] = number;
}
And another problem, you want to fill the array and sort it in the same loop, first fill the array, and then sort it in another loop.

3. Member
Join Date
Dec 2009
Posts
11
Rep Power
0
Yes, but could you please tell me which part exactly need to be changed? Sorry i really didn't get it. and if you can provide the code as well.

Thanks
Last edited by yrollgayanth; 03-31-2010 at 06:02 PM.

4. What you exactly want to do? If I'm correct you want to sort numbers which are entered by the user. So what's the requirement to generate a random number?

5. Member
Join Date
Dec 2009
Posts
11
Rep Power
0
I want this program to generate user defined amount of random numbers between 0-999 and display them. After that display them again in ascending order. This program already generates random numbers, say if i enter 3 it will generate 3 random numbers between 0-999 ,but not in any order. I want them to be in ascending order.
Last edited by yrollgayanth; 03-31-2010 at 06:33 PM.

6. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
8
Originally Posted by Eranga
What you exactly want to do? If I'm correct you want to sort numbers which are entered by the user. So what's the requirement to generate a random number?
No, he wants the user to tell him how many random numbers to generate, and then he wants to generate and sort them.

@yrollgayanth, no, we're not going to write your code for you. That would not be helping you to learn Java. We will give you advice and pointers.

Always take the time to format and indent your code properly, especially when you are posting it for others to look at. Doing so will give you another opportunity to read it and possibly spot mistakes.
Java Code:
package random;

import java.util.Random;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please Enter the amount of random numbers you want to generate : ");

Random dice = new Random();
int number;

int input = in.nextInt();
Keep the print() and the nextInt() together -- move the Random() initialization and the number declaration before the print(). It makes more sense that way and is easier to read.
Java Code:
for (int counter = 1; counter <= input; counter++) {
Note the spacing. for is a statement, not a method, so leave a space before the parentheses. White space around infix operators (+, -, *, /, ==, <=, etc.) makes things easier to read, but postfix operators (++, --) are customarily placed right after the variable. Also, computer scientists start counting at zero, not at one. You'll see why in a few lines.
Java Code:
number = dice.nextInt(999); // Randomly generated values between 0-999
No, this will give you a random integer between 0 and 998. It will never generate 999 for you. Take another look at the doc.
Java Code:
{
What is that for? This is one of those times that formatting your code properly can help you.
Java Code:
int nums [] = {number}; // Passing randomly generated values to array
No. This is what m00nchile was trying to explain to you. You need an index variable (hint: see your counter variable above?), and you need to do nums[index] = number;
Java Code:
int a,b,t;// Integers for bubble sorting
Declare these below, as you use them. It's better style, unless you will need them out at this scope (which you don't).
Java Code:
int size = nums.length;

for (int i = 0; i < size; i++)
System.out.println(nums[i]);// Display original array

// Sorting
for (a = 1; a < size; a++)
This is legal, but bad style. Use curly braces. Also, remember that we want to start counting at zero (because you have a nums[0] that you don't want to forget about).
Java Code:
for(b = size - 1; b >= a; b--) {
if (nums[b - 1] > nums[b]) {
t = nums[b - 1];
nums[b - 1] = nums[b];
nums[b] = t;

}
}
//Display sorted array
System.out.println("Sorted array");
for (int i = 0; i < size; i++)
System.out.print("" + nums[i]);
System.out.println();
}
}
}
}

-Gary-

7. Member
Join Date
Dec 2009
Posts
11
Rep Power
0
Thanks Gary :)

8. Originally Posted by gcalvin
No, he wants the user to tell him how many random numbers to generate, and then he wants to generate and sort them.
Okay now it's clear to me. Go through the post again and clear it.

9. Originally Posted by yrollgayanth
Thanks Gary :)
If you've solve the problem please mark it as solved.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•