循环在从文本文件读取时未正确赋值。

huangapple 未分类评论47阅读模式
英文:

Loop is not properly assigning values when reading from text file

问题

很显然,我对Java还很陌生,我正在尝试读取一个文本文件并将其放入一个循环中。循环的每次迭代都会读取一行并将其分配给一个变量。我的类中有一些方法可以帮助实现这一点。但是,当我通过循环分配变量并打印它们时,什么都不打印出来。我尝试了许多不同的方法,并花了几个小时尝试让它工作,但没有成功。
以下是我的代码:

public void readFile(File value) {
    try {
        Scanner inputFile = new Scanner(value);
        for (int i = 0; i == 4; i++) {
            switch(i) {
                case 0:
                    setId(inputFile.nextLine());
                case 1:
                    setDescription(inputFile.nextLine());
                case 2:
                    setDepartment(inputFile.nextLine());
                case 3:
                    setPrice(inputFile.nextLine());
                case 4:
                    setLocation(inputFile.nextLine());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

//toString
public String toString() {
    return id + description + department + price + location;
}

setIdsetDescription等等是我类中的方法。我有一个主程序调用这些方法:

stringClass stringTest = new stringClass();
File myFile = new File("readFromFile.txt");
try {
    stringTest.readFile(myFile);
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(stringTest);

然而,当它打印时,它只打印出null(这是我在默认构造函数中为id/description等变量赋予的默认值)。非常感谢您的任何帮助!

英文:

So obviously I'm really new to Java and I'm trying to read a text file and put in a loop. Each iteration of the loop will read one line and assign it to a variable. I have a few methods inside my class that will help with this. However, when I assign the variables through the loop, and print them out, it prints out nothing. I've tried many different things and spent hours trying to get this to work but to no avail.
Here is my code:

public void readFile(File value) {
	try {
		Scanner inputFile = new Scanner(value);
		for (int i = 0; i == 4; i++) {
			switch(i) {
			case 0:
				setId(inputFile.nextLine());
			case 1:
				setDescription(inputFile.nextLine());
			case 2:
				setDepartment(inputFile.nextLine());
			case 3:
				setPrice(inputFile.nextLine());
			case 4:
				setLocation(inputFile.nextLine());
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
//toString
public String toString() {
	return id + description + department + price + location;
}

}

The setId, setDesc...etc are my methods inside my class. I have a main program that calls upon these methods:

stringClass stringTest = new stringClass();
	File myFile = new File("readFromFile.txt");
	try {
		stringTest.readFile(myFile);
	} catch (Exception e) {
		e.printStackTrace();
	}
	System.out.println(stringTest);

However, when it prints, it only prints out null(which is the default value I gave to the id/description/etc variables within my default constructor. Any help would be greatly appreciated thanks!

huangapple
  • 本文由 发表于 2020年4月9日 09:03:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61112381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定