java - 如何从文件读取并添加到对象数据?

标签 java

17  5
12  8
15  22
17  11
33  21
43  15
15  4
44  35
23  19
10  23
55  39
8   6
21  9
20  28
20  13
45  29
18  16
21  19
68  55
10  16
33  54
3   1
5   9

我正在尝试读取类似于上面输入的 input.txt 文件,但问题是该程序仅读取文本文件的最后一行。

这是我的代码:

import java.io.FileNotFoundException;
import java.io.*;
import java.io.IOException;
import java.util.*;
import java.util.Scanner;

/**
 * 
 */
public class ReadIn {


public static void main(String[] args) throws IOException {

    //List<Integer> value = new ArrayList<Integer>();
    //List<Integer> weight = new ArrayList<Integer>();
    FileReader readFile = new FileReader ("C:\\Users\\owner\\IdeaProjects\\knapsack\\src\\input");
    BufferedReader br = new BufferedReader(readFile);
    Scanner input = new Scanner(br);
    Scanner keyboard = new Scanner(System.in);
    //List<Items> items = new ArrayList<Items>();


    Items[] items= new Items[23];//this only creates references that are set to their default value null which throws a null exception
    for(int i =0 ; i <items.length; i++) {//fixes null exception

        items[i] = new Items();//fully creates the objects

        while (input.hasNext()) {//read in file input to object data
            items[i].setValue(input.nextInt());
            items[i].setWeight(input.nextInt());
            items[i].setId(i);
        }
    }
    input.close();
    input.reset();
    System.out.println("What is the max capacity for the knapsack? ");
    Integer maxCapacity = keyboard.nextInt();
    System.out.println(maxCapacity);
    int[] maxWeight = new int[maxCapacity];//creates int array so we can use the index as the maxWeight as the number of fields
    printArray(items);

}
    public static void printArray(Items[] x) {

        for (int i = 0; i < x.length; i++) {
            x[i] = new Items();
            System.out.println(x[i].getValue() + " " + x[i].getWeight() + " " + x[i].getId() + " ");
        }
    }

}

感谢您的帮助,我被困住了! 我还附加了我尝试使用的 Items 类

    public class Items {
        private Integer value ;
        private Integer weight;
        private Integer Id;

        public Items(){
            this.value = 0;
            this.weight = 0;
            this.Id =0;
        }

        public Items(Integer v, Integer w, Integer ID){
            this.value = v;
            this.weight = w;
            this.Id = ID;
        }


        public Integer getValue() {
            return value;
        }

        public void setValue(Integer value) {
            this.value = value;
        }

        public Integer getWeight() {
            return weight;
        }

        public void setWeight(Integer weight) {
            this.weight = weight;
        }

        public Integer getId() {
            return Id;
        }

        public void setId(Integer id) {//sets id
            Id = id;
        }
    }

代码到此结束

最佳答案

也许这会有用:

更新:

您应该声明并初始化 j 变量,并在 while 循环中声明变量 j 在每次循环中加 1:

int j=0; // ----> initialize j variable
while (input.hasNext()) {
    items[j].setValue(input.nextInt());
    items[j].setWeight(input.nextInt());
    items[j].setId(j);
    j++; // ----> increment 1 y each loop
}

在 for 循环内的 printArray (Items[] x) 方法中,不应定义以下内容:

x[i] = new Items();

因为在每次迭代中都会创建一个新对象,并且在打印时它将是 0 0 0

<小时/>

示例如下:

public class MyTree {
    public static void main(String[] args) throws IOException {

        FileReader readFile = new FileReader("C:\\Users\\owner\\IdeaProjects\\knapsack\\src\\input");
        BufferedReader br = new BufferedReader(readFile);
        Scanner input = new Scanner(br);
        Scanner keyboard = new Scanner(System.in);

        Items[] items = new Items[23];//this only creates references that are set to their default value null which throws a null exception
        for (int i = 0; i < items.length; i++) {//fixes null exception
            items[i] = new Items();//fully creates the objects
        }

        int j=0; // ----> initialize j variable
        while (input.hasNext()) {
            items[j].setValue(input.nextInt());
            items[j].setWeight(input.nextInt());
            items[j].setId(j);
            j++; // ----> increment 1 y each loop
        }

        input.close();
        input.reset();
        System.out.println("What is the max capacity for the knapsack? ");
        Integer maxCapacity = keyboard.nextInt();
        System.out.println(maxCapacity);
        int[] maxWeight = new int[maxCapacity];//creates int array so we can use the index as the maxWeight as the number of fields
        printArray(items);

    }

    public static void printArray(Items[] x) {

        for (int i = 0; i < x.length; i++) {
            //x[i] = new Items();  //It shouldn't be here, because in each iteration create a new object
            System.out.println(x[i].getValue() + " " + x[i].getWeight() + " " + x[i].getId() + " ");
        }
    }
}

关于java - 如何从文件读取并添加到对象数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43105547/

相关文章:

Java访问Thread的类属性

java - MyBatis - 如何创建 w 动态 WHERE 子句

java - Weka 中的 ID 属性

java - 使用 invalidate、validate 和 repaint 时 JPanel 不更新

java - 当应用程序本身依赖第三方 OAuth 时,如何提供基于 OAuth 的 API

javascript - 从 Postman 到 JBoss 的 HTTP 请求

java - 如何从菜单按键调用上下文菜单?

java - keystore 的 setkeyentity 的参数

java - HermiT Resoner 与 Protege OWL API

java - 当多个线程想要访问 ReentrantReadWriteLock 时会发生什么?