扫描文本文件时出错显示错误“;“线程中的异常”;AWT-EventQueue-0“;java.util.NoSuchElementException“;

扫描文本文件时出错显示错误“;“线程中的异常”;AWT-EventQueue-0“;java.util.NoSuchElementException“;,java,swing,text-files,java.util.scanner,bufferedreader,Java,Swing,Text Files,Java.util.scanner,Bufferedreader,当我使用“boolean checkDuplicateNames1”方法时,我会遇到这个错误 public class checkDuplicateNames1 { public boolean checkDuplicateNames1(String name , String surname){ boolean found = false; File file = new File("transactions.txt"); try { Scanne

当我使用“boolean checkDuplicateNames1”方法时,我会遇到这个错误

public class checkDuplicateNames1 {



public boolean checkDuplicateNames1(String name , String surname){
   boolean found = false;
    File file = new File("transactions.txt");

    try {
        Scanner sc = new Scanner(new File("transactions.txt"));
        sc.useDelimiter("/");


        while(sc.hasNext()){

        String userName = sc.next();
        String userLastName = sc.next();
        String userCash = sc.next();
        String paidStatus = sc.next();

        if((userName.equals(name)&&userLastName.equals(surname))){

            found = true;

        }
        else
        {

            found = false;
        }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(checkDuplicateNames.class.getName()).log(Level.SEVERE, null, ex);
    }






 return found;



}

}
我试着使用不同的文件阅读器,比如bufferedReader,都是一样的

下面是使用“checDuplicateNames1”方法的“Loan class”方法

文件中的文本也是这样的

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    // TODO add your handling code here:


    String name = jTextField1.getText();
    String surname = jTextField4.getText();
    String cash = jTextField5.getText();
    System.out.println("loan clicked");

    try {

        String info = name + "/" + surname + "/" + cash + "/" + "unpaid";

        checkDuplicateNames1 c = new checkDuplicateNames1();
        boolean duplicated = c.checkDuplicateNames1(name,surname);

        if(duplicated == true){
            System.out.println("duplicated");
            this.currentName = name;
            this.currentSurname = surname;
          JOptionPane.showMessageDialog(null,"Person already in the Record ");
          wannaAddLoanValue w = new wannaAddLoanValue(name,surname,cash);
          w.show();



        }
        else
        {
            File file = new File("transactions.txt");
             PrintWriter writer = new PrintWriter(new FileWriter(file,true));
             String info1 = name + "/" + surname + "/" + cash + "/" + "unpaid";
             writer.println(info1); 
              writer.close();

        }



    } catch (FileNotFoundException ex) {
        Logger.getLogger(Loan.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Loan.class.getName()).log(Level.SEVERE, null, ex);
    }




}                                

您应该始终在
sc.next()之前调用
sc.hasNext()

checkDuplicateNames1()
可以重构如下:

while(sc.hasNext()){

    // This line is OK
    String userName = sc.next();

    // The following lines can cause NoSuchElementException
    String userLastName = sc.next();
    String userCash = sc.next();
    String paidStatus = sc.next();

ur类中的哪一行是
41
?i、 e哪个
next()
导致错误

我认为
hasNext()
将整个当前输入视为一个令牌,但是当您多次调用
next()
时,您只得到了第一个1,然后是异常,要解决这个问题,我认为您必须在每个
next()之前检查
hasNext()

在代码的其余部分,您可能希望检查每个变量是否为空

while(sc.hasNext()){
    String userName="",userLastName="",userCash="",paidStatus="";

    //first one already checked in while(...)
    userName = sc.next();

    if(sc.hasNext())        
        userLastName = sc.next();

    if(sc.hasNext())
        userCash = sc.next();

    if(sc.hasNext())
        paidStatus = sc.next();

    //... rest of code

比我快。回答得好,+1。你应该包括问题的解决方案我甚至使用了BufferedReader,但仍然得到了相同的错误,我还尝试扫描一行并使用数组用分隔符分割数据,但仍然是一样的。这很有效,谢谢,下面的另一个答案是:))谢谢
while(sc.hasNext()){

    // This line is OK
    String userName = sc.next();

    // The following lines can cause NoSuchElementException
    String userLastName = sc.next();
    String userCash = sc.next();
    String paidStatus = sc.next();
public boolean checkDuplicateNames1(String name, String surname) {
    boolean found = false;
    try (Scanner sc = new Scanner(new File("transactions.txt"))) {
        sc.useDelimiter("/");
        String userName = sc.hasNext() ? sc.next() : null;
        String userLastName = sc.hasNext() ? sc.next() : null;
        return userName != null && userLastName != null && userName.equals(name) && userLastName.equals(surname);            
    } catch (FileNotFoundException ex) {
        Logger.getLogger(checkDuplicateNames.class.getName()).log(Level.SEVERE, null, ex);
    }
    return found;
}
while(sc.hasNext()){
    String userName="",userLastName="",userCash="",paidStatus="";

    //first one already checked in while(...)
    userName = sc.next();

    if(sc.hasNext())        
        userLastName = sc.next();

    if(sc.hasNext())
        userCash = sc.next();

    if(sc.hasNext())
        paidStatus = sc.next();

    //... rest of code
if(!userLastName.isEmpty()) ...