--------- Please delete this thread, thank you for all your help! ---------
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.
--------- Please delete this thread, thank you for all your help! ---------
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!
Welcome Chris07
....yeah, what KevinWorkman said.
---- Edited my original post ----
I would appreciate a quick response. Thanks in advance.
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.
Jps, I've edited the post again and included further information on the error message.
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?
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:
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
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.
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.