Anyone on NT know Java

Status
Not open for further replies.

thadocta

Banned
Joined
Sep 4, 2012
Messages
5,952
Reaction score
1,144
trying to learn so i could eventually make some apps but im struggling through this

1. Open and read the first line of the input file. See LottoNumbers.java included with the Chapter 6 Slideshow for examples on how to do this.
2. Create an integer array named numberArray with size to match the first line of the file. So if the first line of the file contains 500, the array will have 500 “slots.”
3. Read the remaining lines of the input file, parsing them as numbers, and assigning them to slots in numberArray.
4. Write a method named sort, which takes numberArray as a parameter and sorts the array from low to high.
5. Once numberArray has been sorted, compute the average of the numbers in the array (not including the count).
6. When sorting is completed and the average has been calculated, open a file named dataout.txt and write the following information to it:
a. The count (number of “slots” in numberArray).
b. The sorted list of numbers in numberArray (one per line).
c. The calculated average of the numbers in the array.


need help on number 4

herees wat i have done so far




public class NumberSorter{

public static void main(String[] args){


int[] intArray = new int[11];

intArray[0] = 12345;
intArray[1] = 23456;
intArray[2] = 123;
intArray[3] = 4567;
intArray[4] = 123456;
intArray[5] = 7654;
intArray[6] = 999;
intArray[7] = 3453;
intArray[8] = 997733;
intArray[9] = 43;
intArray[10] =654321;
for (int i=0; i
 
What IDE are you using?  Try Eclipse, if you're not already using it.  It's pretty helpful because it underlines errors and offers solutions.  It also allows you to compile, run, and debug your code.

As for your method, I haven't coded in Java for a while, but it should look something like this:

    public int[] Sort (int[] numberArray) {
        
        for (int i = 0; i < numberArray.length; i++) {
            for (int j = i+1; j < numberArray.length; j++) {
                if (numberArray >= numberArray[j]) {
                    int temp;
                    temp = numberArray;
                    numberArray = numberArray[j];
                    numberArray[j] = temp;
                }
            }
        }
        
        for (int x  = 0; x < numberArray.length; x++) {
            System.out.println(numberArray[x]);
        }
        
        return numberArray;
    }

This is selection sort, not the best sorting algorithm, but it works here.  This gif pretty much explains it, you can PM me if you need more help...
 
Why not just call Arrays.sort()? I don't see the point in writing your own sorting algorithm, and honestly if you don't know about sorting algorithms I don't know how its even possible to know where to start. Theres a whole class and entire books on sorting algorithms. You should check out this book for sorting http://en.wikipedia.org/wiki/Introduction_to_Algorithms. Im sure you can find a pdf online. Its a great reference, and i honestly wish I learned it better. Every single interview i have been in has asked me about sorting.
 
Why not just call Arrays.sort()? I don't see the point in writing your own sorting algorithm, and honestly if you don't know about sorting algorithms I don't know how its even possible to know where to start. Theres a whole class and entire books on sorting algorithms. You should check out this book for sorting http://en.wikipedia.org/wiki/Introduction_to_Algorithms. Im sure you can find a pdf online. Its a great reference, and i honestly wish I learned it better. Every single interview i have been in has asked me about sorting.
Thanks for the advice
 
Its good that your trying to learn java. Honestly youll have haters telling you its never used, but most major companies use it, its easy to learn, and a great starting point. If you get good with java, you looking at future jobs with over 100k salary.
 
What IDE are you using?  Try Eclipse, if you're not already using it.  It's pretty helpful because it underlines errors and offers solutions.  It also allows you to compile, run, and debug your code.

As for your method, I haven't coded in Java for a while, but it should look something like this:

    public int[] Sort (int[] numberArray) {

        

        for (int i = 0; i = numberArray[j]) {

                    int temp;

                    temp = numberArray;

                    numberArray = numberArray[j];

                    numberArray[j] = temp;

                }

            }

        }

        

        for (int x  = 0; x
 
Why not just call Arrays.sort()? I don't see the point in writing your own sorting algorithm, and honestly if you don't know about sorting algorithms I don't know how its even possible to know where to start.
I see your point, but he's just a beginner.  This exercise is more for him to get familiar with loops and arrays than being efficient.  No sense in him learning to walk before he can crawl...
 
We should have a Official Coding thread 8o?

I know HTML, CSS, getting into javascript, PHP just working my self up right now.
 
Last edited:
Status
Not open for further replies.
Back
Top Bottom