My program is working fine, but when it opens the JOptionPane.showInputDialog to enter a grade the text field for box has a highlighted -1 in it. I'm pretty clueless and searches came up with nothing. Thank you for your time!
import javax.swing.JOptionPane;
public class MeanDeviCalc extends javax.swing.JFrame {
//set array size
private double[] gradeArray = new double[25];
//intialize number of grades
private int GradeTotal = 0;
/**
* Creates new form MeanDeviCalc
*/
public MeanDeviCalc() {
initComponents();
}
//get function for mean
public double getAverage(double[] gradeArray, int numElem) {
//intialize total with 0
double total = 0;
for (int i = 0; i < numElem; i++)
{
//add one for total when grade inputed
total=total+gradeArray[i];
}
//divide for mean
return (total/numElem);
}
//get function for standard deviation
public double getstddev(double[] gradeArray, int numElem, double average) {
//intialize total with 0
double total = 0;
for (int i = 0; i < numElem; i++)
{
//standard deviation
total = total + Math.pow((gradeArray[i] - average), 2);
}
return Math.sqrt(total / numElem);
}
boolean exitloop = false;
do {
String gradeInput = JOptionPane.showInputDialog(
"Enter Grade",
JOptionPane.PLAIN_MESSAGE);
// When we receive empty/null input, we're done entering grades
if (gradeInput == null || gradeInput.length() == 0)
exitloop=true;
if(!exitloop){
double gradeValue = 0;
if (GradeTotal == 25) {
// max array size check
JOptionPane.showMessageDialog(this,
"You've already entered the maximum of 25 grades.",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
try {
gradeValue = Double.parseDouble(gradeInput);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,
"Your input must be numeric!","Bad Data!",JOptionPane.ERROR_MESSAGE);
}
// Put grade in the array update total
gradeArray[GradeTotal] = gradeValue;
GradeTotal++;
// Add to grade total
txtGradeNumber.setText(Integer.toString(GradeTotal));
double gradeAverage = getAverage(gradeArray, GradeTotal);
txtMean.setText(Double.toString(gradeAverage));
double standardDeviation = getstddev(gradeArray, GradeTotal, gradeAverage);
txtStdDev.setText(Double.toString(standardDeviation));}
} while (GradeTotal < 25 && !exitloop) ;
Because you're using public static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue) and JOptionPane.PLAIN_MESSAGE is set to -1 (public static final int PLAIN_MESSAGE = -1;)
I think you meant to use public static String showInputDialog(Component parentComponent, Object message, String title, int messageType)
instead
Related
Can someone see why the user can enter more than 27 apple, blueberry, or peanut pies? Even after declaring a final int for the max number of each type of pie.
The object here is to continually prompt the user for type of pie until the user wants to quit. Each time one of the valid inputs is entered it is stored in it's own array. After the user has indicated they are finished, calculations are done and a message is printed.
import javax.swing.JOptionPane;
public class CalcPieProfit {
public static void main(String[] args) {
final int MAX_PER_TYPE = 27;
int appleTotal = 0;
int blueberryTotal = 0;
int peanutTotal = 0;
String typeOfPie = getPieType();
while (!typeOfPie.equalsIgnoreCase("q")) {
if (typeOfPie.equalsIgnoreCase("apple")) {
String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE);
appleTotal++;
}
else if (typeOfPie.equalsIgnoreCase("blueberry")) {
String[] blueberryArray = fillBlueberry(typeOfPie, MAX_PER_TYPE);
blueberryTotal++;
}
else if (typeOfPie.equalsIgnoreCase("peanut")) {
String[] peanutArray = fillPeanut(typeOfPie, MAX_PER_TYPE);
peanutTotal++;
}
typeOfPie = getPieType();
}
if (typeOfPie.equalsIgnoreCase("q")) {
int totalPies = calcTotalPies(appleTotal, blueberryTotal, peanutTotal);
double profit = calcProfit(appleTotal, blueberryTotal, peanutTotal);
printReport(totalPies, appleTotal, blueberryTotal, peanutTotal, profit);
}
}
public static String getPieType() {
String pieType;
do {
try {
pieType = JOptionPane.showInputDialog("Enter a pie type:");
}
catch (NumberFormatException e) {
pieType = "";
}
if (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") &&
!pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q")) {
JOptionPane.showMessageDialog(null, "Enter 'apple', 'blueberry', 'peanut', or 'q' only.");
}
} while (!pieType.equalsIgnoreCase("apple") && !pieType.equalsIgnoreCase("blueberry") &&
!pieType.equalsIgnoreCase("peanut") && !pieType.equalsIgnoreCase("q"));
return pieType;
}
public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) {
String[] appleArray = new String[MAX_PER_TYPE];
for (int i = 0; i < appleArray.length; i++) {
appleArray[i] = typeOfPie;
}
return appleArray;
}
public static String[] fillBlueberry(String typeOfPie, int MAX_PER_TYPE) {
String[] blueberryArray = new String[MAX_PER_TYPE];
for (int i = 0; i < blueberryArray.length; i++) {
blueberryArray[i] = typeOfPie;
}
return blueberryArray;
}
public static String[] fillPeanut(String typeOfPie, int MAX_PER_TYPE) {
String[] peanutArray = new String[MAX_PER_TYPE];
for (int i = 0; i < peanutArray.length; i++) {
peanutArray[i] = typeOfPie;
}
return peanutArray;
}
public static int calcTotalPies(int appleTotal, int blueberryTotal, int peanutTotal) {
int total = appleTotal + blueberryTotal + peanutTotal;
return total;
}
public static double calcProfit (int appleTotal, int blueberryTotal, int peanutTotal) {
final double APPLE_PROFIT = 5.94;
final double BLUEBERRY_PROFIT = 5.89;
final double PEANUT_PROFIT = 6.95;
double profit = (APPLE_PROFIT * appleTotal) + (BLUEBERRY_PROFIT * blueberryTotal) +
(PEANUT_PROFIT * peanutTotal);
return profit;
}
public static void printReport(int totalPies, int appleTotal, int blueberryTotal, int peanutTotal, double profit) {
if (totalPies > 0) {
JOptionPane.showMessageDialog(null,
"Pie Report\n\n" +
"Total pies: " + totalPies +
"\nTotal of apple pie: " + appleTotal +
"\nTotal of blueberry pie: " + blueberryTotal +
"\nTotal of peanut butter pie: " + peanutTotal +
"\nTotal profit: $" + String.format("%.2f", profit));
}
else {
JOptionPane.showMessageDialog(null, "Enjoy your day off.");
}
}
}
You are not really using the String[]s appleArray, blueberryArray and peanutArray - they are created in their respective method but not used anywhere else. For calculating the profits, you are (rightfully) only the total variables.
Instead of
if (typeOfPie.equalsIgnoreCase("apple")) {
String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE);
appleTotal++;
}
you should do something like
if (typeOfPie.equalsIgnoreCase("apple")) {
if (appleTotal >= MAX_PER_TYPE) {
JOptionPane.showMessageDialog(null, "Too many apples.");
} else {
appleTotal++;
}
}
(and the same for other pie types).
You're redeclaring the pie arrays each time you go to add them.
public static String[] fillApple(String typeOfPie, int MAX_PER_TYPE) {
String[] appleArray = new String[MAX_PER_TYPE];
for (int i = 0; i < appleArray.length; i++) {
appleArray[i] = typeOfPie;
}
return appleArray;
}
Each time you call this method, a new "appleArray" is generated. If you want it to persist between calls to this method, declare the appleArray as private static outside of the loop, and reference that instead.
The program I am working on is a simple shipping program. What I am having difficulty with is populating a multidimensional array factoring in certain variables.
Example
320 items need to be shipped out to 1 receiver using different box sizes.
XL can hold 50 items
LG can hold 20 items
MD can hold 5 items
SM can hold 1 items
Use the least number of boxes so far.
Code
This is my code so far.
import java.util.Scanner;
public class Shipping {
public static void main(String [] args) {
Scanner kbd = new Scanner(System.in);
final int EXTRA_LARGE = 50;
final int LARGE = 20;
final int MEDIUM = 5;
final int SMALL = 1;
String sBusinessName = "";
int iNumberOfGPS = 0;
int iShipmentCount = 0;
displayHeading(kbd);
iShipmentCount = enterShipments(kbd);
int[][] ai_NumberOfShipments = new int [iShipmentCount][4];
String[] as_BusinessNames = new String [iShipmentCount];
for (int iStepper = 0; iStepper < iShipmentCount; iStepper++) {
sBusinessName = varifyBusinessName(kbd);
as_BusinessNames[iStepper] = sBusinessName;
iNumberOfGPS = varifyGPS(kbd);
calculateBoxes(ai_NumberOfShipments[iStepper],iNumberOfGPS, EXTRA_LARGE, LARGE, MEDIUM, SMALL);
}
//showArray(as_BusinessNames);
}
public static void displayHeading(Scanner kbd) {
System.out.println("Red River Electronics");
System.out.println("Shipping System");
System.out.println("---------------");
return;
}
public static int enterShipments(Scanner kbd) {
int iShipmentCount = 0;
boolean bError = false;
do {
bError = false;
System.out.print("How many shipments to enter? ");
iShipmentCount = Integer.parseInt(kbd.nextLine());
if (iShipmentCount < 1) {
System.out.println("\n**Error** - Invalid number of shipments\n");
bError = true;
}
} while (bError == true);
return iShipmentCount;
}
public static String varifyBusinessName(Scanner kbd) {
String sBusinessName = "", sValidName = "";
do {
System.out.print("Business Name: ");
sBusinessName = kbd.nextLine();
if (sBusinessName.length() == 0) {
System.out.println("");
System.out.println("**Error** - Name is required\n");
} else if (sBusinessName.length() >= 1) {
sValidName = sBusinessName;
}
} while (sValidName == "");
return sValidName;
}
public static int varifyGPS(Scanner kbd) {
int iCheckGPS = 0;
int iValidGPS = 0;
do {
System.out.print("Enter the number of GPS receivers to ship: ");
iCheckGPS = Integer.parseInt(kbd.nextLine());
if (iCheckGPS < 1) {
System.out.println("\n**Error** - Invalid number of shipments\n");
} else if (iCheckGPS >= 1) {
iValidGPS = iCheckGPS;
}
} while(iCheckGPS < 1);
return iValidGPS;
}
public static void calculateBoxes(int[] ai_ToFill, int iNumberOfGPS) {
for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++)
}
//public static void showArray( String[] ai_ToShow) {
// for (int iStepper = 0; iStepper < ai_ToShow.length; iStepper++) {
// System.out.println("Integer at position " + iStepper + " is " + ai_ToShow[iStepper]);
// }
//}
}
Change your definition of calculateBoxes() to also take an array that represents the volume of each of the boxes (in your case this will be {50, 20, 5, 1}:
public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
// for each box size
for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
// while the remaining items to pack is greater than the current box size
while(iNumberOfGPS >= boxVolumes[iStepper]) {
// increment the current box type
ai_ToFill[iStepper]++;
// subtract the items that just got packed
iNumberOfGPS -= boxVolumes[iStepper];
}
}
}
Another way of calculating this (using / and % instead of a while loop) would be:
public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
// for each box size
for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
if(iNumberOfGPS >= boxVolumes[iStepper]) {
// calculate the number of boxes that could be filled by the items
ai_ToFill[iStepper] = iNumberOfGPS/boxVolumes[iStepper];
// reset the count of items to the remainder
iNumberOfGPS = iNumberOfGPS%boxVolumes[iStepper];
}
}
}
I am trying to get this code to work and my problem lies in getting the return statement correct so it will print the correct results. Any help is greatly appreciated. The problem lies in getting it to return the correct value. When I did have it working it would return a value of 0.
public static void main (String []args){
int famIncome, numChildren, asstTotal, asstAmount;
numChildren = userChildren();
famIncome = userIncome();
asstTotal = determineAsst(numChildren, famIncome);
System.out.println(asstTotal);
}
public static int userChildren (){
int children = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Your Number of Children: ");
children = keyboard.nextInt();
return children;
}
public static int userIncome (){
int income = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Your Family Income: ");
income = keyboard.nextInt();
return income;
}
public static void displayResults(int famIncome, int numChildren){
System.out.println("Your Income is: " + famIncome + " " + "Children: " + numChildren);
}
public static int determineAsst (int userIncome, int numChildren){
if(userIncome > 25000 && numChildren > 2){
int asstTotal = 0;
asstTotal = numChildren * 1000;
return asstTotal;
}
return asstTotal;
}
}
In order to compile, this
public static int determineAsst (int userIncome, int numChildren){
if(userIncome > 25000 && numChildren > 2){
int asstTotal = 0;
asstTotal = numChildren * 1000;
return asstTotal;
}
return asstTotal;
}
must be changed to this:
public static int determineAsst (int userIncome, int numChildren){
int asstTotal = 0;
if(userIncome > 25000 && numChildren > 2){
asstTotal = numChildren * 1000;
}
return asstTotal;
}
In your original code, the asstTotal variable is not declared until inside the if block. This means that once the if block exits, that variable no longer exists. It's in the wrong scope, and therefore, the return statement will not compile.
Also, as mentioned by #donfuxx, the return statement inside the if-block is unnecessary. It works, but it's redundant.
Does this solve your problem?
rewrite method like this:
public static int determineAsst (int userIncome, int numChildren){
int asstTotal = 0; //here!
if(userIncome > 25000 && numChildren > 2){
//int asstTotal = 0; not here!
asstTotal = numChildren * 1000;
//return statement here not necessary
}
return asstTotal;
}
I have a 2D Array called sectionArray which is of type Student class. Each student has a name and an array[] of 5 grades for exam scores. These names and scores are divided into to sections in a file that must be read. I keep getting a nullPointer on my sectionArray no matter what I change. Thank you for any suggestions.
import java.util.*;
import java.io.*;
public class ProgressReport {
private Student[][] sectionArray;// each student has a name,
// grade, average,
// and the array of scores
private File file;
private Scanner inputFile;
public ProgressReport() throws IOException {
sectionArray = new Student[2][];
file = new File("Lab5A.in.txt");
inputFile = new Scanner(file);
}
/**
*
* #return the values in the sectionArray
*/
public Student[][] getSectionArray() {
return sectionArray;
}
/**
* initialize sectionArray and set the values
*
* #param sectionArray
* passed 2D array of Students
*/
public void setSectionArray(Student[][] sectionArray) {
for (int i = 0; i < sectionArray.length; i++) {
for (int j = 0; j < sectionArray[i].length; j++) {
this.sectionArray[i][j] = sectionArray[i][j];
}
}
}
/**
* reads from the file and creates new Students
*
* #throws IOException
*/
public void readInputFile() throws IOException {
int colNum = 0;
int section = 0;
String name = " ";
int[] grades = new int[5];
while (inputFile.hasNext()) {
colNum = inputFile.nextInt();// gets size of row
sectionArray[section] = new Student[colNum];// initialize array
// iterates through colNum amount of times
for (int j = 0; j < colNum; j++) {
name = inputFile.next();// gets next name in column
for (int i = 0; i < 5; i++) {
// stores scores for that name
grades[i] = inputFile.nextInt();
}
// creates new Student with name and grades
sectionArray[section][j] = new Student(name, grades);
section++;
}
}
// passes the values in sectionArray
setSectionArray(sectionArray);
}
}
My student class looks like this:
public class Student {
private String name = " "; // Store the name of the student
private char grade; // Store the letter grade
private double average; // Store the average score
private int[] scores; // Store the five exam scores
public Student() {
grade = ' ';
average = 0.0;
}
public Student(String name, int[] score) {
this.name = name;
scores = new int[5];
for (int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
for (int i = 0; i < scores.length; i++) {
this.scores[i] = score[i];
}
}
// getters
public String getName() {
return name;
}
public char getGrade() {
return grade;
}
public double getAverage() {
return average;
}
// think about changing this to return a different format
public int[] getScores() {
return scores;
}
// setters
public void setScore(int[] scores) {
}
public void setName(String name) {
this.name = name;
}
public void setGrade(char grade) {
this.grade = grade;
}
public void setAverage(double average) {
this.average = average;
}
/**
* determine the average of the five test scores for each student
*/
public void calculateAverage() {
double total = 0;
double average = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
average = total / scores.length;
setAverage(average);
}
/**
* Determine the student's letter grade based on average of test scores
*/
public void calculateGrade() {
double average = 0;
average = getAverage();
if (average <= 100 && average >= 90) {
setGrade('A');
} else if (average <= 89 && average >= 80) {
setGrade('B');
} else if (average <= 79 && average >= 70) {
setGrade('C');
} else if (average <= 69 && average >= 60) {
setGrade('D');
} else if (average <= 59 && average >= 0) {
setGrade('F');
}
}
public String toString() {
return getName() + " " + getAverage() + " " + getGrade();
}
}
The array you are trying to write is initialized with sectionArray = new Student[2][];. This will create a matrix (2D array) with 2 columns and only one row, and then you try to set your new values on this array. If you already know the size of the matrix you are going to read from the file, then initialize it with the correct values.
Anyway, I did not get why you are trying to use a 2D array for this. If I understood correctly the purpose of your code, you should be using a List instead to store the read data, and since it has a dynamically increasing size you wouldn't have to bother with controlling the indexes like you do with the array. Take a look at this tutorial to learn how to use lists.
I'm trying to convert my single dimensional array into a multidimensional array. Convert the existing Interest Calculator Batch application from several single dimensional arrays
into 1 multi-dimensional array. Use the following data type: double[][] values;
Hint: The maximum number of data sets(5) will be your row and the each data array (from the original InterestCalculatorBatch)(7)will be a column.
2.All data will be contained within a
single multi-dimensional array.
3. Modify the sortBySimple and displayInterest methods to accept a single multi-dimensional array instead of multiple single dimensional arrays
I fixed the displayInterest to accept a single multi dimensional array. I am just confused if I have the sortBySimple set up correctly and when the program calculates the interest if I need to fix those or keep them as is. Thank you for the help.
import java.util.Scanner;
public class InterestCalculatorBatchMDA {
public static void main(String[] args )
{
int cnt = 0;
double[][] arrPrincipalAmt = new double[5][7];
double[][] arrInterestRate = new double[5][7];
double[][] arrTerm = new double[5][7];
double[][] arrSimple = new double[5][7];
double[][] arrCompoundMonthly = new double[5][7];
double[][] arrCompoundDaily = new double[5][7];
double[][] arrCompoundWeekly = new double[5][7];
do{
arrPrincipalAmt[cnt] = getPrincipalAmount(1);
arrInterestRate[cnt] = getInterestRate(1);
arrTerm[cnt] = getTerm(1);
arrSimple[cnt] = round(calculateSimpleInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt]),5);
arrCompoundMonthly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt],arrTerm[cnt] ,12.0 ),5);
arrCompoundWeekly[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 52.0 ),5);
arrCompoundDaily[cnt] = round(calculateCompoundInterest(arrPrincipalAmt[cnt], arrInterestRate[cnt], arrTerm[cnt], 365.0 ),5);
cnt++;
}while (cnt < 5 && askYesNo("Enter another set of data (Yes/No):"));
displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt);
sortBySimple(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt);
displayInterest(arrPrincipalAmt,arrInterestRate,arrTerm,arrSimple,arrCompoundMonthly,arrCompoundWeekly,arrCompoundDaily,cnt);
}
/** Round **/
public static double round(double numb1, double numb2) {
double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
return round;
}
/** Calculate Simple **/
public static double calculateSimpleInterest(double numb1, double numb2, double numb3) {
double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0));
return calculateSimpleInterest;
}
/** Calculate Compounded Daily **/
public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) {
double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1;
return calculateCompoundInterest;
}
/** Get principal amount **/
public static double getPrincipalAmount(double numb1) {
Scanner input = new Scanner(System.in);
double numb2 = 1;
do{System.out.print("Enter Loan Amount: ");
numb2 = input.nextDouble();
if(numb2 > 0);
else{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
}
}while (numb2 < 0);
return numb2;
}
/** Get interest rate **/
public static double getInterestRate(double numb1) {
Scanner input = new Scanner(System.in);
double numb2=1;
do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
numb2 = input.nextDouble();
double getInterestRate = 0;
if (numb2 >= 0 && numb2 <= 100)
getInterestRate = numb2;
else{
System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2);
}
}while (numb2 <= 0 || numb2 >= 100);
return numb2;
}
/** Get term **/
public static double getTerm(double numb1) {
Scanner input = new Scanner(System.in);
double numb2=1;
do{System.out.print("Enter the Term (in months): ");
numb2 = input.nextInt();
double getTerm = 0;
if (numb2 > 0)
getTerm = numb2;
else{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
}
}while (numb2 <= 0);
return numb2;
}
/** Sort by simple interest **/
public static void sortBySimple(double[][] arrPrincipalAmt ,double[][] arrInterestRate, double[][] arrTerm, double[][] arrSimple, double[][] arrCompoundMonthly, double[][] arrCompoundWeekly, double[][] arrCompoundDaily, double count){
for(int i = 0;i<count;i++)
{
for(int j=i+1; j<count;j++)
{
if(arrSimple[i][j] < arrSimple[i][j])
{
double temp = arrSimple[i][j];
arrSimple[i][j] = arrSimple[i][j];
arrSimple[i][j] = temp;
double temp1 = arrPrincipalAmt[i][j];
arrPrincipalAmt[i][j] = arrPrincipalAmt[i][j];
arrPrincipalAmt[i][j] = temp1;
double temp2 = arrInterestRate[i][j];
arrInterestRate[i][j] = arrInterestRate[i][j];
arrInterestRate[i][j] = temp2;
double temp3 = arrTerm[i][j];
arrTerm[i][j] = arrTerm[i][j];
arrTerm[i][j] = temp3;
double temp4 = arrSimple[i][j];
arrSimple[i][j] = arrSimple[i][j];
arrSimple[i][j] = temp4;
double temp5 = arrCompoundMonthly[i][j];
arrCompoundMonthly[i][j] = arrCompoundMonthly[i][j];
arrCompoundMonthly[i][j] = temp5;
double temp6 = arrCompoundDaily[i][j];
arrCompoundDaily[i][j] = arrCompoundDaily[i][j];
arrCompoundDaily[i][j] = temp6;
double temp7 = arrCompoundWeekly[i][j];
arrCompoundWeekly[i][j] = arrCompoundWeekly[i][j];
arrCompoundWeekly[i][j] = temp7;
}
}
}
}
/** Display Interest **/
public static void displayInterest(double[][] amt ,double[][] interest, double[][] term, double[][] simple, double[][] monthly, double[][] weekly, double[][] arrCompoundDaily, int count){
int i=0;
System.out.println("[Line #] [Principal Amount] [Interest Rate] [Term] [Simple Interest] [Compound Monthly] [Compound Weekly] [Compound Daily]");
do{
System.out.print((i+1)+" ");
System.out.print(amt[i][i]+" ");
System.out.print(+interest[i][i]+" ");
System.out.print(+ term[i][i]+" ");
System.out.print(+simple[i][i]+" ");
System.out.print(+monthly[i][i]+" ");
System.out.print(+weekly[i][i]+" ");
System.out.println(+arrCompoundDaily[i][i]);
i++;
}while(i < count);
}
/**ask yes or no **/
public static boolean askYesNo(String question) {
Scanner input = new Scanner(System.in);
String enteredText;
boolean isAnswerValid;
do{
isAnswerValid = false;
System.out.println(question);
enteredText = input.nextLine();
if (enteredText.length() > 0)
{
enteredText = enteredText.toUpperCase();
if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N"))
{
isAnswerValid = true;
}
}
if(isAnswerValid == false)
{
System.out.println("Please enter 'Yes' or 'No'?");
}
} while(isAnswerValid == false);
if(enteredText.equals("YES") || enteredText.equals("Y"))
{
return true;
}
return false;
}
}
Your code looks like an attempt to write C in Java. Java is an OO language, and you will have much more success if you design and write your programs in to make use of that. Data structures that are "smeared" across multiple arrays like that are fundamentally awkward to deal with.
The first thing you need to do to remedy this is to create a class that represents an entry in the arrays.
Trying to fix the program while retaining the current "smeared" data structure design is ... to be blunt ... a waste of time.