Java 从文件中添加一行数字,并将平均值存储到双向量中
我需要使用双向量来存储一个学生的平均值 im读取的文件设置如下:Java 从文件中添加一行数字,并将平均值存储到双向量中,java,string,file,vector,double,Java,String,File,Vector,Double,我需要使用双向量来存储一个学生的平均值 im读取的文件设置如下: 2 //num of students 60 //total possible score John //name 4 16 9 7 10 //scores private String myFileName; private BufferedReader myFile; public FileIn(String filename) { myFileName = filename; try {
2 //num of students
60 //total possible score
John //name
4 16 9 7 10 //scores
private String myFileName;
private BufferedReader myFile;
public FileIn(String filename)
{
myFileName = filename;
try
{
myFile = new BufferedReader(new FileReader(myFileName));
}
catch(Exception e)
{
e.printStackTrace();
}
finally{}
}
public String read()
{
String myLine = new String();
try
{
myLine = myFile.readLine();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{}
return myLine;
}
所以我需要将字符串转换为double,将行中的所有整数相加,除以平均值,然后将平均值存储到double向量中
到目前为止,我掌握的代码是:
public static String line;
public static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("enter the name of your file");
String filename = in.next();
FileIn file = new FileIn(filename);
String firstLine; // String to hold first line which is number of students total in file.
String secondLine; //String to hold second line which is number of points available
ArrayList<String> students = new ArrayList<String>(); // holds the students names
//reads first line of the file. sets that number as the number of students
firstLine = file.read();
int numStu = Integer.parseInt(firstLine);
// Just to test that number is being read correctly.
System.out.println(numStu + " Number of students");
//reads the second line of the file. sets that number as the total possible points in a semester
secondLine = file.read();
int totalPoints = Integer.parseInt(secondLine);
// Just to test that number is being read correctly.
System.out.println(totalPoints + " Total possible points");
double avg = 0;
double[]vector = new double [numStu];
readFile(students,numStu,file,vector, avg);
System.out.println(students);
System.out.println(vector);
}
//puts the names into an arraylist and scores into a double vector
public static void readFile(ArrayList<String> students,int numStu, FileIn file, double[]vector, double avg)
{
for(int k=0; k<(numStu*2); k++)
{
//odd numbers are the students
if (k % 2 == 0)
students.add(file.read());
else
{
//code to read and add the numbers from one line together, and storing the added and averaged score
}
}
}
}首先!请阅读我在你的问题下的评论。学习如何以更聪明的方式提问,学习如何分而治之
这不是对你问题的直接回答,因为我认为程序员应该能够自己解决它,并学会在你的问题中使用它
完成下面的一段代码,完成后您将知道自己问题的答案
public class Foo {
public static int main(String[] args) {
String input = "10 15 20.5 70 40";
// write code to convert the input string to doubles and sum them up
double sum = ......;
// answer should equals to 155.5
}
}
提示:
String.split()
和Double.parseDouble()
,并使用for循环,您的问题是什么?@AdrianShum我想知道如何将文件中的一行数字相加,求平均值,然后将其存储到双向量中。1-从该行读取数字,2-找到平均值,3-将平均值存储在双矢量中,你有什么问题?你不知道如何从文件中读取一行吗?或者您不知道如何将字符串转换为整数/双精度?或者你不知道如何平均多个数字?或者你不知道如何将某个东西“存储”到“向量”中?下次,请把你的问题“直接”提出来。简单地引用一堆不相关的代码,让其他人猜你的问题对回答者没有帮助。学会简化问题。在您的情况下,尝试编写一段代码,其中包含一个包含数字的字符串,并将其作为双精度数添加。即使你最终失败了,让我们看看你做了什么,让人们知道你的问题是什么。我明白了。谢谢你帮助我。下次我会更清楚的!