Results 1 to 3 of 3
  1. #1
    scottdaking is offline Member
    Join Date
    Feb 2013
    Location
    Baltimore, MD
    Posts
    10
    Rep Power
    0

    Default How do I print a string and array value in the same line of text?

    Hello All,

    I'm working on a program that tracks tutoring earnings. The program uses a GUI where the user enters time and earnings for each tutoring session. The program stores the input values in a 2D array when an enter button is pushed, calculates the total time spent tutoring, avg earnings, total earnings, minimum wage earned and whether the earnings are avg, above or below. These calculations are displayed in a specific format in the GUI text box when a report button is pushed.

    Right now, I am having difficulty determining how to display a string alongside the array value. Based on the required output, each time the enter button is pushed, the time and earnings should be displayed. This is my code for the enter button method thus far:

    Java Code:
    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    
            double time = Double.parseDouble(sessionLength.getText());
            double earnings = Double.parseDouble(earningsReceived.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);
                return;
            }
            else {
                tutorArray[i][0] = time;
                tutorArray[i][1] = earnings;
                
             //determine the minimum wage   
                if (earnings < tutorArray [i][1]) {
                    minimumWage = earnings;
                }
                else {
                    minimumWage = tutorArray [i][1];
                }
                totalMinutes += time;
                totalWages += earnings;
                
             //clear text area output
             dataTextArea.setText(" ");
            
             //display time and earnings so far
             dataTextArea.append("Minutes =" + "      " + "Earnings =\n");
             i++;
            }
    This is the required output:

    Minutes = 60.0 Earnings = $10.0
    Minutes = 120.0 Earnings = $40.0

    This is my last output:

    How do I print a string and array value in the same line of text?-task-1-output-2-23-2013.png

    Any assistance will be greatly appreciated and YES, I AM Java-challenged! Thanks for noticing!

    Scott

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: How do I print a string and array value in the same line of text?

    First thing I notice:
    Java Code:
    tutorArray[i][1] = earnings;
    
    if (earnings < tutorArray [i][1]) {
    ...
    }
    If you set them equal, how can this if statement ever be true?



    Second,
    just concatenate the values to the string like this:

    Java Code:
    dataTextArea.append("Minutes = " + totalMinutes + "      " + "Earnings = " + totalWages + "\n");

  3. #3
    scottdaking is offline Member
    Join Date
    Feb 2013
    Location
    Baltimore, MD
    Posts
    10
    Rep Power
    0

    Default Re: How do I print a string and array value in the same line of text?

    Thanks for the reply and the insight, Andrew! How could I forget the rules of concatenation????

    I understand your logic as far as my "if" statement. This was my attempt to assign the minimum wage, which will be necessary for the reporting method that follows this. I'm not sure how else to code that functionality.

Similar Threads

  1. Skipe line if text in array?
    By fpena06 in forum New To Java
    Replies: 7
    Last Post: 04-27-2012, 01:13 PM
  2. Replies: 3
    Last Post: 03-08-2012, 02:09 AM
  3. how to print the String Array?
    By nirasiva in forum New To Java
    Replies: 4
    Last Post: 06-09-2011, 04:07 PM
  4. Text file into an array then Print
    By Mrussell9 in forum New To Java
    Replies: 0
    Last Post: 05-06-2011, 02:28 PM
  5. Print String from an Array
    By adityasirohi in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •