Results 1 to 9 of 9
  1. #1
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default 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. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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. #3
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    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. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #5
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    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. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Eranga View Post
    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();
                            }
                    }
            }
    }
    That should about do it.

    -Gary-

  7. #7
    yrollgayanth is offline Member
    Join Date
    Dec 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks Gary :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    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. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 06:17 AM
  2. Import data from file into a gui
    By SBL in forum AWT / Swing
    Replies: 5
    Last Post: 11-21-2009, 04:10 PM
  3. Data array to file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:42 AM
  4. add data into an array
    By mispeed in forum New To Java
    Replies: 9
    Last Post: 11-08-2007, 03:53 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 02:01 AM

Posting Permissions

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