I'm getting a "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at WeatherAnalysis.main(WeatherAnalysis.java:37)" Im not sure what is wrong at line 37, date[CurrentLine][0] = (int)fullDate % 100;. Trying to use .txt file to output weather patterns from 1941 to 2013.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class WeatherAnalysis {
public static void main(String[] args) throws FileNotFoundException {
int NumberOfDays = 0;
String station;
File f = new File("PortlandWeatherCut.txt"); //Import text file.
Scanner input= new Scanner(f);
input.nextLine();
input.nextLine();
while (input.hasNextLine()) { //Assign number of days to be
NumberOfDays++;
input.nextLine();
}
double[][] date = new double[NumberOfDays][3];
double[] prcp = new double[NumberOfDays];
double[] snow = new double[NumberOfDays];
double[] snwd = new double[NumberOfDays];
double[] tmax = new double[NumberOfDays];
double[] tmin = new double[NumberOfDays];
int CurrentLine = 0;
double fullDate = 0.0;
File g = new File("PortlandWeather.txt"); //Import text file.
Scanner weather= new Scanner(g);
weather.nextLine(); weather.nextLine();
while(weather.hasNextLine()) { //variables and converts to inches.
station = weather.next();
fullDate = weather.nextDouble();
date[CurrentLine][0] = (int)fullDate % 100;
date[CurrentLine][1] = ((int)fullDate % 10000) / 100;
date[CurrentLine][2] = (int)fullDate / 10000;
prcp[CurrentLine] = weather.nextDouble()* .00393701;
snow[CurrentLine] = weather.nextDouble()* 0.0393701;
snwd[CurrentLine] = weather.nextDouble()* 0.0393701;
tmax[CurrentLine] = (weather.nextDouble() / 10.0) * (9.0/5.0) + 32.0;
tmin[CurrentLine] = (weather.nextDouble() / 10.0) * (9.0/5.0) + 32.0;
CurrentLine++;
weather.nextLine();
}
int indexPoint = 0;
int nextPoint = 1;
int endPoint = 0;
for (int x = 1; x < date.length; x++){
//loop that calculates array section to be averaged,
//uses a method to average them, then prints a table
do {
nextPoint++;
endPoint++;
}
while(((int)date[nextPoint - 1][02] / 10) == ((int)date[nextPoint][1] / 10));
if ( nextPoint < date.length) {
System.out.printf("%4.0f's Average Max Temp = %4.1f\tAverage Min Temp = %4.1f\n",
date[indexPoint][02],arrayAvg(tmax, indexPoint, endPoint),
arrayAvg(tmin, indexPoint, endPoint));
indexPoint = nextPoint;
} else
System.out.println();
}
}
public static double arrayAvg(double a[], int fromIndex, int toIndex) {
//Averages the values between the entered parameters and returns it.
double sum = 0;
double divisor = 0;
for (int i = fromIndex; i < toIndex; i++){
if (a[i] == 393.6616299) {
divisor++;
} else {
sum += a[i];
divisor++;
}
}
double average = sum / divisor;
return average;
}
}
Seems like your issue is at line 37. I think that is the
date[currentline][0] = (int)fullDate%100
Inside of your while loop. The loop continues above the NumberOFdAYS that the date 2-dimensional array was created with, therefore it throws out of bout.
My first advise is to break down this monster function into smaller function (Look up Single Responsibility principle).
My second advise is to do TDD (Test driven development)
But if you want a quick and dirty debug, do the following at the beginning of your while loop:
System.out.printf("Current line = %d, does weather has next line? = %b", CurrentLine, weather.hasNextLine)
This will show you all the values of currentLine before the exception and shows the value of your boolean.
Related
Here i am calculating the value of Pi using Gregory-Leibniz series which is an infinite series.I want to calculate the value of pi in each iteration of for loop and compare is to the previous iteration so i can calculate the difference. Further i want to stop the iteration when the difference is less than or equal to 0.001 . How to do it???Please help me.
import java.util.*;
import java.lang.Math;
class ValueOfPi{
public static void main(String[] args) {
double currentTerm = 0;
double pi = 0.0;
int odd = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of terms:");
int x = sc.nextInt();
for(int i = 1;i<=x;i++) {
if(i%2 == 0) {
currentTerm = (double) -4/odd;
}
else {
currentTerm = (double) 4/odd;
}
odd=odd+2;
pi=pi+currentTerm;
}
System.out.println("The value of pi:"+pi);
}
}
Just keep a variable(here: previous_pi) from the past calculation and compare it with the new calculation of pi.
double currentTerm = 0;
double pi = 0.0;
double previous_pi = 0.0;
int odd = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of terms:");
int x = sc.nextInt();
for(int i = 1;i<=x;i++) {
previous_pi = pi;
if(i%2 == 0) {
currentTerm = (double) -4/odd;
}
else {
currentTerm = (double) 4/odd;
}
odd=odd+2;
pi=pi+currentTerm;
//Find the difference from the previous calculation of pi
System.out.println("Difference from previous calculation: " + Math.abs(pi-previous_pi));
//Stop the iteration when the difference is less than or equal to 0.001
if(Math.abs(pi - previous_pi) <= 0.001){
break;
}
}
System.out.println("The value of pi:"+pi);
}
I found someone who had a similar problem (How to calculate the median of an array?), but I couldn't figure out how to incororate it in to my own code since I am rather new to java. Right now, my findmedian method is returning 0 instead of the actual median and I can't seem to figure it out. Thanks!
import java.util.Scanner;
import java.util.Arrays;
public class Original
{
public static void main(String[] args)
{
Scanner inputNumber = new Scanner(System.in);
Scanner dataItem = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.print("This stores a list of contirbutions to a charity drive.\n ");
System.out.print("How many contributors will be entered? ");
double contributors = inputNumber.nextDouble();
double contributions[ ] = new double[50];
double contributions_check[] = findData (contributors, contributions);
System.out.print("See if the contributions are correct. ");
// Displays the contributions, loop allows numbers to be displayed correctly
for (int count = 0; count < contributors; count++) {
System.out.print(contributions_check[count] + " ");
}
double median = findmedian(contributors,contributions_check);
System.out.print("\n The median contribution is: " + median);
}
public static double[] findData(double n, double[] contributions2)
{
Scanner dataItem = new Scanner(System.in);
// x must be 0 and x must be < than n
for (int x = 0; x < n; x++) {
System.out.print("Please enter the next contribution: ");
contributions2[x] = dataItem.nextDouble();
}
return contributions2;
}
public static double findmedian(double n, double data[])
{
Arrays.sort(data);
double median;
if (data.length % 2 == 0) {
median = ((double) data[data.length / 2] +
(double) data[data.length / 2 - 1]) / 2;
} else {
median = (double) data[data.length/2];
}
return median;
}
}
I think the issue is you are using data.length in findmedian, where you should be using n. data.length is always going to be 50, even if you only entered 5 items....
Use the number of contributors n to know the valid contributors in your array.
public static double findmedian(double n, double data[])
{
Arrays.sort(data);
double median;
if (data.length % 2 == 0) {
median = ((double) data[n / 2] +
(double) data[n / 2 - 1]) / 2;
} else {
median = (double) data[n/2];
}
return median;
}
I am trying to calculate pi with the Nilakantha method. Whenever I run this program I get -Infinity if I input 1 and anything else I get NaN.
I am trying to modify my program that uses the Leibniz method, and I'm very new to java.
I appreciate all help!
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of calculations you would like to do");
long no = Long.parseLong(reader.readLine());
long cycle = 0;
long w = 2;
long x = 3;
long y = 4;
long z = 4;
long odd=1;
long i=1;
long a = 1;
long b = 1;
double pi= 0.0;
for(;i<=no;i++)
{
a = w*x*y;
b = x*y*z;
double currentTerm=0.0;
if (i%2==0)
{
currentTerm=(double)4/a;
cycle = cycle+1;
w = w+1;
x = x+1;
y = y+1;
}
else
{
currentTerm=(double)-4/b;
cycle = cycle+1;
x = x+1;
y = y+1;
z = z+1;
}
odd=odd+2;
pi = pi+currentTerm;
}
System.out.println("You calculated that pi is");
System.out.println(pi);
System.out.println(3.1415926535897932);
System.out.println("Pi is actually");
double error = pi/3.1415926535897932;
if(error >= 1) {
double bigerror=2-error;
System.out.println("Your accuracy is");
System.out.println(bigerror*100);
System.out.println("percent");
System.out.println(cycle);
}
else {
System.out.println("Your accuracy is");
System.out.println(error*100);
System.out.println("percent");
System.out.println(cycle);
}
}
}
On your first iteration a and b are both zeros.
I don't think you have your initialization part correct.
http://helloacm.com/two-simple-equations-to-compute-pi/
Here I see that j starts from 2.
You have zeroes.
Make sure you implement the algorithm correctly.
Here is your code corrected.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainProgram {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of calculations you would like to do");
long no = Long.parseLong(reader.readLine());
long step = 0;
double ans = 3;
long j = 2;
double pi = 0.0;
while (true) {
step++;
if ((step % 2) == 1) {
ans += 4.0 / (1.0 * j * (j + 1) * (j + 2));
} else {
ans -= 4.0 / (1.0 * j * (j + 1) * (j + 2));
}
j += 2;
pi = ans;
if (step >= no)
break;
}
System.out.println("You calculated that pi is");
System.out.println(pi);
System.out.println("Pi is actually");
System.out.println(3.1415926535897932);
double error = pi / 3.1415926535897932;
if (error >= 1) {
double bigerror = 2 - error;
System.out.print("Your accuracy is: ");
System.out.print(bigerror * 100);
System.out.println(" percent");
System.out.println(step);
} else {
System.out.print("Your accuracy is: ");
System.out.print(error * 100);
System.out.println(" percent.");
System.out.println(step);
}
}
}
I've been staring at this for a few hours, and I can't seem to figure out why my outputs are coming out too small. My outputs do what you'd expect, level off, they just level off to the wrong value. I'm very certain the error lies within the while loop of my main method, or the nested for loop, but I'll go ahead and post everything incase it is hiding somewhere else.
The purpose of this program is to estimate the value of e^x by summing 1 + x + x^2/2! + x^3/3! + ... + x^n/n!. It needs to output the sum for n take to each value between 1 and 10, along with the values of 50 and 100. So 12 outputs all together.
import java.io.*;
import java.text.DecimalFormat;
public class Ch3Ex7
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static double x = 0;
static DecimalFormat df = new DecimalFormat("0.00");
public static void main (String[] args)
{
mainMenu();
int counter = 1;
double heldx = x;
double holderx = 0;
double denom = 1;
double printx = 0;
double f = 1;
while (counter != 100)
{
x = x*heldx;
denom++;
for(int i = 2; i<=denom; i++)
{
f = f*i;
}
holderx = holderx + x/f;
if ((counter > 0 && counter <= 10) || (counter == 50))
{
printx = 1 + heldx + holderx;
System.out.println(df.format(printx));
}
counter++;
}
System.out.println(df.format(printx));
f = 0;
x = 0;
counter = 1;
denom = 1;
callMain();
}
public static void mainMenu()
{
try
{
System.out.println("Requesting user input, press 0 to leave");
x = Integer.parseInt(br.readLine());
space();
if (x == 0)
{
System.exit(0);
}
}
catch(IOException ioe) {}
}
public static void callMain()
{
String[] x = {"A" , "B"};
Ch3Ex7.main(x);
}
public static void space()
{
System.out.println();
}
}
The problem is with your "f" variable. You're trying to calculating the value every time, but you end up starting with your old value.
Get rid of that for loop and add f *= denom; in its place.
import java.io.*;
public class TestCaseAbcd {
public static void main(String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(
float x0;
float a, c, mod;
int num, ch = 0;
double[] rNumbers;
double[] rTemp;
System.out.println("Enter the SEED value: ");
System.out.println("x0 ");
x0 = Float.parseFloat(stdin.readLine());
System.out.println("Enter the multiplier's value:");
System.out.println("a: ");
a = Float.parseFloat(stdin.readLine());
System.out.println("Enter the value of increment c and modulus m: ");
System.out.println("c: ");
c = Float.parseFloat(stdin.readLine());
System.out.println("m: ");
mod = Integer.parseInt(stdin.readLine());
System.out.println("How many random nunbers u need? ");
num = Integer.parseInt(stdin.readLine());
rNUmbers = new double[num];
rTemp = new double[rNumbers.length];
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
for (int i = 0; i < rNumbers.length; i++)
{
if (i + 1 != num)
{
rNumbers[i] = rTemp[i] / mod;
}
else
{
break;
}
}
System.out.println("The PSEUDO random numbers are: ");
for (int i = 0; i < rNumbers.length; i++)
{
System.out.println(rNumbers[i]);
}
double firstNum = rNumbers[0];
System.out.println("1. Select Mutant 1 ");
System.out.println("2. Select Mutant 2 ");
System.out.println("3. Exit ");
}
}
In the above code, the expected output should start from: 0.68.
But, instead it started from:
0.37.
In fact, even after I changed the following code:
rTemp[i+1] = ( ( (rTemp[i]*a) + c ) % mod);
to:
rTemp[i+1] = ( ( (rTemp[i]/a) + c ) % mod);
The output still started from 0.37.
The input values are:
x0 = 37
a = 7
c = 9
m = 100
Please help me in analyzing the code so as that the output shouldn't start with 0.37.
Summary of the problem: the code is producing the same number i.e. 0.37 no matter what the equation stated above in the code is modified to.
You're setting the first value in rTemp to 37, then you start printing from the first value. It only makes sense that the first value printed would be 0.37.
x0 = 37
mod = 100
...
rTemp[0] = x0;
...
rNumbers[i] = rTemp[i] / mod;
...
System.out.println(rNumbers[i]);
To achieve the output you're looking for, change this:
rTemp[0] = x0;
for (int i = 0; i < num; i++)
{
if(i + 1 != num)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
else
{
break;
}
}
to this:
rTemp[0] = (((x0 * a) + c) % mod);
for (int i = 0; i < num-1; i++)
{
rTemp[i + 1] = (((rTemp[i] * a) + c) % mod);
}
On a side note, if all you really want is an array of psudo-random doubles, it's easier to do something like this:
Random rnd = new Random(seed);
double[] rNumbers = new double[num];
for(int i = 0; i < num; i++)
rNumbers[i] = (double)rnd.nextInt(100) / 100;
For any given seed, you'll get the same array of doubles every time.