Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Confused on Arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confused on Arrays

    I am trying to do a shuffle program where I randomize a group of numbers and print them out so none repeat. I have the randomization right just not sure how to implement a users input with an array.

    Thank you for any help.

    p.s I am new to this community so anything I may do wrong as far as the way I post or anything of that nature please let me know.

    import java.util.*;
     
    public class Shuffle
    {
    public static int[] RandomizeArray(int[] array){
    Random rgen = new Random();
     
     for (int i = 0; i<array.length; i++){
    int randomPosition = rgen.nextInt(array.length);
    int temp = array[i];
                array[i] = array[randomPosition];
                array[randomPosition] = temp;
      }
               return array; 
     }
    public static void main(String [] args)
    {
    Scanner keyboard = new scanner(System.in);
    System.out.println("Enter number of tracks");
    int n = keyboard.nextInt();
    array[n] = RandomizeArray(n);
    System.out.println(array[n]);
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Confused on Arrays

    That code is far from compiling. Please fix your errors.

    Are you asking how to get user input and enter it into array?
    loop array length {
        get user input
        store in array
    }
    Not that hard.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on Arrays

    I have it to where it will compile now. I am just confused as to the syntax in being able to implement my RandomizeArray and the users input in the main. I know this is a simple task but Im new and just looking for some help.

    import java.util.*;
     
    public class Shuffle
    {
    public static int[] RandomizeArray(int[] array){
    Random rgen = new Random();
     
     for (int i = 0; i<array.length; i++){
    int randomPosition = rgen.nextInt(array.length);
    int temp = array[i];
                array[i] = array[randomPosition];
                array[randomPosition] = temp;
      }
               return array; 
     }
    public static void main(String [] args)
    {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter number of tracks");
    int n = keyboard.nextInt();
     
    }
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Confused on Arrays

    Extending the pseudocode above.
    get array length
    make array
    loop array length {
        get user input
        store in array
    }
    randomize array
    print array
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Greenville NC
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on Arrays

    I have edited my code and dont understand why it doesnt work.

    import java.util.*;
     
    public class Shuffle
    {
    public static int[] RandomizeArray(int[] array){
    Random rgen = new Random();
     
     for (int i = 0; i<array.length; i++){
    int randomPosition = rgen.nextInt(array.length);
    int temp = array[i];
                array[i] = array[randomPosition];
                array[randomPosition] = temp;
      }
               return array; 
     }
    public static void main(String [] args)
    {
    Scanner keyboard = new Scanner(System.in);
    int n;
    System.out.println("Enter number of tracks");
    n = keyboard.nextInt();
    RandomizeArray(n);
    System.out.println(RandomizeArray(n));
    }
    }

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: Confused on Arrays

    What type of parameter does the RandomizeArray method need? what type of parameter are you passing to the method? The error message the compiler gives you tells you exactly the problem.

    Also, why have you not followed my pseudocode?
    Improving the world one idiot at a time!

Similar Threads

  1. So confused, I need help!
    By sessypeanut in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 10:45 PM
  2. I'm confused
    By Wonderlandslost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2012, 12:33 PM
  3. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM
  4. Confused about Arrays and JOptionPane!
    By Java Neil in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 4th, 2011, 03:55 PM
  5. Confused with Arrays
    By Solidius in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 09:54 AM