Hi,

I am in a requirement of selecting a particular .csv file for reading into the program for further manipulations (calculations) based on zipcode entered by the user.

So, can u pls direct me for the same so as to get the things moving on.

what is the relationship between zipcode and the .csv file? I mean if it is simply [zipcode].csv, you can just read the user input and read the file...

what is the relationship between zipcode and the .csv file? I mean if it is simply [zipcode].csv, you can just read the user input and read the file...

The relationship is that - when the user enters a zipcode, the program should map and read .csv file associated with that specific zipcode entered. (for eg: if the user enters zipcode for Orlando, FL then the program should read all the weather data from orlando.csv only). I hope this helps to make the problem clearer.

ok, so where is the problem? it seems you have fairly clear requirements, where exactly are you struggling?

if you haven't started at all, you need to work out how to read a user's input, so you want to investigate java IO.

well, i hav written a sample kinda code for getting the data from user and then processing it based upon certain calculations and then presenting the result as output. This program doesn't concern with reading the data from the database (or available files like FL.zip), but does the processing based on what the user enters.

The code is :-

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;


System.out.println ("value of i"+i);
System.out.println ("value of n"+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=1;i<=n;i++)
{
System.out.println("Enter the rainfall values for" + i + "days");
rainfall = MyInput.readDouble();


System.out.println("i="+i);
System.out.println("n="+n);


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


System.out.println("i="+i);
System.out.println("n="+n);


etc = kc*eto;
}


for (i=1;i<=n;i++)
{
System.out.println ("mad =" +mad);


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


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


waterreqdforirrigation = mad - netsoilwaterbalance;


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


}


}

What the program will do is that it will ask the user to enter how many days of data he has with him. Then based upon this, the user will have to enter rainfall, ETo values for that much amount of days. But, I am getting run time error in this..which is as follows (I get this error after entering the rainfall data for the last day and the program stops thenafter)

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

Arrays are zero based, so for 'n' elements, you have index values 0 to n-1. Therefore, this loop

for (i=1;i<=n;i++)

will skip the first element and run to n, which is past the end of the array. You need to loop

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

yea i know dat....bt the problem is that i don't wanna calculate netsoilwaterbalance[0].
and by doing for(i=0....)
it won't calculate netsoilwaterbalance = netsoilwaterbalance[i-1] + rainfall - etc;
coz of netsoilwaterbalance[i-1] term.
as netsoilwaterbalance[0] is a fixed value (=mad)

i also tried using for(i=1; i<n+1, i++), but still it shows the same error.

Well, you are using that same loop on your inputs and it's invalid.

>netsoilwaterbalance = netsoilwaterbalance[i-1] + rainfall - etc;
If you let i get to 'n', that expression is going to fail as well. You still need to limit that to i<n .

I made following modifications in portion of the code:

      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);
                   }

      }

  }

}

This is to get rid of zero values being shown. but, still it shows the same error.

Of course it does. You are still advancing to 'n' because you just moved the increment inside the loop. You are checking that it's less than n, but then you increment it.

Your input loop needs to fill those arrays, from i=0 to i=n-1.
For your output, you just want to process from i=1 to i=n-1.
So write those loops accordingly and put the increment back in the for() statement.

And make sure to place [code] [/code] tags around your code in future posts so that formatting is preserved.

Of course it does. You are still advancing to 'n' because you just moved the increment inside the loop. You are checking that it's less than n, but then you increment it.

Your input loop needs to fill those arrays, from i=0 to i=n-1.
For your output, you just want to process from i=1 to i=n-1.
So write those loops accordingly and put the increment back in the for() statement.

And make sure to place [code] [/code] tags around your code in future posts so that formatting is preserved.

if i use from i=1 to i=n-1 in my output loop, how can i get the values for my last day (say i am entering n=3 initially) so the output will give me results for only days 1 and 2.

now pls help me out on this...this is gettin on my nerves...

It will access the last day. That is the entire point. Because arrays are zero based, the index is one less than the ordinal number of the element you are accessing.

If n=3, you have 3 elements in the array. Write out their index values: 0,1,2
So on your input loop, trace the value of i all the way through this

for (int i=[B]0[/B]; [B]i<n[/B]; i++)

and now for your output loop, trace this

for (int i=[B]1[/B]; [B]i<n[/B]; i++){
  netsoilwaterbalance[i] = netsoilwaterbalance[i-1] + rainfall[i] - etc[i];
}

Write down on paper the value of i in each of those.
I really can't explain it any more than that.

the iterations are as follows for the output loop:

1st iteration:- (for i=1, and here n=2)
netsoilwaterbalance[1]=netsoilwaterbalance[0]+rainfall[1]-etc[1]
2nd iteration:-
it won't go into 2nd iteration as i=2<n=2 is not satisfied....so i won't get any results for 2nd day.
I hope this helps to get a clearer picture.

>it won't go into 2nd iteration as i=2<n=2 is not satisfied....so i won't get any results for 2nd day.
See the point is that i=1 is the second day. i=2 would be a third day that doesn't exist in your n=2 array.

I'm not really following which days you are expecting to add to the "netsoilwaterbalance". On day 2, are you trying to add day 1's rainfall values or day 2's values?
if you want netsoilwaterbalance(day 2) = netsoilwaterbalance(day 1) + rainfall(day 2) - etc(day 2) then I would recommend writing your loop like this instead

double lastWaterBalance = mad;
for (i=0;i<n;i++}{
    netsoilwaterbalance[i] = lastWaterBalance + rainfall[i] - etc[i];
    lastWaterBalance = netsoilwaterbalance[i];
}

Thanks a lot...I am really grateful to u...
the last replied helped me to solve d problem...hurrah !!!

Now Java looks really good to me !!! nways thanks again for your kind help and co-operation..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.