Results 1 to 3 of 3

Thread: Java Help

  1. #1
    Gigi is offline Member
    Join Date
    Dec 2008
    Posts
    24
    Rep Power
    0

    Default Java Help

    ok, here's my code:
    All i'm trying to do is to generate a certain amount of random numbers (0-100) then pass them to the class and all the class has to do is to return that value back to the client, but when I run it,it displays something different, thanks in advance


    client code:

    import java.util.*;
    import java.util.Scanner;

    public class ch10Ex11SortedArray
    {
    public static void main(String[] args)
    {

    Scanner input = new Scanner(System.in);
    SelectionSort num;
    Random r = new Random();
    int values;
    String list = "";
    System.out.println("Enter the number of values: ");
    values = input.nextInt();
    int[] myNums = new int [values];

    for (int tryIndex = 0 ; tryIndex < values ; tryIndex++)
    {
    myNums[tryIndex]= r.nextInt(100);
    // list += myNums[tryIndex] + " ";
    }
    num = new SelectionSort(myNums);

    System.out.println(num.display());

    }
    }

    class:
    public class SelectionSort
    {
    int[] originalArray;


    public SelectionSort(int[] list)
    {
    originalArray = list;
    }


    public int[] display()
    {
    return(originalArray);
    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Array elements

    You can't print an array. You have to print the elements of the array using a loop... for example a "for" loop.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    CJSL suggest to you something like this.

    Java Code:
            for(int i = 0; i < num.display().length; i++) {
                System.out.println(num.display()[i]);
            }
    Better to read bit about return values from a method.

Posting Permissions

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