英文:
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:
AvacadoMining
should beAvocadoMining
in the constructor declaration.- Fixed the import statements for clarity.
- Used
String.format()
to correctly format the output line in thefindMax()
method. - Added a missing
System.out.println()
to actually display the result of thefindMax()
method. - Added the
findTotal()
method for finding the total volume for a given region. - 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(",");
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.
答案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 = "Price: $.2f" + maximum + " Total Volume: " + totalVolumes[indexOf(price, maximum)] + "Region: " + 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论