?
Solved

Sorting a Multidimensional array, descending order

Posted on 2005-04-05
20
Medium Priority
?
297 Views
Last Modified: 2013-11-23
I hope you can help me out. I'm working on a program that displays transaction data based upon a multidimensional array that was created. I need to create a sort my transactions  once a transaction type is chosen.  My question is, am I heading in the right direction with my code and am I able to get a hint if I'm not in the right direction?
Thanks in advance!!


This is my code so far:

// Import libraries
import java.lang.Math;
import java.text.DecimalFormat;
import java.util.*;

public class OnlineBanking {

      public static void main(String [] args)
      {
// Initialize Initial state variables and constants
      String[] users = { "George", "Christie", "Manny", "Seth", "Pat", "Amanda" };
      int[] customerid = { 1, 2, 3, 4, 5, 6 };
      double[][] accountvalue = {{0.00, 0.00, 0.00},{100.00, 100.00, 100.00},{0.00, 100.00, 200.00},{200.00, 100.00, 0.00},{50.00, 75.00, 100.00},{100.00, 75.00, 50.00}};
      double[][][] transactions = { { {-10.34, -232.22, 255.23, 123.23, -43.23}, {12.32, 2.87, -35.48, 49.44, 55.44}, {12.22, 212.22, 35.03, 41.97, -53.29} },
{ {12.34, -282.22, 31.23, 26.55, -123.23}, {12.32, 4.87, -34.48, 47.44, 51.44}, {62.22, 222.22, 75.03, 61.97, -52.29} },
{ {13.34, -272.22, 32.23, 36.55, -223.23}, {42.32, 5.87, -35.48, 46.44, 52.44}, {52.22, 242.22, 15.03, 45.97, -57.29} },
{ {14.34, -262.22, 33.23, 46.55, -323.23}, {-82.32, 6.87, 38.48, 45.44, 53.44}, {42.22, 612.22, 35.03, 34.97, -54.29} },
{ {15.34, -252.22, 34.23, 56.55, -423.23}, {14.32, -7.87, 36.48, 48.44, 54.44}, {32.22, 262.22, 25.03, 44.97, -53.29} },
{ {16.34, -242.22, 35.23, -66.55, 523.23}, {15.32, -8.87, 34.48, 44.44, 55.44}, {92.22, 292.22, 85.03, 66.97, -59.29} } };
// Initialize variables and constants
      String[] accounttype = { "Saving","Checking","Money-Market",};
      int currentuser = 0 ;
      int currenttype = 0;
      int menuitem = 0;      
      int no = 0;
      int yes = 1;
      int valid = no;
      int exit = no;
      int fine = 0;
      int bonus = 0;

      
// myNumberFormat Create formatting class for display
      DecimalFormat myNumberFormat = new DecimalFormat("#####0.00");
      System.out.println("\n\nWelcome to The learning Team Bank");
      System.out.println("Your on-line transaction manager\n");
      while (exit == no)
// facts to work the menu system
// ascii chart for read()
// 65 = A      97 = a
// 66 = B      98 = b
// 67 = C      99 = c
// 68 = D      100 = d
// 69 = E      101 = e
// 70 = F      102 = f
// 71 = G      103 = g
// 72 = H      104 = h
// 73 = I      105 = i
// 74 = J      106 = j
// 75 = K      107 = k
// 76 = L      108 = l
// 77 = M      109 = m
// 78 = N      110 = n
// 79 = O      111 = o
// 80 = P      112 = p
// 81 = Q      113 = q
// 82 = R      114 = r
// 83 = S      115 = s
// 84 = T      116 = t
// 85 = U      117 = u
// 86 = V      118 = v
// 87 = W      119 = w
// 88 = X      120 = x
// 89 = Y      121 = y
// 90 = Z      122 = z
      {
      // Start of Main menu system:
                        String output3 = myNumberFormat.format(accountvalue[currentuser][currenttype]);
                        String output4 = " ";
                        double dtemp = 0.0;
                        fine = 0;
                        bonus = 0;
                        
                        for (int a=0; a < 5; a++)
                        {
                         output4 = output4 + myNumberFormat.format(transactions[currentuser][currenttype][a]) + " ";
                         dtemp = transactions[currentuser][currenttype][a];
                         if ( dtemp < 0.00)
                         {
                              fine = fine + 1;
                         }
                         
                          if (dtemp > 100.00)
                         {
                              bonus = bonus + 1;
                         }
                         } // end of for loop

            valid = no;
            while (valid==no)
            {
                        System.out.print("\nPOS406 Team Assignment for LT B\n");
                        System.out.print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                        System.out.print("Customer Name : " + users[currentuser]+ "\n");
                        System.out.print("Customer ID  : " + customerid[currentuser] + "\n");
                        System.out.print("Bonus : " + bonus + "   Fine : " + fine + "\n");
                        System.out.print("Carried Over Balance in " + accounttype[currenttype] + ": "  + output3 + "\n");
                        System.out.print("Transactions for Account " + customerid[currentuser] + " : [" + output4 + "]\n");
                        System.out.print("\n");
                        System.out.print("Select operation using assigned letter\n");
                        System.out.print("--------------------------------------\n");
                        System.out.print("I) Change Customer ID\n");
                        System.out.print("N) Change Account Number\n");
                        System.out.print("A) Display Ascending Transactions List\n");
                        System.out.print("D) Display Descending Transactions List\n");
                        System.out.print("S) Display Account Statistics\n");
                        System.out.print("T) Display Transaction Report\n");
                        System.out.print("Q) Quit\n");
                        System.out.print("\n");
                        System.out.print(" Selection is  (I,N,A,D,S,T,Q) : ");

                  try
                  {                  
// read buffer until find the chr 13
                  menuitem = System.in.read();                        
                  while(System.in.read() != 10)
                        {
// do not do anything just empty the buffer
                        }      
// debug                        System.out.print(" Menu Item is " + menuitem);
                        if (menuitem == 81 || menuitem == 113)
                        {
                              exit = yes;
                        }
                              
                  }
                  catch(Exception error)
                  {
                        System.out.println(" \nInvalid entree!(1)\n");
                        valid = no;
                  }
// convert menuitem to upper case:
                  System.out.print("\n");
                  if (menuitem > 84)
                  {
                        menuitem = menuitem - 32;
                  }
// Facts to work the functions.
// Array list for processing.
// users[] string with user's name
// Syntax: users[currentuser] (for current user)
//
// customerid[] int with ID number
// Syntax: customerid[currentuser] (for current ID)
//
// accountvalue[][] double with starting $ value
// Syntax: accountvalue[currentuser][currenttype] (for current user and current account type)
//
// accounttype[] string with names of accounts
// Syntax: accounttype[currenttype] (for name of current account type)
//
// transactions[][][] double with five (5) $ values for each user and each account type
// Syntax: transactions[currentuser][currenttype][0-4]
//
// Variable for current array index's
// currentuser hold current user index (0 - 5)
// currenttype holds current account type index (0 - 2)      
//
// switch / case to process the menuitem



            switch (menuitem)
            {
                case 73 : // I Change Customer ID
                        {
                              currentuser = currentuser + 1;
                              if (currentuser > 5) { currentuser = 0;}
                              valid = yes;
                              break;
                        }
                case 78 : // N Change Account Number
                        {
                              currenttype = currenttype + 1;
                              if (currenttype > 2) { currenttype = 0;}
                              valid = yes;
                              break;
                        }
                  case 65 : // A Display Ascending Transactions List
                        {
                              valid = yes;
                              break;
                        }
                  case 68 : // D Display Descending Transactions List
                        {
                              valid = yes;
                        
*****                  Arrays.sort(transactions);
                        
                              for (i=0; i<1000, i++) {
                                    System.out.println(i + " :  " + transaction[i]);
                           
                              for (t=0; t<5, t++) {
          
                              if (transaction[i] >= transaction[t]){
                                     
                                  Count = count - 1;
                                      }
                                    }
                                    Count=count -1;
                                    
                                    Otherarray[count] = cash[i];
}

                              
                              break;
                        }
                  case 83 : // S Display Account Statistics
                        {
                              valid = yes;
                              break;
                        }
                  case 84 : // T Display Transaction Report
                        {
currentuser = currentuser + 1;
//this syncronizes the item id in the array with the user id

System.out.print("Detail Listing\n");
System.out.print("--------------\n");
System.out.print("Transaction\t Amount\t Balance\n");
System.out.print("--------------------------------\n");
System.out.print("Savings Account\n");
System.out.print("User ID:\t" + currentuser + "\n");
System.out.print("Starting balance:\t" + accountvalue[currentuser][0] + "\n");

double varbalance = accountvalue[currentuser][0];
String varstate;

for (int x=0; x < 5; x++){
      
      if (transactions[currentuser][0][x] < 0){
      varstate="withdrawl";
      }else{
      varstate="deposit\t";
      }
      
      varbalance = (varbalance + transactions[currentuser][0][x]);

      System.out.print(varstate + "\t" + transactions[currentuser][0][x] + "\t" + myNumberFormat.format(varbalance) + "\n");

}





System.out.print("\n\n\nChecking Account\n");
System.out.print("User ID:\t" + currentuser + "\n");
System.out.print("Starting balance:\t" + accountvalue[currentuser][1] + "\n");

varbalance = accountvalue[currentuser][1];


for (int x=0; x < 5; x++){
      
      if (transactions[currentuser][1][x] < 0){
      varstate="withdrawl";
      }else{
      varstate="deposit\t";
      }
      
      varbalance = (varbalance + transactions[currentuser][1][x]);

      System.out.print(varstate + "\t" + transactions[currentuser][1][x] + "\t" + myNumberFormat.format(varbalance) + "\n");

}


System.out.print("Starting balance:\t" + accountvalue[currentuser][2] + "\n");



System.out.print("\n\n\nMoney Market Account\n");
System.out.print("User ID:\t" + currentuser + "\n");
System.out.print("Starting balance:\t" + accountvalue[currentuser][2] + "\n");

varbalance = accountvalue[currentuser][2];


for (int x=0; x < 5; x++){
      
      if (transactions[currentuser][2][x] < 0){
      varstate="withdrawl";
      }else{
      varstate="deposit\t";
      }
      
      varbalance = (varbalance + transactions[currentuser][2][x]);

      System.out.print(varstate + "\t" + transactions[currentuser][2][x] + "\t" + myNumberFormat.format(varbalance) + "\n");

}
      
                              valid = yes;
                              break;
                        }
                  case 81 : // Q Quit
                        {
                              valid = yes;
                              break;
                        }

                default  :
                        {
                              System.out.print("\nERROR; Please select one of the menu item (I,N,A,D,S,T,Q)\n");
                              valid = no;      
                        }  
                  } // End of switch
            }// End of valid while loop                  
//  stop display and ask user to hit the enter key to continue.
                        try
                        {      
                              System.out.println(" \nHit Enter Key to continue!\n" );      
// read buffer until find the chr 10
                        while(System.in.read() != 10)
                        {
// do not do anything just empty the buffer
                        }
                        }
                        catch(Exception error)
                        {
                              System.out.println(" \nbad input! bad!\n");
                        }
            }// End of exit while loop
      System.out.println("\nThank you for using The My Bank");
      System.out.println("On-line transaction manager\n");
      }
}
0
Comment
Question by:chunkyshu
  • 10
  • 9
