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

Thread: Sorting parallel arrays project - HELP!

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Sorting parallel arrays project - HELP!

    I need the names of the card, rates, and amount owed to print out. however im not sure how to do this. please help. been stuck for too long.
    the class that needs work is the selectionSort class.

    THANKS in advance.


     
    public class sortparallelarrays
    {
     
        public static void  main (String [] args) {
            String [] card = {"Disney Rewards" , "Amazon Rewards", "Chase Sapphire", "Marriott Rewards"};
            double [] rates = {14.24, 13.24, 15.24, 14.20};
            double [] owed = {4500.00, 1300.10, 3200.80, 775.00};
            double amount;
     
     
            displayHeader();
     
            selectionSort(card, rates, owed);
     
     
        }
     
        public static void displayHeader()
        {
            System.out.printf("%8s %9s %10s %16s  \n" , "Card", "Rate of Interest", "Amount owed", "In one year, you'll owe");
     
            System.out.println("===================================================================");
     
     
        }
     
        public static void displayValues(String [] c, double [] r, double [] owed)
        {
            System.out.printf("%8s %18s %25s \n", c, r, owed);
     
        }
     
        public static void selectionSort(String [] card, double [] rates, double [] owed)
        {
            for (int i=0; i< rates.length; i++) {
                for (int j=i+1; j< rates.length; j++){
                    if (rates[j] > rates[i]){
                        double temp = rates[i];
                        rates[i] = rates[j];
                        rates[j] = temp;
     
                        String temppp = card[i];
                        card[i] = card[j];
                        card[j] = temppp;
                        double tempppp = owed[i];
                        owed[i] = owed[j];
                        owed[j] = tempppp;
                    }
                }
            }
            for (int i=0; i<rates.length; i++){
                System.out.println();
                System.out.print( rates[i]);
     
            }
     
        }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting parallel arrays project - HELP!

    What does the posted code do wrong? can you print the program's output that shows what the problem is?

    This method is useful for printing out the contents of arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Sorting parallel arrays project - HELP!

    The posted code only will return this


    Card Rate of Interest Amount owed In one year, you'll owe
    ================================================== =================

    15.24
    14.24
    14.2
    13.24


    however, i need it to return the card names, and amount owed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Sorting parallel arrays project - HELP!

    To print the contents of the other arrays, you need to access elements in those arrays in the print/println/printf method just like you are accessing the element in the array whose values are now printing.

    You could build a String to print out by concatenating separate array elements with +" "+ between them
    or use the printf() method which will format the line for you.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sorting parallel arrays
    By dynasty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2013, 07:23 AM
  2. Two Parallel Arrays
    By jsmnr77 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2013, 09:41 AM
  3. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  4. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM