Java 创建用于读取txt文件的构造函数
我正在创建一个程序,将产生一个棒球队的统计数据 我试图创建一个构造函数,将文件读入teamName实例变量和battingAverages数组 txt文件包含一个单词的球队名称,后跟20次击球平均数 “Tars 0.592 0.427 0.194 0.445 0.127 0.483 0.352 0.190 0.335 0.207 0.116 0.387 0.243 0.225 0.4010.382 0.556 0.319 0.475 0.279”Java 创建用于读取txt文件的构造函数,java,arrays,text,constructor,Java,Arrays,Text,Constructor,我正在创建一个程序,将产生一个棒球队的统计数据 我试图创建一个构造函数,将文件读入teamName实例变量和battingAverages数组 txt文件包含一个单词的球队名称,后跟20次击球平均数 “Tars 0.592 0.427 0.194 0.445 0.127 0.483 0.352 0.190 0.335 0.207 0.116 0.387 0.243 0.225 0.4010.382 0.556 0.319 0.475 0.279” 我正在努力找到如何着手并开始这项工作?好吧,如果
我正在努力找到如何着手并开始这项工作?好吧,如果这些都在一个特定文件的一行上,那么您可以构造一个bufferedreader来读取文件的第一行,根据空格拆分行,然后解析团队名称和击球平均数
BufferedReader br = new BufferedReader(new FileReader("myfile.txt"));
String[] line = br.readLine().split(" ");
br.close();
teamName = line[0];
battingAverages = new int[20];
for(int i = 0; i < 20; i++)
battingAverages[i] = Integer.parseInt(line[i+1]);
BufferedReader br=newbufferedreader(newfilereader(“myfile.txt”);
String[]line=br.readLine().split(“”);
br.close();
团队名称=行[0];
battingAverages=新整数[20];
对于(int i=0;i<20;i++)
battingAverages[i]=整数.parseInt(行[i+1]);
这些可能会抛出IOException,您需要捕获这些异常。我认为Java 7有一种方法可以自动处理这些类型的错误(对此不确定),但由于我对Java 7新增的功能不熟悉,我会手动检查这些异常。如果这些异常都在特定文件的一行上,那么您可以做的是构造一个bufferedreader来读取文件的第一行,根据空格分割线,然后解析队名和击球平均数
BufferedReader br = new BufferedReader(new FileReader("myfile.txt"));
String[] line = br.readLine().split(" ");
br.close();
teamName = line[0];
battingAverages = new int[20];
for(int i = 0; i < 20; i++)
battingAverages[i] = Integer.parseInt(line[i+1]);
BufferedReader br=newbufferedreader(newfilereader(“myfile.txt”);
String[]line=br.readLine().split(“”);
br.close();
团队名称=行[0];
battingAverages=新整数[20];
对于(int i=0;i<20;i++)
battingAverages[i]=整数.parseInt(行[i+1]);
这些可能会抛出IOException,您需要捕获这些异常。我认为Java 7有一种方法可以自动处理这些类型的错误(对此不确定),但由于我对Java 7新增的功能不熟悉,我只想手动检查这些异常。您需要使用
BufferedReader
,FileInputStream
和InputStreamReader
。您的file.txt应该在每一行上都有击球平均值,如下所示
0.592
0.427
0.194
下面是一个类的示例,创建该类时,它将逐行读取文本文件,并将每一行添加到数组列表中:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Class {
ArrayList<Double> averages;
public Class() {
averages = new ArrayList<Double>();
try {
FileInputStream in = new FileInputStream("inputFile.txt"); //your file path/name
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine())!= null) averages.add(Double.parseDouble(strLine));
}catch(Exception e){
System.out.println(e);
}
}
}
导入java.io.BufferedReader;
导入java.io.FileInputStream;
导入java.io.InputStreamReader;
导入java.util.*;
公共课{
ArrayList平均值;
公共类(){
平均值=新的ArrayList();
试一试{
FileInputStream in=newfileinputstream(“inputFile.txt”);//您的文件路径/名称
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null)平均值为.add(Double.parseDouble(strLine));
}捕获(例外e){
系统输出打印ln(e);
}
}
}
希望这有帮助您需要使用
BufferedReader
、FileInputStream
和InputStreamReader
。您的file.txt应该在每一行上都有击球平均值,如下所示
0.592
0.427
0.194
下面是一个类的示例,创建该类时,它将逐行读取文本文件,并将每一行添加到数组列表中:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Class {
ArrayList<Double> averages;
public Class() {
averages = new ArrayList<Double>();
try {
FileInputStream in = new FileInputStream("inputFile.txt"); //your file path/name
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine())!= null) averages.add(Double.parseDouble(strLine));
}catch(Exception e){
System.out.println(e);
}
}
}
导入java.io.BufferedReader;
导入java.io.FileInputStream;
导入java.io.InputStreamReader;
导入java.util.*;
公共课{
ArrayList平均值;
公共类(){
平均值=新的ArrayList();
试一试{
FileInputStream in=newfileinputstream(“inputFile.txt”);//您的文件路径/名称
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦斯特林;
而((strLine=br.readLine())!=null)平均值为.add(Double.parseDouble(strLine));
}捕获(例外e){
系统输出打印ln(e);
}
}
}
希望这有帮助我运行了这个程序,它可能接近您想要的。与其创建一个混乱的构造函数,不如创建一个私有方法,构造函数将调用该方法将文件读入数组
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Baseball {
private File textFile;
private Scanner input;
private String teamName;
//this will only work if you know there will be 20 entries everytime
//otherwise I recommend loading the data into an ArrayList
private double []battingAvgs = new double[20];
public Baseball(String file){
textFile = new File(file);
readInFile(textFile);
}
//private method that reads in the file into an array
private void readInFile(File textFile){
try {
input = new Scanner(textFile);
//read first string into variable teamName
teamName = input.next();
int i=0;
//iterate through rest of file adding it to an ArrayList
while(input.hasNext()){
battingAvgs[i] = input.nextDouble();
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//print out array
public void printArray(){
for(Double a: battingAvgs){
System.out.println(a);
}
}
}
我运行了这个,这可能接近你想要的。与其创建一个混乱的构造函数,不如创建一个私有方法,构造函数将调用该方法将文件读入数组
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Baseball {
private File textFile;
private Scanner input;
private String teamName;
//this will only work if you know there will be 20 entries everytime
//otherwise I recommend loading the data into an ArrayList
private double []battingAvgs = new double[20];
public Baseball(String file){
textFile = new File(file);
readInFile(textFile);
}
//private method that reads in the file into an array
private void readInFile(File textFile){
try {
input = new Scanner(textFile);
//read first string into variable teamName
teamName = input.next();
int i=0;
//iterate through rest of file adding it to an ArrayList
while(input.hasNext()){
battingAvgs[i] = input.nextDouble();
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//print out array
public void printArray(){
for(Double a: battingAvgs){
System.out.println(a);
}
}
}
尝试使用
扫描仪
类
File file=new File("TestFile.txt"); //Create a new file
Scanner scan=new Scanner(file);//Create a Scanner object (Throws FileNotFoundException)
if(scan.hasNext()) //Check to make sure that there is actually something in the file.
{
String line=scan.nextLine(); //Read the line of data
String[] array=line.split(" "); //Split line into the different parts
teamName=array[0]; //The team name is located in the first index of the array
battingAverages=new double[array.length-1];//Create a new array to hold the batting average values
for(int i=0;i<battingAverages.length;i++) //Loop through all of the averages
{
double average=Double.parseDouble(array[i+1]);//Convert the string object into a double
battingAverages[i]=average; //Add the converted average to the array
}
System.out.print(teamName+" "+Arrays.toString(battingAverages)); //[Optional] Print out the resulting values
}
File File=new文件(“TestFile.txt”)//创建一个新文件
扫描仪扫描=新扫描仪(文件)//创建扫描仪对象(抛出FileNotFoundException)
if(scan.hasNext())//检查以确保文件中确实有内容。
{
String line=scan.nextLine();//读取数据行
String[]array=line.split(“”;//将行拆分为不同的部分
teamName=array[0];//团队名称位于数组的第一个索引中
battingAverages=new double[array.length-1];//创建一个新数组以保存击球平均值
对于(int i=0;i请尝试使用扫描仪
类
File file=new File("TestFile.txt"); //Create a new file
Scanner scan=new Scanner(file);//Create a Scanner object (Throws FileNotFoundException)
if(scan.hasNext()) //Check to make sure that there is actually something in the file.
{
String line=scan.nextLine(); //Read the line of data
String[] array=line.split(" "); //Split line into the different parts
teamName=array[0]; //The team name is located in the first index of the array
battingAverages=new double[array.length-1];//Create a new array to hold the batting average values
for(int i=0;i<battingAverages.length;i++) //Loop through all of the averages
{
double average=Double.parseDouble(array[i+1]);//Convert the string object into a double
battingAverages[i]=average; //Add the converted average to the array
}
System.out.print(teamName+" "+Arrays.toString(battingAverages)); //[Optional] Print out the resulting values
}
File File=new File(“TestFile.txt”);//创建一个新文件
Scanner scan=新建扫描仪(文件);//创建扫描仪对象(抛出FileNotFoundException)
if(scan.hasNext())//检查以确保文件中确实有内容。
{
String line=scan.nextLine();//读取数据行
String[]array=line.split(“”;//将行拆分为不同的部分
teamName=array[0];//团队名称位于数组的第一个索引中
battingAverages=new double[array.length-1];//创建一个新数组以保存击球平均值
对于(int i=0;i)您的问题非常模糊,您希望获得构造函数方面的帮助还是阅读文本文件方面的帮助。显示您的代码到目前为止您可以使用系统使用扫描仪类。在中,扫描。您的问题非常模糊,您希望获得构造函数方面的帮助还是阅读文本文件方面的帮助。显示