Results 1 to 12 of 12

Thread: 1 more error

  1. #1
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default 1 more error

    I've been writing this for my intro to java class and I get the following message-

    Rainfall.java:88: cannot find symbol
    symbol : variable avgRain
    location: class Rainfall

    here's my code -

    import java.util.Scanner;

    public class Rainfall{

    public static void main(String args [] )
    {
    Scanner in = new Scanner(System.in);
    int numYears = 0;

    int invalid = 0;
    do{
    if(invalid == 0)
    {
    System.out.print("Please enter how many years you wish to calculate: ");
    numYears = in.nextInt();
    }
    else
    {
    System.out.print("Please enter a positive number for Years: ");
    numYears = in.nextInt();
    }

    invalid++;
    }while( numYears <= 0);

    {

    do{

    int janRain;
    int febRain;
    int marRain;
    int aprRain;
    int mayRain;
    int junRain;
    int julyRain;
    int augRain;
    int sepRain;
    int octRain;
    int novRain;
    int decRain;
    int avgRain;

    System.out.println ("Enter average amount of rain for January");
    janRain = in.nextInt();

    System.out.println ("Enter average amount of rain for February");
    febRain = in.nextInt();

    System.out.println ("Enter average amount of rain for March");
    marRain = in.nextInt();

    System.out.println ("Enter average amount of rain for April");
    aprRain = in.nextInt();

    System.out.println ("Enter average amount of rain for May");
    mayRain = in.nextInt();

    System.out.println ("Enter average amount of rain for June");
    junRain = in.nextInt();

    System.out.println ("Enter average amount of rain for July");
    julyRain = in.nextInt();

    System.out.println ("Enter average amount of rain for August");
    augRain = in.nextInt();

    System.out.println ("Enter average amount of rain for September");
    sepRain = in.nextInt();

    System.out.println ("Enter average amount of rain for October");
    octRain = in.nextInt();

    System.out.println ("Enter average amount of rain for November");
    novRain = in.nextInt();

    System.out.println ("Enter average amount of rain for December");
    decRain = in.nextInt();

    avgRain = (janRain+febRain+marRain+aprRain+mayRain+junRain+j ulyRain+augRain+sepRain+octRain+novRain+decRain);

    System.out.println("The Average amount of rain for " + numYears + " years, or " + (numYears*12) + "Months is");

    System.out.print("Average Per year = " + numYears / avgRain);

    }while(avgRain >= 0);

    }
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens ifyou declare all the variables outside the do while loop?

  3. #3
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    depending on where I place them 12-28 more errors, most of which say my entered ints may not have initialized

  4. #4
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    i was wondering if it was because I didn't have an invalid value for the 2nd do while loop like the 1st one, but im not 100% sure how to do that. Or maybe a second Scanner for the 2nd loop.
    Last edited by Promisha; 03-09-2011 at 02:52 AM.

  5. #5
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    This is a variable scope problem. avgRain is only visible from inside the loop. You should probably initialize this variable somewhere outside of the loop.

    See: Variable Scope : Variable Scope**Language**Java Tutorial

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Instead of hardcoding it may be more efficient to declare an array outside the loops and using a for loop fill the array with user input. When you moved all the month variables did you try initializing them all to 0?

  7. #7
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Instead of hardcoding it may be more efficient to declare an array outside the loops and using a for loop fill the array with user input. When you moved all the month variables did you try initializing them all to 0?
    It's true that it would be more efficient to use an array, however, the only variable which must be declared outside of the loop is avgRain

  8. #8
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Tried setting month variables to 0, same issue.

    kwgivler, where exactly would you suggest I move my avgRain variable? I'm still really new to this.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Outside the do loop.

  10. #10
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Promisha View Post
    Tried setting month variables to 0, same issue.

    kwgivler, where exactly would you suggest I move my avgRain variable? I'm still really new to this.
    As sunde887 said, outside the do loop. Maybe after int invalid = 0; ?

  11. #11
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    moved it below numYears and I got to compile! it now does the math and all that jazz. It immediately starts asking for the amount of rain for each month after even though it displays "average amount of rain for x years is y inches".
    I didn't see a requirement saying it needed to exit, but I'm assuming I need to... now what :(

  12. #12
    Promisha is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    no what nevermind about exiting, I don't think he'll mind that much, I just gotta fidget around w/ the math so my average comes out as the correct number. Thanks all!

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 10:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 09:53 AM

Posting Permissions

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