Results 1 to 7 of 7
  1. #1
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default HELP ON errors occuring in the code

    I have written the following code, in which a user has to first enter the no. of days for which he has the data available. Then he has to enter the rainfall, ETo data for those days. Then the program will calculate the required values and present the output - is irrigation required and if yes, then how much amount ?

    import java.io.*;

    public class calculations {


    public static void main (String[] args) throws IOException

    {
    double rootingdepth=0.6;
    double asws=125;
    double sws;
    double allowablecoefficient=0.45;
    double mad;


    double kc=0.42;

    double[] netsoilwaterbalance;
    double[] waterreqdforirrigation;

    int i=0;





    System.out.println ("Enter the days of available data");
    int n=MyInput.readInt();
    i=n;


    double[] rainfall = new double[n];
    double[] eto = new double[n];
    double[] etc = new double[n];

    netsoilwaterbalance = new double[n];
    waterreqdforirrigation = new double [n];

    sws = rootingdepth*asws;
    mad = sws*allowablecoefficient;

    netsoilwaterbalance[0] = mad;


    for (i=0;i<n;)
    { i++;
    System.out.println("Enter the rainfall values for" + i + "days");
    rainfall[i] = MyInput.readDouble();



    System.out.println("Enter the ETo values for" + i + "days");
    eto[i] = MyInput.readDouble();



    etc[i] = kc*eto[i];
    }



    for (i=0;i<n;)
    {
    i++;

    netsoilwaterbalance[i] = netsoilwaterbalance[i-1] + rainfall[i] - etc[i];
    System.out.println ("the amount of water in the soil on day" + i + "is" + netsoilwaterbalance[i]);

    if (netsoilwaterbalance[i] <= mad)
    { System.out.println ("Irrigation is required on day" + i);

    waterreqdforirrigation[i] = mad - netsoilwaterbalance[i];

    System.out.println ("water reqd for irrigation on day" + i + "is" + waterreqdforirrigation[i]);
    }
    else
    { System.out.println ("Irrigation is not required on day" +i);
    }

    }

    }

    }

    But, I am getting following error: (this error comes when I enter the rainfall data for the last day. The program stops thenafter)

    " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at calculations.main(calculations.java:57) "

    Note: The for loop is made such that I don't have to deal with rainfall[0] (for eg)..and also to take care of "netsoilwaterbalance[i] = netsoilwaterbalance[i-1] + rainfall[i] - etc[i];" , where initially for the 1st iteration netsoilwaterbalance[1]=netsoilwaterbalance[0]+rainfall[1] should be there.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Welll, for starters...
    Java Code:
    for (i=0;i<n;)
    { i++;
    Why are you doing this? Why not have it as:
    Java Code:
    for (i=0;i<n;i++)
    {
    Becaue the way you have it, when you start to to work with the array, the index is 1, instead of 0.
    On closer look at this code, it has many problems...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    i also tried for (i=0;i<n;i++)
    but the problem with this is that system.out.print command will ask the user to enter the rainfall data for 0th day in the first iteration...so how to make this as 1st day ?
    Also, the same for loop when will be used second time, the first iteration would have to calculate
    netsoilwaterbalance[0] = netsoilwaterbalance[0-1] + rainfall[0] - etc[0]; which is impossible

    and netsoilwaterbalance[0] is already assigned value of mad in the beginning of the program.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default index vs position

    just add a 1 to "i" when printing or create a "pos" variable which is equal to "i+1" and use that in your print statements:
    Java Code:
    int pos = i+1;
    .
    .
    .
    System.out.println("Enter the rainfall values for" + pos + "days");
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    jaiminparikh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default

    import java.io.*;

    public class calculations {


    public static void main (String[] args) throws IOException

    {
    double rootingdepth=0.6;
    double asws=125;
    double sws;
    double allowablecoefficient=0.45;
    double mad;


    double kc=0.42;

    double[] netsoilwaterbalance;
    double[] waterreqdforirrigation;

    int i=0;
    int pos;



    System.out.println ("Enter the days of available data");
    int n=MyInput.readInt();



    double[] rainfall = new double[n];
    double[] eto = new double[n];
    double[] etc = new double[n];

    netsoilwaterbalance = new double[n];
    waterreqdforirrigation = new double [n];

    sws = rootingdepth*asws;
    mad = sws*allowablecoefficient;

    netsoilwaterbalance[0] = mad;


    for (i=0;i<n;i++)
    { pos=i+1;
    System.out.println("Enter the rainfall values for" + pos + "days");
    rainfall[pos] = MyInput.readDouble();



    System.out.println("Enter the ETo values for" + pos + "days");
    eto[pos] = MyInput.readDouble();



    etc[pos] = kc*eto[pos];
    }



    for (i=0;i<n;i++)
    {
    pos=i+1;

    netsoilwaterbalance[pos] = netsoilwaterbalance[pos-1] + rainfall[pos] - etc[pos];
    System.out.println ("the amount of water in the soil on day" + pos + "is" + netsoilwaterbalance[pos]);

    if (netsoilwaterbalance[pos] <= mad)
    { System.out.println ("Irrigation is required on day" + pos);

    waterreqdforirrigation[pos] = mad - netsoilwaterbalance[pos];

    System.out.println ("water reqd for irrigation on day" + pos + "is" + waterreqdforirrigation[pos]);
    }
    else
    { System.out.println ("Irrigation is not required on day" +pos);
    }

    }

    }

    }

    I have modified the code using pos variable, but still it shows the same error.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Use 'pos' only for display to the user, not as an array index. For the array index, continue to use 'i'.

    If you're going to write Java code, you're going to have to embrace the idea of zero indexed arrays. Learn to love them rather than trying to work arround them.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default index vs pos

    Yes... read other peoples posts carefully...
    and use that in your print statements:
    I said to use the pos variable in the print statements.

    Also, you don't need to put "pos=i+1" in the "for" statements... just declare it once in your code:
    Java Code:
    int i=0;
    int pos = i+1;
    What is the following?:
    Java Code:
    netsoilwaterbalance[0] = mad;
    Won't this get written over by the "for" statement that populates the netsoilwaterbalance array?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. 2 errors with Scanner , help
    By enghar in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 09:34 PM
  2. makeButton errors
    By ljk8950 in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2008, 01:10 AM
  3. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM
  4. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 09:32 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
  •