Java 有没有大神帮我看一下这个怎么解决,总是显示NosuchElementException T.T
publicstaticBSTreadProfile(Stringfilename){FileinputFile=newFile(filename);Scannerin=...
public static BST readProfile(String filename){
File inputFile=new File(filename);
Scanner in=null;
try{
in=new Scanner(inputFile);
}catch(FileNotFoundException e){
System.out.println("Cannot open "+filename);
System.exit(0);
}
public static BST readProfile(String filename){
File inputFile=new File(filename);
Scanner in=null;
try{
in=new Scanner(inputFile);
}catch(FileNotFoundException e){
System.out.println("Cannot open "+filename);
System.exit(0);
}
in.useDelimiter(",");
BST tree=new BST(null);
while(in.hasNext()){
String name=in.next();
int dayofbirth=in.nextInt();(从这开始报错)
int monthofbirth=in.nextInt();
int yearofbirth=in.nextInt();
String town=in.next();
String country=in.next();
String nationality=in.next();
String interest=in.next();
String[] interests={interest};
Profile person=new Profile(name, town,country, nationality, interests, dayofbirth, monthofbirth, yearofbirth);
// why is empty?
tree.insertProfile(person);
}
return tree;
}
这个是我写的方法,在主函数里面调用方法读取TXT的时候总是显示NoSuchElement,TXT如下:
Jessie,02,11,1994,HUNAN,China,Chinese,sing;picture,
Aron,11,06,1995,SHANDONG,China,Chinese,basketball;pingpang, 展开
File inputFile=new File(filename);
Scanner in=null;
try{
in=new Scanner(inputFile);
}catch(FileNotFoundException e){
System.out.println("Cannot open "+filename);
System.exit(0);
}
public static BST readProfile(String filename){
File inputFile=new File(filename);
Scanner in=null;
try{
in=new Scanner(inputFile);
}catch(FileNotFoundException e){
System.out.println("Cannot open "+filename);
System.exit(0);
}
in.useDelimiter(",");
BST tree=new BST(null);
while(in.hasNext()){
String name=in.next();
int dayofbirth=in.nextInt();(从这开始报错)
int monthofbirth=in.nextInt();
int yearofbirth=in.nextInt();
String town=in.next();
String country=in.next();
String nationality=in.next();
String interest=in.next();
String[] interests={interest};
Profile person=new Profile(name, town,country, nationality, interests, dayofbirth, monthofbirth, yearofbirth);
// why is empty?
tree.insertProfile(person);
}
return tree;
}
这个是我写的方法,在主函数里面调用方法读取TXT的时候总是显示NoSuchElement,TXT如下:
Jessie,02,11,1994,HUNAN,China,Chinese,sing;picture,
Aron,11,06,1995,SHANDONG,China,Chinese,basketball;pingpang, 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询