# Stuck on 2D array issue

• 02-20-2013, 03:05 PM
scottdaking
Stuck on 2D array issue
Greetings,

I'd greatly appreciate any assistance with this issue. I am writing a program (in NetBeans) that tracks and calculates tutoring sessions. The user will input the length of each tutoring session and the rate charged through a GUI. These inputs need to be stored in a 2D array and I'm unsure how to properly get the input of both variables into the array.

I can provide my code later this evening (I'm at work). I wanted to get the question out because this is driving me batty! Thank you, in advance, for your time and assistance.

Scott :8):
• 02-20-2013, 03:25 PM
PhHein
Re: Stuck on 2D array issue
We'll wait for yopur code.
• 02-25-2013, 08:11 PM
scottdaking
Re: Stuck on 2D array issue

So, I got some much needed assistance through another thread, and I actually got most of the program working properly. The last issue I can't seem to code correctly is determining the minimum wage value from the array inputs. I have two primary methods for the user input and to run a report. Although the instructions indicate the minimum wage should be displayed when the report method is run, I figured it would make more sense to determine the minimum wage in the input method. I'm still trying to get the hang of the logic, so...can anyone clue me in on how (and where) to get the minimum values from this array? Here's the code:

Code:

import java.awt.Component;
import javax.swing.JOptionPane;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author King
*/
public class TutorUI extends javax.swing.JFrame {

double[][] tutorArray = new double [20][2];
int i = 0;
double totalMinutes = 0;
double totalHours = 0;
double totalWages = 0;
double averageWage = 0;
double minimumWage = 0;
/**
* Creates new form TutorUI
*/
public TutorUI() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
sessionLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
dataTextArea = new javax.swing.JTextArea();
earningsLabel = new javax.swing.JLabel();
sessionLength = new javax.swing.JTextField();
enterButton = new javax.swing.JButton();
runReportButton = new javax.swing.JButton();
quitButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

sessionLabel.setText("Enter length of session in seconds:");

dataTextArea.setColumns(20);
dataTextArea.setRows(5);
jScrollPane1.setViewportView(dataTextArea);

earningsLabel.setText("Enter how much received in dollars and cents:");

sessionLength.setText("Session Length");
public void actionPerformed(java.awt.event.ActionEvent evt) {
sessionLengthActionPerformed(evt);
}
});

public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

enterButton.setText("Enter");
public void actionPerformed(java.awt.event.ActionEvent evt) {
enterButtonActionPerformed(evt);
}
});

runReportButton.setText("Run Report");
public void actionPerformed(java.awt.event.ActionEvent evt) {
runReportButtonActionPerformed(evt);
}
});

quitButton.setText("Quit");
public void actionPerformed(java.awt.event.ActionEvent evt) {
quitButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
);
jPanel1Layout.setVerticalGroup(
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);
layout.setVerticalGroup(
);

pack();
}// </editor-fold>

private void sessionLengthActionPerformed(java.awt.event.ActionEvent evt) {
}

}

private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
double time = Double.parseDouble(sessionLength.getText());
//each time user enters the time spent and earnings received for each
//tutor session, those values will be added to the array.

//positive input warning
if (time <= 0 || earnings <= 0) {
Component frame = null;
JOptionPane.showMessageDialog(frame,"Values for time and earnings must be positive numbers", "Invalid Input Warning", JOptionPane.WARNING_MESSAGE);
}
else {
tutorArray[i][0] = time;
tutorArray[i][1] = earnings;

totalMinutes += time;
totalWages += earnings;

//display time and earnings so far
dataTextArea.append("Minutes =" + " " + time + "      " + "Earnings = \$" + earnings + "\n");

i++;
}

}

private void runReportButtonActionPerformed(java.awt.event.ActionEvent evt) {

//determine the minimum wage
//minimumWage = tutorArray[0][0];
for (int s = 0; s < tutorArray.length - 1; s++) {
//find position of minimum value
int minPos = s;
for (int t = s + 1; t < tutorArray.length; t++){
if (tutorArray [t][1] < tutorArray[s][1]) {
minPos = t;
//minimumWage = tutorArray [s][1];
}
}
}
String minPos = null;
dataTextArea.append("\nThe position of the minimum value is:" + "  " + minPos + "\n");
//convert total minutes of tutoring to hours
totalHours = totalMinutes / 60;

//then calculate average wage per hour
averageWage = totalWages / totalHours;

//print report
dataTextArea.append("\n\n**************************");
String line;
line = String.format("\n\nReport of your wages to Date:" + "  " + "%7.2f", totalWages);
dataTextArea.append(line);
line = String.format("\n\nTotal Minutes Spent Tutoring =" + "  " + "%7.2f", totalMinutes);
dataTextArea.append(line);
line = String.format("\nTotal Earnings =" + "  " + "%7.2f", totalWages);
dataTextArea.append(line);
line = String.format("\nAverage Per Hour Wage = \$" + "  " + "%7.2f", averageWage);
dataTextArea.append(line);

//determine whether average wages per hour are average, above or below average, then print

line = String.format("\n\nMinimum Wage is currently = \$" + minimumWage);
dataTextArea.append(line);
}

private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TutorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TutorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TutorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TutorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TutorUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea dataTextArea;
private javax.swing.JLabel earningsLabel;
private javax.swing.JButton enterButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton quitButton;
private javax.swing.JButton runReportButton;
private javax.swing.JLabel sessionLabel;
private javax.swing.JTextField sessionLength;
// End of variables declaration
}

I'm using NetBeans 7.2.1 and am very appreciative of all of the support!

Thanks so much,
Scott :(handshake):