write a program(AvocadoMining.java) that has the following several fields and three methods
size:int, the number of lines in a file.
prices: double[]
totalVolumes:double[]
regions: String[]
+ AvocadoMining(fileName:String)
It reads the file, assigns the number of lines in the file to the field size, then creates several arrays for prices, totalVolumes, regions.
+findMax():void
The method finds out the maximum average price during the year, and then prints the price, the total volumes and the region associated with the price.
+findTotal(String regionName):double
The method finds out and returns the total of the total volumes for a given region during the year.
I am working on the findMax() method but i am having trouble getting all of the data in my code. Here is my work so far
import java.util.Scanner;
import java.io.*;
public class AvocadoMining
{
private int size=0;
private String[] region;
private double[] price;
private double[] totalVolumes;
public AvacadoMining(String fileName) throws IOException
{
File file=new File(fileName);
Scanner inputF = new Scanner(file);
while(inputF.hasNextLine())
{
String str=inputF.nextLine();
size++;
}
inputF=new Scanner(file);
region = new String[size];
price = new double[size];
totalVolumes=new double[size];
int index=0;
while(inputF.hasNextLine())
{
String[] tokens = inputF.nextLine().split(",");
price[index]=Double.parseDouble(tokens[2]);
totalVolumes[index]=Double.parseDouble(tokens[3]);
region[index]=tokens[13].trim();
index++;
}
}
public void findMax() throws IOException
{
double maximum = price[0];
for(int i=0;i<price.length;i++)
{
if(maximum<price[i])
maximum = price[i];
}
String line ="Price: $.2f"+maximum + " Total Volume: "+
totalVolumes[price.indexOf(maximum)] +"Region: " +
region[price.indexOf(maximum)];
}
}
If I try to compile I will get a cannot find symbol error for price in the String line.
indexOf
is not a method on arrays. You'd have to write it yourself. In real life coding (for dollars or eyeballs), you don't use arrays, you'd use Lists which do have an indexOf function, but this sounds like homework, so you're probably 'locked' into arrays. You can write indexOf yourself, with a for loop.