20 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13712644
> Arrays.sort(transactions);

Thats what you want to use, but you'll need to supply a custom comparator to specify the ordering you require

public MyComparator implement Comparator
{
   public int compare(Object _a, Object _b)
   {
      double[][] a = (double[][]) _a;
      double[][] b = (double[][]) _b;
      // new return the comparison result of these two
   }
}
0
 

Author Comment

by:chunkyshu
ID: 13717952
Thanks.. I am new to programming and I had a hard time figuring that portion out. So I am doing this but it just doesn't seem to compile and run the required outcome:

 case 68 : // D Display Descending Transactions List
{  
valid = yes;
                        
double[] TempTrans = new double[5];
     for(int Index = 0; Index < 3; Index++)
     {
     for(int IndexB = 0; IndexB < 5; IndexB++)
     {
          TempTrans[IndexB] = transactions[Index][IndexB];  
          System.out.println("unsorted list : " + transactions[Index][IndexB].toString() + "\n");
     }
     Arrays.sort(TempTrans);
     System.out.println("Account : " + accounttype[Index] + "\n");
     for(int IndexB = 4; IndexB >= 0; IndexB--)
          {
          System.out.println("Sorted List : " + TempTrans[IndexB] + "\n");
          }
}
break;
}
                        
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13719556
What is the order you want to display the transaction (sorted by name and then sorted by acount type)? I don't think you want to order the transaction numbers [0-4], right?
Do you display the sorted transaction for a specific user or for all the users?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:chunkyshu
ID: 13719789
I want to display my transactions by user, then by account type... i.e. Christie's Saving's account, transactions listed in descending order... is that possible?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13719956
Still not sure what you are looking for.
Do you want to print "Amanda" transaction before "George" (alphabetically)?
And for a specific user do you want to print "Saving" transactions before "Checking" transaction?
Is there any order for the 4 transactions once picked a user and type? or do you want to display them in the order they are defined?
Can you give an expected output example?
0
 

Author Comment

by:chunkyshu
ID: 13720186
Sorry, maybe a little background will help. I'll attach code that works to give you an idea also. Basically when the program is run it displays the account information of a person, i.e. "Amanda". I can then pickfrom a group of menu items, I can change the user or change the account type (to savings, checking, money market). Once the data is displayed again, I'm trying to sort the transactions of that are attached to the account for User/Account type.

Here is code that works, without my sorting code in it yet:

import java.lang.Math;
import java.text.DecimalFormat;

public class OnLineBank
{
      public static void main(String [] args)
      {
// Initialize Initial state variables and constants
      String[] users = { "George", "Christie", "Manny", "Seth", "Pat", "Amanda" };
      int[] customerid = { 1, 2, 3, 4, 5, 6 };
      double[][] accountvalue = {{0.00, 0.00, 0.00},{100.00, 100.00, 100.00},{0.00, 100.00, 200.00},{200.00, 100.00, 0.00},{50.00, 75.00, 100.00},{100.00, 75.00, 50.00}};
      double[][][] transactions = { { {-10.34, -232.22, 255.23, 123.23, -43.23}, {12.32, 2.87, -35.48, 49.44, 55.44}, {12.22, 212.22, 35.03, 41.97, -53.29} },
{ {12.34, -282.22, 31.23, 26.55, -123.23}, {12.32, 4.87, -34.48, 47.44, 51.44}, {62.22, 222.22, 75.03, 61.97, -52.29} },
{ {13.34, -272.22, 32.23, 36.55, -223.23}, {42.32, 5.87, -35.48, 46.44, 52.44}, {52.22, 242.22, 15.03, 45.97, -57.29} },
{ {14.34, -262.22, 33.23, 46.55, -323.23}, {-82.32, 6.87, 38.48, 45.44, 53.44}, {42.22, 612.22, 35.03, 34.97, -54.29} },
{ {15.34, -252.22, 34.23, 56.55, -423.23}, {14.32, -7.87, 36.48, 48.44, 54.44}, {32.22, 262.22, 25.03, 44.97, -53.29} },
{ {16.34, -242.22, 35.23, -66.55, 523.23}, {15.32, -8.87, 34.48, 44.44, 55.44}, {92.22, 292.22, 85.03, 66.97, -59.29} } };
// Initialize variables and constants
      String[] accounttype = { "Saving","Checking","Money-Market",};
      int currentuser = 0 ;
      int currenttype = 0;
      int menuitem = 0;      
      int no = 0;
      int yes = 1;
      int valid = no;
      int valid2 = no;
      int exit = no;
      int fine = 2;
      int bonus = 1;

      
// myNumberFormat Create formatting class for display
      DecimalFormat myNumberFormat = new DecimalFormat("#####0.00");
      System.out.println("\n\nWelcome to The learning Team Bank");
      System.out.println("Your on-line transaction manager\n");
      while (exit == no)
      {

                        String output3 = myNumberFormat.format(accountvalue[currentuser][currenttype]);
                        String output4 = " ";
                        double dtemp = 0.0;
                        
                        for (int a=0; a < 5; a++)
                        {
                         output4 = output4 + myNumberFormat.format(transactions[currentuser][currenttype][a]) + " ";
                         dtemp = transactions[currentuser][currenttype][a];

                         } // end of for loop

            valid = no;
            while (valid==no)
            {
                        System.out.print("\nPOS406 Team Assignment for LT B\n");
                        System.out.print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                        System.out.print("Customer Name : " + users[currentuser]+ "\n");
                        System.out.print("Customer ID  : " + customerid[currentuser] + "\n");
                        System.out.print("Bonus : $" + bonus + "  Fine : $" + fine + "\n");
                        System.out.print("Carried Over Balance in " + accounttype[currenttype] + ": "  + output3 + "\n");
                        System.out.print("Transactions for Account " + customerid[currentuser] + " : [" + output4 + "]\n");
                        System.out.print("\n");
                        System.out.print("Select operation using assigned letter\n");
                        System.out.print("--------------------------------------\n");
                        System.out.print("I) Change Customer ID\n");
                        System.out.print("N) Change Account Number\n");
                        System.out.print("A) Display Ascending Transactions List\n");
                        System.out.print("D) Display Descending Transactions List\n");
                        System.out.print("S) Display Account Statistics\n");
                        System.out.print("T) Display Transaction Report\n");
                        System.out.print("Q) Quit\n");
                        System.out.print("\n");
                        System.out.print(" Selection is  (I,N,A,D,S,T,Q) : ");

                  try
                  {                  

                  menuitem = System.in.read();                        
                  while(System.in.read() != 10)
                        {

                        }      
                                  System.out.print(" Menu Item is " + menuitem);
                        if (menuitem == 81 || menuitem == 113)
                        {
                              exit = yes;
                        }
                              
                  }
                  catch(Exception error)
                  {
                        System.out.println(" \nInvalid entree!(1)\n");
                        valid = no;
                  }

                  System.out.print("\n");
                  if (menuitem > 84)
                  {
                        menuitem = menuitem - 32;
                  }
            switch (menuitem)
            {
                case 73 : // I Change Customer ID
                        {
                        valid2 = no;
                        while (valid2==no)
                        {
                        try
                        {      
                              System.out.println("\nEnter one of the User Accounts:");
                              for (int u=0 ; u<6 ; u++)
                              {
                                    System.out.println(customerid[u] + ": " + users[u] + ":");
                              }
                              System.out.print("\n(1,2,3,4,5,6): ");
                              

                              currentuser = System.in.read();
                              while(System.in.read() != 10)
                              {

                              }
                              if (currentuser == 49 || currentuser == 50 || currentuser == 51 || currentuser == 52 || currentuser == 53 || currentuser == 54)
                              {
                                    currentuser = currentuser - 49;
                                    valid2 = yes;                              
                              } else
                              {
                                    System.out.println("Please select a number betwee 1 and 6\n");
                                    valid2 = no;
                              }
                        }
                        catch(Exception error)
                        {
                              System.out.println(" \nInvalid entree!(1)\n");
//                              valid2 = no;
                        }
                  } // End of While loop

                              valid = yes;
                              break;
                        }
                case 78 : // N Change Account Number
                        {
// Start of Account type selection:
                        valid2 = no;
                        while (valid2==no)
                        {
                        try
                        {      
                              System.out.println("\nenter one of the Account type:");
                              System.out.println("0: saving:");
                              System.out.println("1: checking:");
                              System.out.print("2: money-market:\n(0,1,2): ");            
// read buffer until find the chr 10
                              currenttype = System.in.read();
                              while(System.in.read() != 10)
                              {
// do not do anything just empty the buffer
                              }
// debug                        System.out.println("currenttype " + currenttype);
                              if (currenttype == 48 ||currenttype == 49 || currenttype == 50)
                              {
                                    currenttype = currenttype - 48;
                                    valid2 = yes;      
                              } else
                              {
                                    System.out.println("Please select a number betwee 0 and 2\n");
//                                    valid = no;
                              }
                        }
                        catch(Exception error)
                        {
                              System.out.println(" \nInvalid entree!(2)\n");
                              valid2 = no;
                        }

                  } // End of While loop
                              valid = yes;
                              break;
                        }
                  case 65 : // A Display Ascending Transactions List
                        {
                              valid = yes;
                              break;
                        }
                  case 68 : // D Display Descending Transactions List
                        {
                              valid = yes;
                              break;
                        }
                  case 83 : // S Display Account Statistics
                        {
                              double highTrans = 0; // Value of highest transaction
                              double lowTrans = 0; // Value of lowest transaction
                              double totalTrans = 0; // Total value of all transactions
                              double aveTrans = 0; // Average value of transactions

                              for (int x=0; x < 5; x++){
                                    if (transactions[currentuser][currenttype][x] < lowTrans){  // Determine lowest transaction
                                          lowTrans = transactions[currentuser][currenttype][x];
                                    }
                                    if (transactions[currentuser][currenttype][x] > highTrans){  // Determine highest transaction
                                          highTrans = transactions[currentuser][currenttype][x];
                                    }
                                    totalTrans = totalTrans + transactions[currentuser][currenttype][x];  // Calculate total of all transactions
                              }

                              aveTrans = totalTrans / 5;  // Calculate average transactions

                              // Format the calculated transactions for proper display
                              String myAverage = myNumberFormat.format(aveTrans);
                              String myTotal = myNumberFormat.format(totalTrans);

                              // Output ResultsS
                              System.out.println("Statistics");
                              System.out.println("----------");
                              System.out.println("Highest\t" + highTrans);
                              System.out.println("Lowest\t" + lowTrans);
                              System.out.println("Total\t" + myTotal);
                              System.out.println("Average\t" + myAverage);                              
                              
                              valid = yes;
                              break;
                        }
                  case 84 : // T Display Transaction Report
                        {
                              // currentuser = currentuser + 1;
                              //this syncronizes the item id in the array with the user id
                              // EAG use the customerid[currentuser] to display the users ID

                              System.out.print("Detail Listing\n");
                              System.out.print("--------------\n");
                              System.out.print("Transaction\t Amount\t Balance\n");
                              System.out.print("--------------------------------\n");
                              System.out.print(accounttype[currenttype] +" Account\n");
                              System.out.print("User ID:\t" + customerid[currentuser] + "\n");
                              System.out.print("Starting balance:\t" + accountvalue[currentuser][currenttype] + "\n");

                              double varbalance = accountvalue[currentuser][currenttype];
                              String varstate;

                              for (int x=0; x < 5; x++){
      
                                    if (transactions[currentuser][currenttype][x] < 0){
                                          varstate="withdrawl";
                                    }else{
                                          varstate="deposit\t";
                                    }
      
                                    varbalance = (varbalance + transactions[currentuser][currenttype][x]);

                                    System.out.print(varstate + "\t" + transactions[currentuser][currenttype][x] + "\t" + myNumberFormat.format(varbalance) + "\n");

                              }

                              }
      
                              valid = yes;
                              break;
                        }
                  case 81 : // Q Quit
                        {
                              valid = yes;
                              break;
                        }

                default  :
                        {
                              System.out.print("\nERROR; Please select one of the menu item (I,N,A,D,S,T,Q)\n");
                              valid = no;      
                        }  
                  } // End of switch
            }// End of valid while loop                  

                        try
                        {      
                              System.out.println(" \nHit Enter Key to continue!\n" );      

                        while(System.in.read() != 10)
                        {

                        }
                        }
                        catch(Exception error)
                        {
                              System.out.println(" \nbad input! bad!\n");
                        }
            }// End of exit while loop
      System.out.println("\nThank you for using The learning Team Bank");
      System.out.println("On-line transaction manager\n");
      }
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13720500
BTW, you have a problem with brackets there:
OnLineBank.java:260: orphaned case
               case 81 : // Q Quit

Maybe you want to replace:
}
     
                         valid = yes;
                         break;
                    }

with
     
                         valid = yes;
                         break;
}


Ok, I see that your want to display results for a specific user.
Then how to output should look  like? is it like this:

Saving:
-10.34, -232.22, 255.23, 123.23, -43.23
12.32, 2.87, -35.48, 49.44, 55.44

Checking:
....

Or what?
0
 

Author Comment

by:chunkyshu
ID: 13720642
Yes pretty much, I would like the output to be listed like you have it but in a descending order...

Saving:
Transactions listed in descending order:
-232.22
-43.23
-35.48
-10.34
2.87
12.32
49.44
55.44
123.23
255.23

Checking:
....
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1500 total points
ID: 13720856
Try this:
 case 68 : // D Display Descending Transactions List
                    {
                                    for (int i = 0; i < accounttype.length; i++)
                                    {
                                          System.out.println("Account: " + accounttype[i]);
                                          double trans[] = new double[transactions[currentuser][i].length];
                                          System.arraycopy(transactions[currentuser][i], 0, trans, 0, trans.length);
                                          java.util.Arrays.sort(trans);
                                          for (int j = 0; j < trans.length; j++)
                                                System.out.println("\t" + trans[j]);
                                    }
                         valid = yes;
                         break;
                    }
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13722738
:-)
0
 

