Yes, this question is regarding an assignment, but I have been killing myself for several days trying to find a solution. The programs is one that we have added to / expanded on all semester, so parts of it may be ugly, but they work.
I have a 2 dimensional String array with all of the information I need in it. I know that this information is correct because the printout of the array works. What doesn't work, however, is the Arrays.sort(array) method.
JAVA should sort in the order of the columns, so I have purposefully placed the data in column order according to how it needs to be sorted. I have tried to methods for the sort, and both compile fine but produce a run time error.
The objective of the program is to accept an employee's expense record details, convert them to US dollar amounts, and display them with certain stats. I am to accept all of the input, then show them 10 at a time until they are all displayed. Sorting is by date then by country.
There is a driver class that I am not including that calls this method. Execution of this class enters at Prob7Entry. Taking out non essential code for the sake of space. The sorting part is surrounded by "**********" near the bottom. The super.ExternalMethods simply prompt the user and return data.
import javax.swing.*;
import java.text.*;
import java.lang.*;
import java.util.*;
public class Prob7App extends Prob7Emp
{
public void Prob7Entry()
{
// Declaring Variables for use within this program
// I'm deleting all variable declarations except for the array. . .
String[][] inputArray = new String[200][9];
while (!wantContinue.equalsIgnoreCase("no"))
{
if (employeeName.equals("no"))
super.setEmployeeNameString();
super.setCountryString();
inputArray[arrayCounter][4] = super.getCountryString();
super.setConversionType();
super.setConversionAmount();
super.setTransactionType();
inputArray[arrayCounter][8] = super.getTransactionType();
boolean validData = false;
boolean validData2 = false;
do
{ // this checks a few things about the date
super.setExpenseYear(currentYear);
super.setExpenseMonth(currentMonth);
super.setExpenseDay(currentDay);
GregorianCalendar inputDateGreg = new GregorianCalendar(super.getExpenseYear(), super.getExpenseMonth()-1, super.getExpenseDay());
inputDate = inputDateGreg.getTime();
inputDateString = stdDateFormat.format(inputDate);
if (inputDateGreg.before(dateTodayGreg))
validData = true;
else
{
outputLine = "Date is a future date. \nPlease reenter";
outputTitle = "Invalid Date";
JOptionPane.showMessageDialog (null, outputLine, outputTitle, JOptionPane.ERROR_MESSAGE);
}
Date startDate = inputDateGreg.getTime();
Date endDate = dateTodayGreg.getTime();
long startDateMS = startDate.getTime();
long endDateMS = endDate.getTime();
long elapsedMS = endDateMS - startDateMS;
long elapsedDays = elapsedMS / (24 * 60 * 60 * 1000);
if (elapsedDays > 60)
{
outputLine = "Date is older than 60 days. Was it entered correctly? \nType 'Yes' or 'No'";
outputTitle = "Old Date Entered";
oldDate = JOptionPane.showInputDialog (null, outputLine);
if (oldDate.equalsIgnoreCase("yes"))
validData2 = true;
}
else
{
validData2 = true;
}
// These store the year, month, date, as strings in the array in order they need to be sorted
inputArray[arrayCounter][1] = Integer.toString(super.getExpenseYear());
inputArray[arrayCounter][2] = Integer.toString(super.getExpenseMonth()-1);
inputArray[arrayCounter][3] = Integer.toString(super.getExpenseDay());
}while ((!validData) || (!validData2));
super.setCurrencyName();
inputArray[arrayCounter][5] = super.getCurrencyName();
super.setDollarEqual();
if (super.getConversionType() == 1)
{
// If conversion is 1; divide by currency hold field
// If conversion is 1; US Dollar amount is the currency amount
// If conversion is 1; input value needs to be rounded
// foreign converted to USD
currencyAmount = (super.getConversionAmount() / super.getDollarEqual());
intCurrency = Math.round(super.getConversionAmount() * 100);
intCurrency = intCurrency / 100;
USDAmount = currencyAmount;
conversionCurrencyString = super.getCurrencyName();
}
else if (super.getConversionType() == 2)
{
// if conversion is 2; multiply by conversion rate
// if conversion is 2; US Dollar amount is the input amount
// if conversion is 2; currencyAmount needs to be rounded
// USD converted to foreign
currencyAmount = (super.getConversionAmount() * super.getDollarEqual());
intCurrency = Math.round(currencyAmount * 100);
intCurrency = intCurrency / 100;
USDAmount = super.getConversionAmount();
conversionCurrencyString = super.getCurrencyName();
}
// keep grandTotal running through ALL ENTRIES (even after printing on 10)
NumberFormat currency = NumberFormat.getCurrencyInstance();
grandTotal = grandTotal + USDAmount;
// Put numeric values in currency format
String currencyAmountString = currency.format(currencyAmount);
String USDAmountString = currency.format(USDAmount);
String intCurrencyString = currency.format(intCurrency);
inputArray[arrayCounter][6] = USDAmountString;
inputArray[arrayCounter][7] = intCurrencyString;
String grandTotalString = currency.format(grandTotal);
// Switch the name switch and save the entered name for later output
if (employeeName.equals("no"))
employeeNameSave = super.getEmployeeNameString();
employeeName = ("yes");
//Call internal method to see if there is more information
//Make sure that Yes / No is not blank
wantContinue = promptUserInternal ("Do you have more data to input? \n(Type Yes or No)", continueSwitchString);
arrayCounter = arrayCounter + 1;
}
// HERE IS THE OUTPUT PORTION OF THE PROGRAM -- AS IT STANDS
String paneTitlePlus = "Currency Conversion for International Expenses";
outputHeaders = "\nDate Country Currency Amount (Int'l) Amount ($) Payment Method";
outputLine = "Employee Name: "+ employeeNameSave + " Date: " + dateTodayString;
//*********************************************************************************************************************************
//*********************************************************************************************************************************
for (int x = 0; x < inputArray.length; x++) // <-- THIS IS THE SORT I AM TRYING NOW.
{
Arrays.sort(inputArray[x]);
}
// Arrays.sort(inputArray); <---- HAVE ALSO TRIED THIS SORT. BOTH GIVE A RUN TIME ERROR
//*********************************************************************************************************************************
//*********************************************************************************************************************************
boolean donePrinting = false;
boolean forLoopTrue = true;
int holderField = 0;
int arrayMax = 0;
while(!donePrinting) // THIS WORKS PROPERLY WHEN NOT SORTING, SO I KNOW THE ARRAY IS OK
{
int linesOfOutput = 0;
forLoopTrue = true;
outputDetail = "";
for (int i = holderField; forLoopTrue; i++)
{
int k = i;
holderField = i;
outputDetail = outputDetail + "\n" + inputArray[k][3] + "/" + inputArray[k][2] + "/" + inputArray[k][1] + " " + inputArray[k][4] + " " + inputArray[k][5] + " " + inputArray[k][7] + " " + inputArray[k][6] + " " + inputArray[k][8];
linesOfOutput = linesOfOutput + 1;
if (linesOfOutput == 10 || holderField == arrayCounter - 1)
{
forLoopTrue = false;
}
}
JOptionPane.showMessageDialog(null, outputLine + outputHeaders + outputDetail, paneTitlePlus, JOptionPane.PLAIN_MESSAGE);
if (holderField == arrayCounter - 1)
{
donePrinting = true;
}
}
System.exit(0);
}
private static String promptUserInternal (String userPrompt, String initialValue)
{
boolean goodData = false;
String outputLineInternal = "";
String stringData = "";
while (!goodData)
{
stringData = JOptionPane.showInputDialog(null, ""
+userPrompt+"", initialValue);
{
if (stringData.equals(""))
{
outputLineInternal = "Must enter 'yes' or 'no'!";
displayMessage(outputLineInternal);
}
else
goodData = true;
}
}
return stringData;
}
/**
Error message for promptUserInternal method based on data validation
*/
private static void displayMessage (String messageText)
{
String errorTitle = "An error has occurred";
messageText = messageText+"\nPlease, try again!";
JOptionPane.showMessageDialog(null, messageText, errorTitle,
JOptionPane.ERROR_MESSAGE);
}
}
Thank you for any help you can give me in sorting this array. The run time error for the simple sort is:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String;
at java.util.Arrays.mergeSort(Arrays.java:1124)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.sort(Arrays.java:1074)
at Prob7App.Prob7Entry(Prob7App.java:234)
at Prob7Driver.main(Prob7Driver.java:22)
The run time error for the looped sort is:
Exception in thread "main" java.lang.NullPointerException
at java.util.Arrays.mergeSort(Arrays.java:1124)
at java.util.Arrays.mergeSort(Arrays.java:1131)
at java.util.Arrays.sort(Arrays.java:1074)
at Prob7App.Prob7Entry(Prob7App.java:235)
at Prob7Driver.main(Prob7Driver.java:22)