Java 如何将文件中的变量存储到对象数组中的对象中?

Java 如何将文件中的变量存储到对象数组中的对象中?,java,arrays,file,Java,Arrays,File,自从上学期编程以来,我一直在尝试这样做。我已经能够从一个.txt文件中读取数字,看起来好像有什么东西存储在对象数组中,但是它给了我不同的数字,比如49和-1,这些在文件中找不到 这是我试过的 BufferedReader read = new BufferedReader(new FileReader("weights.txt")); //size of the array. int size = 15; Weight[] weightArray = new Weight

自从上学期编程以来,我一直在尝试这样做。我已经能够从一个.txt文件中读取数字,看起来好像有什么东西存储在对象数组中,但是它给了我不同的数字,比如49和-1,这些在文件中找不到

这是我试过的

BufferedReader read = new BufferedReader(new FileReader("weights.txt"));

//size of the array.
int size = 15;

Weight[] weightArray = new Weight[size];

String line;
try {
while(( line = read.readLine()) != null) {
    
//next line.    
double ounce = read.read();
int pound = read.read();


for(int i = 0; i < size; i++){

read.readLine();

weightArray[i] = new Weight(ounce, pound); 

}

}//end while.

}catch(InputMismatchException e) {
    e.getMessage();
}catch(NoSuchElementException e) {
    e.getMessage();
}finally {
    read.close();
}

for(int i = 0; i < 15; i++) {
    
System.out.println(weightArray[i]);
} 
BufferedReader read=new BufferedReader(new FileReader(“weights.txt”);
//数组的大小。
int size=15;
权重[]权重数组=新权重[大小];
弦线;
试一试{
而((line=read.readLine())!=null){
//下一行。
双盎司=read.read();
int pound=read.read();
对于(int i=0;i
我试图将这些数字放在我正在创建的对象中:

20 10 11 11 14 15 12 12 6 1 1 1 4 5 1 9 10 99 31 23 12 12 17 17 83 42 12 45 1254

最后是我的体重等级:

public class Weight {

//Declare variables.
    private double ounce = 0;
    private int pound = 0;
    private static final double ouncesToPound = 16;
 

public Weight(double ounce, int pound){

    this.ounce = ounce;
    this.pound = pound;
}//end of constructor.


/*2.A public instance method named lessThan that accepts one weight as a parameter and returns 
 * whether the weight object 
 * on which it is invoked is less than the weight supplied as a parameter*/
public void lessThan(Weight a) {
    
//IT WORKS!
if(a.ounce < ounce && a.pound < pound) {
    
    System.out.println("The parameter is less than the object!\n"); 
}else {

    System.out.println("The object is less than the parameter!\n");
    
}//end of first if statement.

}//end of lessThan.

/*A public instance method named addTo that accepts one weight as a parameter and adds the weight supplied 
 * as a parameter to the weight object on which it is invoked. It should normalize the result*/

public void addTo(Weight a) {
     
    
    double ounceResult = 0;
    int poundResult = 0;
    
    ounceResult = a.ounce + ounce;
    poundResult = a.pound + pound;

    
    
    int result = (int) (ounceResult + poundResult); 
    
    normalize(ounceResult);
    //System.out.println("Added ounces: " + ounceResult);
    System.out.println("Pounds: " + poundResult);
    System.out.println("Total weight: " + result + "\n");

}//end addTo.

/*A public instance method named divide that accepts an integer divisor as a parameter. 
 * It should divide the weight object on which the method is invoked by the supplied 
 * divisor and normalize the result*/
    
public void divide(int a){
    

    int result = (int) (ounce + pound);  
    int divisionResult = result / a;

    System.out.println("Division of the parameter you entered against the object is: " + divisionResult + "\n");
    
    
}//end of divide.

/*A public instance toString method that returns a string that looks as follows: 
 * x lbs y oz, where x is the number of pounds and y the number of ounces. 
 * The number of ounces should be displayed with three places to the right of the decimal.*/

public String toString() {
    
    return pound + " lbs" + " " + String.format("%.3f",ounce) + " oz";  
    
    
}//end toString.

/*A private instance method toOunces that returns 
 * the total number of ounces in the weight object on which is was invoked*/

private double toOunces() {


    double result = ouncesToPound * pound;

    double totalOunces = result + ounce;
    
    return totalOunces;
    
}//end toOunces

/*A private instance method 
 * normalize that normalizes the weight on which it was invoked by ensuring that the number of ounces is less than 
 * the number of ounces in a pound*/

private void normalize(double ounces) {

    System.out.println("Ounces: " + String.format("%.3f",ounces));

}//end normalize.

}//end of class. 
公共类权重{
//声明变量。
私人双盎司=0;
私人整数磅=0;
专用静态最终双盎司Topound=16;
公众重量(双盎司,国际磅){
这个。盎司=盎司;
这个。磅=磅;
}//构造函数结束。
/*2.一个名为lessThan的公共实例方法,它接受一个权重作为参数并返回
*是否重量对象
*在其上调用的权重小于作为参数提供的权重*/
公共空间小于(重量a){
//它起作用了!
如果(一盎司<盎司和一磅<磅){
System.out.println(“参数小于对象!\n”);
}否则{
System.out.println(“对象小于参数!\n”);
}//第一个if语句的结尾。
}//莱斯特山的尽头。
/*名为addTo的公共实例方法,它接受一个权重作为参数,并添加提供的权重
*作为调用它的权重对象的参数。它应该规范化结果*/
公共空间添加到(重量a){
双盎司结果=0;
int-poundResult=0;
盎司结果=一盎司+盎司;
poundResult=一磅+一磅;
整数结果=(整数)(盎司苏丹+磅苏丹);
正常化(盎司结果);
//System.out.println(“添加盎司:+ounceResult”);
系统输出打印项次(“磅:+poundResult”);
System.out.println(“总重量:+结果+”\n”);
}//结束添加到。
/*名为divide的公共实例方法,它接受整数除数作为参数。
*它应该划分由提供的
*除数并对结果进行规格化*/
公共空间分割(INTA){
整数结果=(整数)(盎司+磅);
int division result=结果/a;
System.out.println(“您输入的参数对对象的除法为:“+divisionResult+”\n”);
}//分水岭的尽头。
/*一个公共实例toString方法,返回如下所示的字符串:
*x磅y盎司,其中x是磅数,y是盎司数。
*盎司数应显示在小数点右侧三位*/
公共字符串toString(){
返回英镑+“磅”+“+字符串.格式(“%.3f”,盎司)+“盎司”;
}//结束toString。
/*私有实例方法将返回
*调用的权重对象中的盎司总数*/
私人双喇叭(){
双倍结果=盎司至磅*磅;
两倍总盎司=结果+盎司;
返回总盎司;
}//尾翼
/*一种私有实例方法
*normalize,通过确保盎司数小于
*一磅中的盎司数*/
私人空白正常化(双盎司){
System.out.println(“盎司:+String.format(“%.3f”,盎司));
}//结束正常化。
}//下课了。

我只是想知道我做错了什么。为什么这些数字不放在对象数组中的对象中?如果有人能为我指出正确的方向,我将不胜感激!我想了解这个概念,这样我就能成为一名更好的程序员。

更新:看起来我可以拆分字符串,以便将double和integer解析为对象权重();然而,我在这方面没有任何成功。