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

Thread: I NEED HELP WITH LAST CODE PLEASE!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I NEED HELP WITH LAST CODE PLEASE!

    This is my code so far. My last thing I need and don't know how because my teacher logged off is to print the median number. The double med = median(nums); needs to be printed and I need help. It should go in the printArray method and its not printing. I have tried many different ways please help. Its due in less than 2 hours.




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

    public class ArrayLab {
    public static void main(String[] args) throws IOException {

    double[] nums = loadArray();
    printArray(nums);
    sort(nums);
    printArray(nums);
    double med = median(nums);

    }

    public static double[] loadArray() throws IOException {
    Scanner fin = new Scanner(new File("run.txt"));
    int size = fin.nextInt();
    double[] values = new double[size];
    for(int index = 0; index < size; index++)
    values[index] = fin.nextDouble();
    return values;
    }

    public static void printArray(double[] values){

    for(int index = 0; index < values.length; index++)
    System.out.print(values[index] +"\t\t" );
    System.out.println();

    }//end printArray

    public static void sort(double[] list){
    for(int pass = 0; pass < list.length-1; pass++) {
    double max = list[pass];
    int maxIndex = pass;
    for(int index = pass+1; index < list.length; index++){
    if(max < list[index]){
    max = list[index];
    maxIndex = index;
    }//end of if
    }//end of for loop

    if(maxIndex != pass) {
    list[maxIndex] = list[pass];
    list[pass] = max;

    }//end of if minIndex
    }//end of outer for
    }//end method

    public static double median(double[] values) {
    double median;
    if(values.length % 2 == 0)
    median = (values[values.length/2] + values[values.length/2 -1])/2.0;
    else
    median = values[values.length/2];

    return median;
    }


    }//end of public class


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

    Default Re: I NEED HELP WITH LAST CODE PLEASE!

    System.out.println("The median is: " + med);
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I NEED HELP WITH LAST CODE PLEASE!

    I tried that and it didnt work. =(

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I NEED HELP WITH LAST CODE PLEASE!

    wait I got it thank you =DDDDDDDDDDDDDDDDDDDDD YAY ITS FINISHED AFTER HOURS. THANK YOU . YOU ARE A LIFE SAVER.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I NEED HELP WITH LAST CODE PLEASE!

    I'm stunned that you managed the rest of the code but a simple print statement stumped you. Overthinking things probably. Anyway good luck.
    Improving the world one idiot at a time!

Similar Threads

  1. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM
  2. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM