Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with my code for homework assignment, I don't know how to fix it!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my code for homework assignment, I don't know how to fix it!

    import java.text.*;
    public class Rainfall extends javax.swing.JFrame {
            DecimalFormat df = new DecimalFormat("#.##");
            double monthrain = 0, sum = 0, average;
            int years, counter = 0, counter2 = 0, monthcount = 1, i = 0;
     
        public Rainfall() {
            initComponents();
     
        }
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            years = Integer.parseInt(jTextField1.getText());
            jTextArea1.setText("");
            while(years > 0){
                jTextField1.setEditable(false);
                jTextField2.setEditable(true);
                jLabel2.setText("Enter inches of rainfall for month " + monthcount + " of year " + (counter + 1) + ":");
                monthcount++;
                i++;
                monthrain = Double.parseDouble(jTextField2.getText());
                while(monthrain >= 0){
                    while(monthcount < 14){
                        monthrain = Double.parseDouble(jTextField2.getText());    
                        sum += monthrain;
                        jTextField2.setText("");
                        while(monthcount == 13 || i == 12){
                            counter++;
                            monthcount = 1;
                            i = 0;
                            if(counter == years){
                                average = sum / (years * 12);
                                jTextArea1.setText("There is a total of "  + (years * 12) + " months"
                                        + "\nTotal inches of rainfall is " + sum + " and"
                                        + "\nAverage inches of rainfall per month is " + df.format(average));
                                jTextField1.setEditable(true);
                                jTextField2.setEditable(false);
                                jTextField1.setText("");
                                jLabel2.setText("");
                                counter = 0;
                            }
                        }
                    }   
                }
                if(monthrain < 0){
                years = 0;
                counter2++;
                }
            }
            if(counter2 == 1){
                jTextArea1.setText("Invalid inches of rainfall for the month"
                        + "\nPlease start over");
                jTextField1.setText("");
                jTextField1.setEditable(true);
                jTextField2.setText("");
                jTextField2.setEditable(false);
                jLabel2.setText("");
                monthrain = 0;
                sum = 0;
                monthcount = 1;
                counter = 0;
                counter2 = 0;
            }
            else if(counter2 == 0){
                jTextArea1.setText("Invalid years\nTry again");
                jTextField1.setText("");
                jTextField1.setEditable(true);
                jTextField2.setText("");
                jTextField2.setEditable(false);
                monthrain = 0;
                sum = 0;
                monthcount = 1;
                counter = 0;
                counter2 = 0;
            }
        }
    I deleted the generated code from the pasted code above because I didn't think it was necessary.
    Alright, so the assignment is:
    Create a GUI application with Netbeans that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

    Input validation: Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.

    On my GUI, I have 2 textfields, 2 jlabels, 1 textarea, and 1 jbutton. When I enter 1 as the number of years I wanted and enter in values for month, it only loops 11 times and only added values for 11 months and displayed it. But, if I enter 2 as the number of years I wanted, it loops through all 12 months for the first year, but the second year, it only loops 11 times, same as if I were to put 1 as the number of years I wanted. So basically, it will only ask me values for 11 month for the last year. I thought adding in "i == 12" would fix it, but it didn't. Helpppp, I've been stuck on this for the past 2 hours and my head is HURTING!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    When your head hurts from staring at complex or confusing spaghetti code, SIMPLIFY.

    There's way too much going on in your actionPerformed() method and/or it's way more complicated than it needs to be. Fallback and regroup a bit, break out some of the complexity into other methods IF the complexity is necessary at all.

    If the user enters 1 as the number of years, then use a method (or a number of them) to gather the data for years * 12 months. It's not that hard, but you've pretty much made it as difficult as you possibly could.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    That doesn't really help... It basically you calling me stupid... I tried to simplify it, but I don't know how.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    It basically you calling me stupid
    Sorry you took it that way, but I didn't call you anything.

    I told you one thing to DO: SIMPLIFY,

    I told you why you should: because what you posted is too complex/complicated,

    I told you how: break out some of the code into other methods, and

    I gave you an example: gather the data for 12 months * the number of years.

    Now quit feeling sorry for yourself and do something to improve what you've posted.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    Quote Originally Posted by GregBrannon View Post
    Sorry you took it that way, but I didn't call you anything.

    I told you one thing to DO: SIMPLIFY,

    I told you why you should: because what you posted is too complex/complicated,

    I told you how: break out some of the code into other methods, and

    I gave you an example: gather the data for 12 months * the number of years.

    Now quit feeling sorry for yourself and do something to improve what you've posted.
    That's what I don't understand. Isn't that I did here for gathering data for 12 months per year??
    while(monthcount < 14){
                        monthrain = Double.parseDouble(jTextField2.getText());    
                        sum += monthrain;
                        jTextField2.setText("");
                        while(monthcount == 13 || i == 12){
                            counter++;
                            monthcount = 1;
                            i = 0;
                            if(counter == years){
                                average = sum / (years * 12);
                                jTextArea1.setText("There is a total of "  + (years * 12) + " months"
                                        + "\nTotal inches of rainfall is " + sum + " and"
                                        + "\nAverage inches of rainfall per month is " + df.format(average));
                                jTextField1.setEditable(true);
                                jTextField2.setEditable(false);
                                jTextField1.setText("");
                                jLabel2.setText("");
                                counter = 0;
                            }
                        }
                    }   
                }

    The codes after that block is for saying invalid input by the user.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    Is it? Is it working? If you want help with an error message, post the whole error message and stack trace, and somehow point to the line the error message is complaining about.

    If not, what is monthcount? Why is it less than 14? Where did the number 14 come from? What is counter? Why is monthcount == 13 and i == 12 significant? What is years? Why does it matter if counter is the same as years? Why is it so complicated?

    There is all of that complication, and I don't know why. Perhaps a comment now and then would help explain the code to someone reading it, including you.

    Here's a simple method that gets monthly rainfall data for the number of specified years:
        // a Scanner object to collect user's input
        static Scanner input = new Scanner( System.in );
     
        // method getMonthlyRainfallData() obtains the monthly rainfall data for 12
        // times the number of years specified by the parameter years
        public static double[] getMonthlyRainfallData( int years )
        {
            // a double array in which to store the monthly rainfall data
            double[] rainfallData = new double[12 * years];
     
            // the loop collects the monthly data from the user
            for ( int i = 0 ; i < 12 * years ; i++ )
            {
                System.out.print( "Ranfall for month " + ( i + 1 ) + ":  " );
                rainfallData[i] = input.nextDouble();
                System.out.println();
            }
     
            return rainfallData;
     
        } // end method getMonthlyRainfallData()
    Don't complain that you don't know or can't use arrays. If that's the case, don't use them, but use more methods to accommodate another, simpler approach.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    YES!! I finally got it working. It may be complicated, but it's not for something important, just a homework assignment.
    while(years > 0){
                jTextField1.setEditable(false);
                jTextField2.setEditable(true);
                monthcount++;
                while(monthcount == 13){
                    counter++;
                    monthcount = 1;
                }
                jLabel2.setText("Enter inches of rainfall for month " + monthcount + " of year " + (counter + 1) + ":");
                monthrain = Double.parseDouble(jTextField2.getText());
                while(monthrain >= 0){
                    while(monthcount <= 13){
                        monthrain = Double.parseDouble(jTextField2.getText());    
                        sum += monthrain;
                        jTextField2.setText("");
                        if(counter == years){
                            average = sum / (years * 12);
                            jTextArea1.setText("There is a total of "  + (years * 12) + " months"
                                    + "\nTotal inches of rainfall is " + sum + " and"
                                    + "\nAverage inches of rainfall per month is " + df.format(average));
                            jTextField1.setEditable(true);
                            jTextField2.setEditable(false);
                            jTextField1.setText("");
                            jLabel2.setText("");
                            counter = 0;
                        }
                    }
                }
                if(monthrain < 0){
                    jTextArea1.setText("Invalid inches of rainfall for the month"
                        + "\nPlease start over");
                    jTextField2.setText("");
                    monthcount -= 2;
                }
            }
            if(counter2 == 0){
                jTextArea1.setText("Invalid years\nTry again");
                jTextField1.setText("");
                jTextField1.setEditable(true);
                jTextField2.setText("");
                jTextField2.setEditable(false);
                monthrain = 0;
                sum = 0;
                monthcount = 0;
                counter = 0;
                counter2 = 0;
            }

  8. #8
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Quote Originally Posted by maxman190 View Post
    It may be complicated, but it's not for something important, just a homework assignment.
    I originally wrote two paragraphs about the above statement then I deleted it realising it would fall on deaf ears. This is what motivates me to better myself. Knowing that when I go for the job, people with attitudes like this will be my competition.

    Well done getting your code to work.

  9. The Following User Says Thank You to Kewish For This Useful Post:

    GregBrannon (October 19th, 2013)

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with my code for homework assignment, I don't know how to fix it!

    Well said, and may all your competition be similarly unmotivated and unable to recognize what really matters.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    Kewish (October 19th, 2013)

Similar Threads

  1. I keep getting an error message but I don't know how to fix this!
    By mpagudelo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 12th, 2013, 01:56 AM
  2. Replies: 1
    Last Post: June 9th, 2012, 11:29 AM
  3. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  4. Array Homework assignment
    By lich0802 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2011, 08:17 PM
  5. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM