weixin_51936504
2021-10-13 18:00
采纳率: 100%
浏览 184

Exception in thread "main" java.util.NoSuchElementException很多次都遇到这个错误,该改改哪里


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBook {
    String name;
    String sex;
    int phonenumber;
    AddressBook(String name,String sex,int phonenumber){
        this.name=name;
        this.sex=sex;
        this.phonenumber=phonenumber;
    }
    String getname() {
        return name;
    }
    String getsex() {
        return sex;
    }
    int getp() {
        return phonenumber;
    }
    void setn(String name) {
        this.name=name;
    }
    void seti(String sex) {
        this.sex=sex;
    }
    void setp(int phonenumber) {
        this.phonenumber=phonenumber;
    }
    
    public static ArrayList<AddressBook> addressbook=new ArrayList<AddressBook>();
    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        PrintWriter resultwriter=null;
        Scanner readQuery=null;
        Scanner readRecord=null;
        try {
        File record=new File("D:\\java\\javaeclipse\\java_Sourse\\src\\PTA\\record.txt");
        File query=new File("D:\\java\\javaeclipse\\java_Sourse\\src\\PTA\\query.txt");
        File result=new File("D:\\java\\javaeclipse\\java_Sourse\\src\\PTA\\result.txt");
        resultwriter=new PrintWriter(result);
        readRecord=new Scanner(new FileReader(record));
        readQuery=new Scanner(new FileReader(query));
        int lineNumber=readRecord.nextInt();
        for(int i=0;i<lineNumber;i++) {
            for(int j=0;j<3;j++)
            {
                String a=new String(readRecord.next());
                String b=new String(readRecord.next());
                int c=readRecord.nextInt();
                addressbook.add(new AddressBook(a,b,c));
            }
        }
        int column=readQuery.nextInt();
        Object Obj=(Object)readQuery.next();
        
            for(int i=0;i<lineNumber;i++) {
                AddressBook e=addressbook.get(i);
                if(column==1&&e.getname()==Obj) {
                    resultwriter.print(e.getname()+" ");
                    resultwriter.print(e.getsex()+" ");
                    resultwriter.println(e.getp());
                }
                if(column==2&&e.getsex()==Obj) {
                    resultwriter.print(e.getname()+" ");
                    resultwriter.print(e.getsex()+" ");
                    resultwriter.println(e.getp());
                }
                if(column==3&&e.getp()==(int)Obj) {
                    resultwriter.print(e.getname()+" ");
                    resultwriter.print(e.getsex()+" ");
                    resultwriter.println(e.getp());
                }
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}

img

不知道到底应该怎么改,很多次都遇到这种情况。有没有哪个同学帮我看看。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题