Author Comment

by:chunkyshu
ID: 13726486
Perfect solution ~ Thanks!
0
 

Author Comment

by:chunkyshu
ID: 13745856
aozarov:
I've been playing around with your code because I'm trying to sort the data of the current account displayed, not all three accounts. Can you help me with that? Right now it shows this as an output:

Account: Savings
     -232.22
     -43.23
     -10.34
     123.23
     255.23
Account: Checking
     -35.48
     2.87
     12.32
     49.44
     55.44
Account: Money Market
     -53.29
     12.22
     35.03
     41.97
     212.22

Instead, I'd like it to say one list for current account shown:

Transactions listed in descending order:
-53.29
12.22
35.03
41.97
212.22

Can you help me with that? Let me know if I'm not clear... thanks in advance!


0
 
LVL 15

Expert Comment

by:aozarov
ID: 13745947
In that case I think you need to replace:
case 68 : // D Display Descending Transactions List
                    {
                              for (int i = 0; i < accounttype.length; i++)
                              {
                                   System.out.println("Account: " + accounttype[i]);
                                   double trans[] = new double[transactions[currentuser][i].length];
                                   System.arraycopy(transactions[currentuser][i], 0, trans, 0, trans.length);
                                   java.util.Arrays.sort(trans);
                                   for (int j = 0; j < trans.length; j++)
                                        System.out.println("\t" + trans[j]);
                              }
                         valid = yes;
                         break;
                    }
