[英]how to read from file and add to object data?
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() + " ");
}
}
}
我将删除for循环,使用列表而不是数组,并读取每个项目并将其添加到列表中:
public static void main(String[] args) throws FileNotFoundException {
FileReader readFile = new FileReader("C:\\Users\\owner\\IdeaProjects\\knapsack\\src\\input");
BufferedReader br = new BufferedReader(readFile);
Scanner input = new Scanner(br);
List<Item> items= new LinkedList<>();
int i = 0;
while (input.hasNext()) {
Item item = new Item();
item.setValue(input.nextInt());
item.setWeight(input.nextInt());
item.setId(i);
items.add(item);
i++;
}
input.close();
input.reset();
//...
}
另外两个评论:
Items
应称为Item
toString()
方法类Item
这种方式它会很容易打印 您的问题出在下面的for循环中。 在不实际更改目的地的情况下进行了while循环,因此所有内容都将被覆盖,只有最后输入有效。 有两种解决方法。 在正确设置输入文件格式的前提下,这两种方法都只能发挥最佳性能。 其他更改将需要进行调整以使其无法通过验证。
//---------Option 1: Remove the While-loop----
for(int i =0 ; i <items.length; i++) {//fixes null exception
items[i] = new Items();//fully creates the objects
items[i].setValue(input.nextInt());
items[i].setWeight(input.nextInt());
items[i].setId(i);
}
//---------Option 2: Let the for-loop finish before the while-loop ------
for(int i =0 ; i <items.length; i++) { //fixes null exception
items[i] = new Items(); //fully creates the objects
}
int k=0; //Need a variable for count
while (input.hasNext()) {
items[k].setValue(input.nextInt());
items[k].setWeight(input.nextInt());
items[k].setId(k);
k++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.