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 11 of 11

Thread: I need help with this pseudocode!

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy I need help with this pseudocode!

    --------- Please delete this thread, thank you for all your help! ---------
    Attached Images Attached Images


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: I need help with this pseudocode!

    That's really not how this works. What have you tried? Where are you stuck? What have you written so far?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this pseudocode!

    Welcome Chris07

    ....yeah, what KevinWorkman said.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this pseudocode!

    ---- Edited my original post ----

    I would appreciate a quick response. Thanks in advance.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this pseudocode!

    Quote Originally Posted by Chris07 View Post
    a quick response
    I will take a shorter lunch break next time. No, on second thought I will continue to volunteer my time when I feel like it, thanks for the consideration anyway.
    Please post all relevant code on the forum and not through a link. There is no guarantee a third party will continue to host the content over time.

    What did your research say about a NullPointerException? The actual error message (which should also be included here when there is a question) will include line numbers to help locate the problem.

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this pseudocode!

    Jps, I've edited the post again and included further information on the error message.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this pseudocode!

    Did you look up NullPointerException to see what causes it?
    The error message tells you where to look, so look there for the cause of the NPE. Which variable is null and why was it not initialized?

  8. #8
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this pseudocode!

    The variable array is not initialized. So, I tried to add the following line of code:

    public Sort(int[] oldArray, int sizeOfOldArray)
    {
        sizeOfArray = sizeOfOldArray; 
        array = new int[sizeOfOldArray];    // <-- Added this line
        for( int i = 0; i < sizeOfArray; i++) {
            array[i] = oldArray[i];
    }

    I thought this should do the trick, but now it's giving me another error:

     

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this pseudocode!

    Arrays are numbered beginning with zero.
    So an array with a size of 5 will have elements numbered 0 1 2 3 and 4

    Considering the code in post #8, why are you passing the size of oldArray to the method? In java size is a property of an array and can be accessed through the array.
    See the API on arrays for details

  10. #10
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help with this pseudocode!

    Okay, I got rid of the errors. Now, how do I sort the numbers from least to greatest? Here is my new code:

    import java.util.Scanner;
    import java.util.Random;
     
    public class SortedArray
    {
        int [] Array;
        int sizeOfArray;
        Sort sortedArray;
        Scanner input = new Scanner(System.in);
        public SortedArray()
        {
            System.out.print("Enter the number of values to put in the array: ");
            sizeOfArray = input.nextInt();
            Array = new int [sizeOfArray];
            System.out.println("");
            for(int i = 0; i < sizeOfArray; i++)
            {
                Random r = new Random();
                Array[i] = r.nextInt(100) + 1;
                System.out.println(Array[i]);
            }
            sortedArray = new Sort(Array, sizeOfArray);
            sortedArray.display();
        } 
    }
     
     
    public class Sort
    {
      int[] array;
      int sizeOfArray;
      public Sort(int[] oldArray, int sizeOfOldArray)
      {
        sizeOfArray = sizeOfOldArray;
        array = new int [sizeOfArray];
        for( int i = 0; i < sizeOfArray; i++)
        {
            array[i] = oldArray[i];
        }
        sort();
      }
     
      public void display()
        { 
           for ( int i = 0; i < sizeOfArray; i++){
               System.out.println(array[i]);
           }
        }
     
      private void sort()
        {
            for (int i = 0; i < sizeOfArray; i++)
            {
                for (int j = 0; j < sizeOfArray; i++)
                {
                    if (array[j] < array[j])
                    {
                        swap(j,i);
                    }
                }
            }
        }
     
        private void swap(int x, int y)
        {
            int temp;
            temp = array[x];
            array[x] = array[y];
            array[y] = temp;
      }
    }

    Thanks for all the help thus far, almost finished.

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with this pseudocode!

    Quote Originally Posted by Chris07 View Post
    how do I sort the numbers
    There are many sort algorithms. Which do you plan to use?
    What steps must be taken to implement this type of sort?
    List out the steps to take and work on one step at a time.
    If you have any questions be sure to include details about which sort you are using and what step you are on with the question.

    Please use code tags when posting code. See the Announcements page for help.

Similar Threads

  1. Pseudocode help!
    By mohamed95 in forum Algorithms & Recursion
    Replies: 0
    Last Post: January 13th, 2013, 05:22 PM
  2. pseudocode tutorials
    By pascal in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2012, 01:19 AM
  3. Need help with pseudocode please
    By nomss in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 18th, 2011, 03:50 AM
  4. Need help converting Pseudocode to java
    By firebyrd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 05:28 AM
  5. Replies: 4
    Last Post: February 23rd, 2011, 08:20 AM