Announcement

Collapse
No announcement yet.

Programming question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programming question

    I don't know if anyone can help me on this but I thought I might as well post it, if for nothing else to create another thread in a quiet forum. I'm being forced to learn Java and I have about 5 days of programming knowledge. Long story short, part of the assignment requires me to take information from two textfields (variable names payment and time) and then enter them into a multidimensional array every time someone uses the enter button on a GUI. Then, after entering in all their payment and time amounts the user will press the "Run Report" button which will take information from the 2D array and list the information followed by some calculations like average, totals for time and payment, etc. I'm pretty much good to go on everything I think except this 2D array thing. The GUI is built and I'm trying to build functionality for the "Enter" button. I'm not real sure how to properly implement this though. Here's what I have so far.

    I first declared some global variables and the array:

    public class TutorUI extends javax.swing.JFrame {

    double[][] ptArray = new double[20][2];
    double i = 0;
    double j = 0;

    Then added some coding for the enter button:

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

    /*gets the value from the two text fields and sets them equal to the
    variables*/
    double paymentValue = Double.parseDouble(payment.getText());

    double timeValue = Double.parseDouble(time.getText());

    //Checks for negative numbers and throws exception if found.
    if (paymentValue < 0)
    {
    IllegalArgumentException exception
    = new IllegalArgumentException("Invalid payment amount.");
    throw exception;

    }

    //Checks for negative numbers and throws exception if found.
    if (timeValue < 0)
    {
    IllegalArgumentException exception
    = new IllegalArgumentException("Invalid time amount.");
    throw exception;
    }

    //Sets the variables to column 1 and colum 2.
    ptArray[0][0] = timeValue;
    ptArray[0][1] = paymentValue;

    I feel like maybe I'm going about this in the wrong way since when I try to test the array by appending the data to the textarea it spits out gibberish. I've tried about 20 different ways to get this to work and this is just one of the many.

  • #2
    For starters, you want a class member
    Code:
    int index = 0;
    and change those to
    Code:
    ptArray[index][0] = timeValue;
    ptArray[index][1] = paymentValue;
    index++;
    Regarding your TextArea issue, you can't do:

    Code:
    textarea.append(ptArray);
    textarea.append takes a string as an argument. When you cast an object to a string (arrays are a subclass of Object), if there isn't a specific function for converting that object to a string it just prints out "gibberish" (actually, it's something like the memory address of the object IIRC, it should look like "[I@10b62c9"). You want to do something like the following:

    Code:
    for (int i = 0; i < index; i++) textarea.append("time: " + ptArray[i][0] + "; payment: " + ptArray[i][1] + "\n");
    Last edited by Kuciwalker; October 19, 2010, 12:31.

    Comment


    • #3
      Wow! THANK YOU! I have literally spent 18 hours trying to figure this **** out before finally just asking for help.

      Comment


      • #4
        I made some adjustments like you suggested and I understand your reasoning behind it, but I must be doing something wrong still. Clicking the enter button does nothing. Here's the code now:

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

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

        /*gets the value from the two text fields and sets them equal to the
        variables*/
        double paymentValue = Double.parseDouble(payment.getText());

        double timeValue = Double.parseDouble(time.getText());

        int index = 0;
        //Checks for negative numbers and throws exception if found.
        if (paymentValue < 0)
        {
        IllegalArgumentException exception
        = new IllegalArgumentException("Invalid payment amount.");
        throw exception;

        }

        //Checks for negative numbers and throws exception if found.
        if (timeValue < 0)
        {
        IllegalArgumentException exception
        = new IllegalArgumentException("Invalid time amount.");
        throw exception;
        }
        //Sets the variables to column 1 and colum 2.
        ptArray[index][0] = timeValue;
        ptArray[index][1] = paymentValue;
        index++;

        for (int i = 0; index < i; i++){
        reportArea.append("Time: " + ptArray[i][0] + "Payment: " + ptArray[i][1]);
        }


        }

        Comment


        • #5
          Your for loop condition is backwards. The loop is running forever.

          Why are you appending them to reportArea every time the enter button is pressed? I thought you only wanted to do that when the report was generated? If you append to reportArea each time, you don't want the for loop, you just want to say:

          Code:
          reportArea.append("Time: " + ptArray[index][0]...);
          index++;
          and of course remove the earlier index++;

          Comment


          • #6
            Ah, yeah. I just wanted to append them right now because I wanted to see if the array was working correctly. The enter button isn't supposed to do anything other than enter them into the array. The runReport then iterates through the array and appends the information to the textarea. Thanks, I'll fix it.

            Edit: Yep, you're right. Works now. THANK YOU

            Comment


            • #7
              And here I was just about to suggest you simply modify the phase variance.
              1011 1100
              Pyrebound--a free online serial fantasy novel

              Comment


              • #8
                Ok, I've run into another problem. First, the array doesn't seem to be holding more than 1 set of values, so the rows don't appear to be incrementing each time a new pair of data is entered. Second, my for loop now prints out all 20 entries since I set the row amount to 20 in the global array variable. I couldn't figure out how to set it so that the row amount was the number of data pairs entered. Here's my code so far:

                public class TutorUI extends javax.swing.JFrame {

                double[][] ptArray = new double[20][2];
                int i = 0;

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

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

                /*gets the value from the two text fields and sets them equal to the
                variables*/
                double paymentValue = Double.parseDouble(payment.getText());

                double timeValue = Double.parseDouble(time.getText());

                int index = 0;
                //Checks for negative numbers and throws exception if found.
                if (paymentValue < 0)
                {
                IllegalArgumentException exception
                = new IllegalArgumentException("Invalid payment amount.");
                throw exception;

                }

                //Checks for negative numbers and throws exception if found.
                if (timeValue < 0)
                {
                IllegalArgumentException exception
                = new IllegalArgumentException("Invalid time amount.");
                throw exception;
                }
                //Sets the variables to column 1 and colum 2.
                ptArray[index][0] = timeValue;
                ptArray[index][1] = paymentValue;
                index++;



                }

                private void runReportActionPerformed(java.awt.event.ActionEven t evt) {

                for (int i = 0; i < ptArray.length; i++){
                reportArea.append("Time: " + ptArray[i][0] + "Payment: " + ptArray[i][1]);
                }

                }

                Comment


                • #9
                  Code:
                  int index = 0;
                  ...
                  ptArray[index][0] = timeValue;
                  ptArray[index][1] = paymentValue;
                  index++;

                  index is being set to zero each time that runs so it will always be zero when enterActionPerformed is run. it needs to be handled differently, and since you're doing java you should go with Kuci's suggestion.
                  Last edited by Whoha; October 19, 2010, 15:54.

                  Comment


                  • #10
                    Why are you learning Java?
                    "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                    Ben Kenobi: "That means I'm doing something right. "

                    Comment


                    • #11
                      Move

                      int index = 0;

                      up to the def of ptArray.
                      With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

                      Steven Weinberg

                      Comment


                      • #12
                        Sorry, I didn't catch that in your rewrite earlier. "int index = 0" is a class member, i.e. you define it up at the top of the class rather than inside a method.

                        Comment


                        • #13
                          Awesome, thanks for the advice so far guys. I never would have guessed moving the index=0, makes sense though. Now the only last thing is the problem I was having from the beginning which is that I have to set the first parameter in the double statement as an integer.

                          double[][] ptArray = new double[20][2];

                          I really wanted to set it to just i or something, but this causes a type error. Any ideas on how to set this up so that the rows are created on an as needed basis instead of the static 20 I have now?

                          As far as why I'm learning Java, it's part of my B.S. in IT Security. I have two java courses I have to take in addition to the XHTML and Javascript courses I took. I really hate Java, but I bet learning Python will be a breeze after all this.

                          Comment


                          • #14
                            Originally posted by DriXnaK View Post
                            I really hate Java, but I bet learning Python will be a breeze after all this.
                            I won't speak for anyone else but after a while I started to appreciate Java. It seems like it's really limiting and annoying when you're doing things that are straightforward but it just adds structure and sense to large programs that someone else wrote. As an example, there was one time when I spent 40 minutes searching a PHP program I wrote trying to find out why a value was turning null and throwing an exception until I discovered that I had misspelled the variable $current in a function call as $curent. In Java, it would have just said that curent had never been defined anywhere and fixing the bug would have taken more like 4 seconds. But when I was taking classes that were taught in Java, I hated it.

                            That said, there's the right tool for the right job and I think Python should be very easy to learn, I learned the basics over a weekend and just picked up the more advanced stuff as I encountered it. Python's a breeze.
                            If there is no sound in space, how come you can hear the lasers?
                            ){ :|:& };:

                            Comment


                            • #15
                              If you don't have to actually use a multidimensional array, put the following at the top of the file:

                              Code:
                              import java.util.List; import java.util.ArrayList;
                              And then in your class have members:

                              Code:
                              List times = new List();
                              List payments = new List();
                              And then whenever you would add elements to the end of your array, instead do:

                              Code:
                              times.add(some number);
                              payments.add(some number);
                              And instead of ptArray[i][0] say:

                              Code:
                              times.get(i)
                              Otherwise, every time you run past the end of the array you will have to:
                              1. allocate a new, temporary, longer array (let's call it tempArray) (I suggest doubling the length each time you reallocate)
                              2. copy all of the values from ptArray to tempArray
                              3. say "ptArray = tempArray;"

                              Comment

                              Working...
                              X