目前,我正在尝试从有关宠物和医生的文本文件导入数据,并将其发送到我的“petArray”和“doctorArray”。
我对 Java 非常陌生,因此遇到了很大的困难。这就是我试图做的,但似乎不太有效。
请参阅随附的 Java 代码和文本文件(链接中的屏幕截图)。
public void readFile() {
String fileName = "VetManagement.txt";
Scanner inputStream = null;
try {
inputStream = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("Error opening file: " + fileName);
System.exit(0);
}
while (inputStream.hasNextLine()) {
String line = inputStream.nextLine();
String initName = "";
String initSize = "";
String initType = "";
String initDoctor = "";
double initWeight = 0.0;
int initAge = 0;
if (line.equals("Pets")) {
inputStream.nextLine();
if (line.equals("type cat")) {
initType = "cat";
System.out.print(initType);
} else if (line.equals("type dog")) {
initType = "dog";
System.out.print(initType);
}
inputStream.nextLine();
if (line.equals("size small")) {
initSize = "small";
} else if (line.equals("size medium")) {
initSize = "medium";
} else if (line.equals("size large")) {
initSize = "large";
} else System.out.println("error");
inputStream.nextLine();
if (line.startsWith("name")) {
initName = inputStream.next();
} else {
System.out.println("error");
}
inputStream.nextLine();
if (line.startsWith("weight")) {
initWeight = inputStream.nextDouble();
} else {
System.out.println("error");
}
inputStream.nextLine();
if (line.startsWith("age")) {
initAge = inputStream.nextInt();
} else {
System.out.println("error");
}
inputStream.nextLine();
if (line.startsWith("doctor")) {
initDoctor = inputStream.toString();
} else {
System.out.println("error");
}
petArray[sumPets] = new Pet();
petArray[sumPets].setType(initType);
petArray[sumPets].setSize(initSize);
petArray[sumPets].setName(initName);
petArray[sumPets].setWeight(initWeight);
petArray[sumPets].setAge(initAge);
petArray[sumPets].setDoctorName(initDoctor);
} else if (line.equals("Doctors")) ;
}
inputStream.close();
}
文本文件:
Pets type cat size small name Lara weight 4 age 5 doctor Joao type dog size large name Biro weight 15 age 12 doctor Maria type cat size large name Benny weight 7 age 10 doctor no doctor assigned Doctors name Joao specialisation cat name Maria specialisation dog
最佳答案
nextLine
可以轻松解决问题,无需使工作过于复杂
由于文本文件包含格式类似于
的信息type cat
size small
name Lara
weight 4
age 5
doctor Joao
您可以使用
轻松地将信息存储在所需的变量中nextLine = inputStream.nextLine().split(" ");
其中 nextLine[0]
代表第一列,nextLine[1]
代表第二列
我希望这有助于让我知道您是否有任何其他问题,这里是完整代码(如果您需要)
public static void readFile() {
String fileName = "F:\\document\\eclipse\\JavaAZ\\src\\VetManagement.txt";
Scanner inputStream = null;
try {
inputStream = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("Error opening file: " + fileName);
System.exit(0);
}
String[] name = new String[100];
String[] size = new String[100];
String[] type = new String[100];
String[] doctor = new String[100];
double[] weight = new double[100];
int[] age = new int[100];
if (inputStream.hasNextLine()) {
String[] nextLine = inputStream.nextLine().split(" ");
int petCounter = 0;
int doctorCounter = 0;
String workingArray = new String(nextLine[0]);
while(inputStream.hasNextLine()) {
if(workingArray.equals("Pets")) {
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("Doctors")) {
workingArray = "Doctors";
continue;
}
if (nextLine[0].equals("type")) {
type[petCounter] = nextLine[1];
//System.out.println(type);
}
else System.out.println("type error");
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("size")) {
size[petCounter] = nextLine[1];
//System.out.println(size);
}
else System.out.println("size error");
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("name")) {
name[petCounter] = nextLine[1];
//System.out.println(name);
}
else System.out.println("name error");
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("weight")) {
weight[petCounter] = Double.parseDouble(nextLine[1]);
//System.out.println(weight);
}
else System.out.println("weight error");
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("age")) {
age[petCounter] = Integer.parseInt(nextLine[1]);
//System.out.println(age);
}
else System.out.println("age error");
nextLine = inputStream.nextLine().split(" ");
if (nextLine[0].equals("doctor")) {
doctor[petCounter] = nextLine[1];
//System.out.println(doctor);
}
else System.out.println("doctor error");
petCounter++;
}
else if(workingArray.equals("Doctors")) {
// CODE HERE
doctorCounter++;
break;
}
}
}
System.out.println("PET NAME: "+name[0]+" and its Weight: "+weight[0]);
inputStream.close();
}
关于java - 从文本文件导入到 Java 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50753364/