错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at PetOrganizer.main(PetOrganizer.java:23)
这是与Pet Class交互的主要类,也在下面发布。第一次使用FileWriter,如果我在这里做了一些彻底错误的事情,请告诉我。我的主类代码的第23行是"字符串名称"。
编辑:添加了input.txt文件,还有一个output.txt文件,该程序要打印到该文件但它是空的
代码:
import java.util.Scanner;
import java.io.*;
public class PetOrganizer {
public static void main(String[] args) throws IOException
{
//Intialize Text File along with Writer variables
File TextFile = new File("input.txt");
File OutputFile = new File("output.txt");
FileWriter fWriter = new FileWriter(TextFile, true);
FileWriter fOWriter = new FileWriter(OutputFile);
PrintWriter pWriter = new PrintWriter(fOWriter);
//Initialize Scanner Variable
Scanner in = new Scanner(TextFile);
//Gets input from user for pet #1
System.out.println("Please enter what you would like your first pet to be named: ");
这在代码中被剪切是为了显示导致上面错误的第23行
String name = in.nextLine();
System.out.println("Please enter how old you would like "+name+" to be:");
int age = in.nextInt();
System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
String pettype = in.nextLine();
System.out.println("Please enter the name of "+name+"'s owner");
String owner = in.nextLine();
//Creates first instance of Pet and uses input from user
Pet p1 = new Pet();
p1.setPetAge(age);
p1.setPetName(name);
p1.setPetType(pettype);
p1.setPetOwner(owner);
//Gets input from user for pet #1
System.out.println("Please enter what you would like your second pet to be named: ");
name = in.nextLine();
System.out.println("Please enter how old you would like "+name+" to be:");
age = in.nextInt();
System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
pettype = in.nextLine();
System.out.println("Please enter the name of "+name+"'s owner");
owner = in.nextLine();
//Creates first instance of Pet and uses input from user
Pet p2 = new Pet();
p2.setPetAge(age);
p2.setPetName(name);
p2.setPetType(pettype);
p2.setPetOwner(owner);
//Gets input from user for pet #1
System.out.println("Please enter what you would like your third pet to be named: ");
name = in.nextLine();
System.out.println("Please enter how old you would like "+name+" to be:");
age = in.nextInt();
System.out.println("Please enter what type of pet you would like(Dog, Cat, Mouse, Ferret): ");
pettype = in.nextLine();
System.out.println("Please enter the name of "+name+"'s owner");
owner = in.nextLine();
//Creates first instance of Pet and uses input from user
Pet p3 = new Pet();
p3.setPetAge(age);
p3.setPetName(name);
p3.setPetType(pettype);
p3.setPetOwner(owner);
//Outputs all of the instances of Pet with the info gathered from user input
pWriter.println("/////////////////////////////////////////////////////////");
pWriter.println("///////////////////PATIENT LIST//////////////////////////");
pWriter.println("Pet 1");
pWriter.println("Pet 1 Age: "+ p1.getPetAge());
pWriter.println("Pet 1 Name: "+p1.getPetName());
pWriter.println("Pet 1 Type: "+p1.getPetType());
pWriter.println("Pet 1 Owner: "+p1.getPetOwner());
pWriter.println("Pet 2");
pWriter.println("Pet 2 Age: "+ p2.getPetAge());
pWriter.println("Pet 2 Name: "+p2.getPetName());
pWriter.println("Pet 2 Type: "+p2.getPetType());
pWriter.println("Pet 2 Owner: "+p2.getPetOwner());
pWriter.println("Pet 3");
pWriter.println("Pet 3 Age: "+ p3.getPetAge());
pWriter.println("Pet 3 Name: "+p3.getPetName());
pWriter.println("Pet 3 Type: "+p3.getPetType());
pWriter.println("Pet 3 Owner: "+p3.getPetOwner());
pWriter.println("//////////////////////////////////////////////////////////");
pWriter.println("//////////////////////////////////////////////////////////");
fWriter.close();
pWriter.close();
in.close();
}
}
宠物类代码:
public class Pet {
//Class Private Data Members
private String petName;
private int petAge;
private String petType; //Cat, Dog, Ferret, etc
private String petOwner;
//Constructor
public Pet()
{
}
//Public Methods
public String getPetName() {
return petName;
}
public void setPetName(String name) {
petName = name;
}
public int getPetAge() {
return petAge;
}
public void setPetAge(int age) {
petAge = age;
}
public String getPetType() {
return petType;
}
public void setPetType(String type) {
petType = type;
}
public String getPetOwner() {
return petOwner;
}
public void setPetOwner(String owner) {
petOwner = owner;
}
}
输入文件:
George
18
Dog
Ben
Fred
19
Ferret
Carl
Steve
20
Cat
Dog
答案 0 :(得分:0)
如果您需要使用文件,使用bufferedReader,您可以逐行浏览文本文件,直到完成为止。例如,如果您有一个
的文本文件DOG1
DOG2
第一次通过while循环时,字符串currentWord为“Dog1” 第二次通过while循环时,字符串currentWord为“Dog2” 等
public static void main(String[] args)
{
try (BufferedReader br = new BufferedReader(new FileReader("C:\\words.txt")))
{
String currentWord;
while ((currentWord = br.readLine()) != null)
{
//USE EACH OF YOUR STRINGS HERE i.e String dogName = currentWord
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
“C:\ words.txt”指的是您要使用的文件的位置。 使用这个只是导入
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;