需要帮助从 java 中的文本文件获取数据。我需要找到最高价格和与价格相关的数据
Need help getting data from a text file in java. I need to find the maximum price and the data associated with the price
写一个程序(AvocadoMining.java)有如下几个字段和三个方法
size:int, the number of lines in a file.
prices: double[]
totalVolumes:double[]
regions: String[]
+ AvocadoMining(fileName:String)
它读取文件,将文件中的行数分配给字段大小,然后为价格、totalVolumes、地区创建几个数组。
+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.
我正在研究 findMax() 方法,但我在获取代码中的所有数据时遇到问题。
这是我目前的工作
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)];
}
}
如果我尝试编译,我将在字符串行中收到无法找到价格的符号错误。
indexOf
不是数组上的方法。你必须自己写。在现实生活中编码(为了金钱或眼球),您不使用数组,您会使用具有 indexOf 函数的列表,但这听起来像是家庭作业,所以您可能 'locked' 进入数组。你可以自己写indexOf,用for循环。
如下所示更改您的 indexOf 行,
String line = "Price: $.2f" + maximum + " Total Volume: " + totalVolumes[indexOf(price, maximum)] + "Region: " + region[indexOf(price, maximum)];
之后,添加下面的方法,
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
}
写一个程序(AvocadoMining.java)有如下几个字段和三个方法
size:int, the number of lines in a file.
prices: double[]
totalVolumes:double[]
regions: String[]
+ AvocadoMining(fileName:String)
它读取文件,将文件中的行数分配给字段大小,然后为价格、totalVolumes、地区创建几个数组。
+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.
我正在研究 findMax() 方法,但我在获取代码中的所有数据时遇到问题。 这是我目前的工作
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)];
}
}
如果我尝试编译,我将在字符串行中收到无法找到价格的符号错误。
indexOf
不是数组上的方法。你必须自己写。在现实生活中编码(为了金钱或眼球),您不使用数组,您会使用具有 indexOf 函数的列表,但这听起来像是家庭作业,所以您可能 'locked' 进入数组。你可以自己写indexOf,用for循环。
如下所示更改您的 indexOf 行,
String line = "Price: $.2f" + maximum + " Total Volume: " + totalVolumes[indexOf(price, maximum)] + "Region: " + region[indexOf(price, maximum)];
之后,添加下面的方法,
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
}