Need help getting data from a text file in java. I need to find the maximum price and the data associated with the price

huangapple go评论64阅读模式
英文:

Need help getting data from a text file in java. I need to find the maximum price and the data associated with the price

问题

import java.util.Scanner;
import java.io.*;

public class AvocadoMining {
    private int size = 0;
    private String[] regions;
    private double[] prices;
    private double[] totalVolumes;

    public AvocadoMining(String fileName) throws IOException {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNextLine()) {
            String str = inputFile.nextLine();
            size++;
        }

        inputFile = new Scanner(file);
        regions = new String[size];
        prices = new double[size];
        totalVolumes = new double[size];
        int index = 0;

        while (inputFile.hasNextLine()) {
            String[] tokens = inputFile.nextLine().split(",");
            prices[index] = Double.parseDouble(tokens[2]);
            totalVolumes[index] = Double.parseDouble(tokens[3]);
            regions[index] = tokens[13].trim();
            index++;
        }
    }

    public void findMax() throws IOException {
        double maximum = prices[0];
        int maxIndex = 0;

        for (int i = 0; i < prices.length; i++) {
            if (maximum < prices[i]) {
                maximum = prices[i];
                maxIndex = i;
            }
        }

        String line = String.format("Price: %.2f Total Volume: %.2f Region: %s",
                                    maximum, totalVolumes[maxIndex], regions[maxIndex]);
        System.out.println(line);
    }

    public double findTotal(String regionName) {
        double total = 0;
        for (int i = 0; i < regions.length; i++) {
            if (regions[i].equalsIgnoreCase(regionName)) {
                total += totalVolumes[i];
            }
        }
        return total;
    }
}

Please note that I've corrected a few issues in your code:

  1. AvacadoMining should be AvocadoMining in the constructor declaration.
  2. Fixed the import statements for clarity.
  3. Used String.format() to correctly format the output line in the findMax() method.
  4. Added a missing System.out.println() to actually display the result of the findMax() method.
  5. Added the findTotal() method for finding the total volume for a given region.
  6. Fixed the assignment of maxIndex to capture the index of the maximum price.
英文:

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(&quot;,&quot;);
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&lt;price.length;i++)
{
if(maximum&lt;price[i])
maximum = price[i];
}
String line =&quot;Price: $.2f&quot;+maximum + &quot; Total Volume: &quot;+ 
totalVolumes[price.indexOf(maximum)] +&quot;Region: &quot; + 
region[price.indexOf(maximum)]; 
}

}

If I try to compile I will get a cannot find symbol error for price in the String line.

答案1

得分: 0

indexOf不是数组的方法。你需要自己编写它。在实际的编码中(为了金钱或关注度),你不会使用数组,而会使用具有indexOf函数的列表(Lists)。但这听起来像是作业,所以你可能被限制在使用数组。你可以使用for循环自己编写indexOf。

英文:

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.

答案2

得分: 0

Change your indexOf line as shown below,

String line = "价格:$%.2f" + maximum + " 总交易量:" + totalVolumes[indexOf(price, maximum)] + " 地区:" + region[indexOf(price, maximum)];

After that, add the below method,

public int indexOf(double arr[], double t) 
{ 
    int len = arr.length; 
    return IntStream.range(0, len) 
        .filter(i -> t == arr[i]) 
        .findFirst() // first occurrence 
        .orElse(-1); // No element found 
}
英文:

Change your indexOf line as shown below,

String line = &quot;Price: $.2f&quot; + maximum + &quot; Total Volume: &quot; + totalVolumes[indexOf(price, maximum)] + &quot;Region: &quot; + region[indexOf(price, maximum)];

After that, add the below method,

public int indexOf(double arr[], double t) 
{ 
int len = arr.length; 
return IntStream.range(0, len) 
.filter(i -&gt; t == arr[i]) 
.findFirst() // first occurrence 
.orElse(-1); // No element found 
}

huangapple
  • 本文由 发表于 2020年10月7日 12:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定