kyle
kyle

Reputation: 9

Getting a weird output on my program lost on what to do next to fix it

So i've ben at this program for awhile and i've gotten everything to work without compiling error but now I'm getting a weird output, first ill post the program the the problem.

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

public class project3 {

    private static double[] payrate;
    private static String[] names;

    public static void SortData(double payrate[]) {
        int first;
        int temp;
        int i;
        int j;
        for (i = payrate.length - 1; i > 0; i--) {
            first = 0;
            for (j = 1; j <= i; j++) {
                if (payrate[j] < payrate[first]) {
                    first = j;
                }
            }
            temp = (int) payrate[first];
            payrate[first] = payrate[i];
            payrate[i] = temp;
        }
    }

    public static void GetData() {

        Scanner input = new Scanner(System.in);
        System.out.println("How many names do you want to enter?");
        String strNum = input.nextLine();
        int num = Integer.parseInt(strNum);
        int array[] = new int[num];
        for (int i = 0; i < array.length; i++) {
            names = new String[num];
            System.out.println("enter employee's name: ");
            names[i] = input.nextLine();
                            //while(names[i].length < 2)
            //{
            //System.out.println("enter valid employee's name: ");

                            //names[i] = input.nextLine(); 
            //} 
        }
        for (int j = 0; j < array.length; j++) {
            payrate = new double[num];
            System.out.println("enter employee's payrate: ");
            payrate[j] = input.nextDouble();
            while (payrate[j] > 100 || payrate[j] < 0) {
                System.out.println("enter valid employee's payrate: ");
                payrate[j] = input.nextDouble();
            }
        }
    }

    public static void DisplayData(double payrate[], String names[]) {

        System.out.printf("Name    PayRate\n");
        for (int l = 0; l < names.length; l++) {
            //for(int i=0;i<names.length;i++)
            // {
            System.out.print(names[l]);
            System.out.printf("\n", payrate[l]);
            //} 
        }
    }

    public static void main(String[] args) {
        GetData();
        SortData(payrate);
        DisplayData(payrate, names);

    }
}

The program Is suppose to print out something like this

Name      Payrate
Daniel    54.76
josh      73.12
kyle      12.54

but the program is printing out this

Name    PayRate
null
null
null
null
qt

Upvotes: 0

Views: 62

Answers (1)

TomN
TomN

Reputation: 584

Here are some point you can correct it.

  1. in GetData(), you should put names = new String[num]; anywhere before loop start, so deos payrate = new double[num];.

  2. in SortData(), why not use double temp;? then you don't have to turn payrate[first] into int type.

  3. in DisplayData(), System.out.printf("\n", payrate[l]); disply nothing but change line. I think it's better to do like

    System.out.printf("Name\tPayRate\n");
    for (int l = 0; l < names.length; l++) {
        System.out.print(names[l]);
        System.out.println("\t"+payrate[l]);
    }
    

well, it's eaiser to explain with code than English. :'(

public static void SortData(double payrate[]) {
    int first;
    double temp;
    String tempString;
    int i;
    int j;        
    for (i = payrate.length - 1; i > 0; i--) {
        first = 0;
        for (j = 1; j <= i; j++) {
            if (payrate[j] < payrate[first]) {
                first = j;
            }
        }
        temp = payrate[first];
        payrate[first] = payrate[i];
        payrate[i] = temp;

        tempString = names[first];
        names[first] = names[i];
        names[i] = tempString;
    }
}

Upvotes: 1

Related Questions