# Import data to an Array

• 03-31-2010, 04:39 PM
yrollgayanth
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

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();         }     } }     }```
• 03-31-2010, 04:52 PM
m00nchile
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:
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.
• 03-31-2010, 05:58 PM
yrollgayanth
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
• 03-31-2010, 06:15 PM
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?
• 03-31-2010, 06:22 PM
yrollgayanth
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.
• 03-31-2010, 07:01 PM
gcalvin
Quote:

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.
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.
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.
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.
Code:

`                        {`
What is that for? This is one of those times that formatting your code properly can help you.
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;
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).
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).
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-
• 03-31-2010, 07:04 PM
yrollgayanth
Thanks Gary :)
• 04-01-2010, 03:24 AM
Eranga
Quote:

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.
• 04-01-2010, 03:30 AM
Eranga
Quote:

Originally Posted by yrollgayanth
Thanks Gary :)

If you've solve the problem please mark it as solved.