with
case 68 : // D Display Descending Transactions List
                    {
                                   System.out.println("Account: " + accounttype[currenttype]);
                                   double trans[] = new double[transactions[currentuser][currenttype].length];
                                   System.arraycopy(transactions[currentuser][currenttype], 0, trans, 0, trans.length);
                                   java.util.Arrays.sort(trans);
                                   for (int j = 0; j < trans.length; j++)
                                        System.out.println("\t" + trans[j]);
                                   valid = yes;
                                   break;
                    }
0
 

Author Comment

by:chunkyshu
ID: 13745960
You are GOOD! That was such a simple change, you should have seen the mess I made out of it before I  asked for help!

Thanks so much, you're a life saver!

=)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13745974
:-)
0
 

Author Comment

by:chunkyshu
ID: 13765160
Hey I have one last question for you... the code that you recommended to me that I accepted shows the transactions listed in ascending order (smallest to biggest), how would I sort them in descending order... I thought that we did have it in descending order.. but then it was pointed out to me that I was incorrect.
Is it a simple change? I tried changing this line:
for (int j = 0; j < trans.length; j++)
to
for (int J = 0; j< trans.length; j--)

Isn't that the way you change an ascending list to a descending list?

-C ;-)

0
 
LVL 15

Expert Comment

by:aozarov
ID: 13766138
Yes, that would be the simplest way to achive it (though you are not really sorting the array this way but rather traversing it in reverse order).
BTW,
>> for (int J = 0; j< trans.length; j--)
should be
for (int j = trans.length - 1; j >=0; j--)
0
 

Author Comment

by:chunkyshu
ID: 13766296
I was experimenting with Comparators just now and your tip provided me with exactly what I needed, and much simpler.. again.. thanks.

Can I somehow give more points?

-C
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13768330
:-)

>> Can I somehow give more points?
Not sure, but that is fine. Thanks :-)
0
 

Author Comment

by:chunkyshu
ID: 13768714
Again.. thanks!
=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question