Java 没有这样的元素例外

Java 没有这样的元素例外,java,tokenize,Java,Tokenize,我理解没有这样的元素例外,但我不明白我做错了什么。我需要使用标记器,以便读取标记,如“A-902”或“S-823”,并识别0处的字符,以确定员工所在的部门。Information.txt包含如下条目: String thisToken; // the first token is the employee name so skip that one token.nextToken(); // save the next token as its the one we want to look

我理解没有这样的元素例外,但我不明白我做错了什么。我需要使用标记器,以便读取标记,如“A-902”或“S-823”,并识别0处的字符,以确定员工所在的部门。Information.txt包含如下条目:

String thisToken;

// the first token is the employee name so skip that one
token.nextToken();
// save the next token as its the one we want to look at
thisToken = token.nextToken();

outFile.print(thisToken);

if(thisToken.charAt(0)=='A'){
    outFile.print(thisToken);
    outFile.print("Accounting ");

}else if(thisToken.charAt(0)=='H'){
    outFile.print(thisToken);
    outFile.print("Human Resources ");

}else if(thisToken.charAt(0)=='P'){
    outFile.print(thisToken);
    outFile.print("Production ");

}else if(thisToken.charAt(0)=='S'){
    outFile.print(thisToken);
    outFile.print("Shipping");
}
简·里弗斯,A-902,05/16/2001,1,16.25
鲍勃·考克斯,S-823,06/21/1990,2,17.50

import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;

    public class CreateFile {

    public static void main(String[] args)throws FileNotFoundException{

        File newFile = new File("Information.txt");
        Scanner readFile = new Scanner(newFile);
        PrintWriter outFile = new PrintWriter("Department.txt");

        String[] employees = new String[9];

        while(readFile.hasNext()){

            for(int i=0; i<employees.length; i++){
                employees[i] = readFile.nextLine();
            }
        }

        for(int k=0; k<employees.length; k++){

        StringTokenizer token = new StringTokenizer(employees[k],",");

        while(token.hasMoreTokens()){

                outFile.print(token.nextToken());

                if(token.nextToken().charAt(0)=='A'){
                    outFile.print(token.nextToken());
                    outFile.print("Accounting ");
                }else{

                if(token.nextToken().charAt(0)=='H'){
                    outFile.print(token.nextToken());
                    outFile.print("Human Resources ");
                }else{              

                if(token.nextToken().charAt(0)=='P'){
                    outFile.print(token.nextToken());
                    outFile.print("Production ");
                }else{              

                if(token.nextToken().charAt(0)=='S'){
                }
                    outFile.print(token.nextToken());
                    outFile.print("Shipping");
                }
                }
                }

        }
        }
        readFile.close();
        outFile.close();

    }



    }
import java.util.Scanner;
导入java.io.*;
导入java.util.StringTokenizer;
公共类创建文件{
公共静态void main(字符串[]args)引发FileNotFoundException{
File newFile=新文件(“Information.txt”);
扫描仪读取文件=新扫描仪(新文件);
PrintWriter outFile=新的PrintWriter(“Department.txt”);
String[]employees=新字符串[9];
while(readFile.hasNext()){

对于(int i=0;i您在
while
循环中调用了
token.nextToken()
很多次。这就是程序变得疯狂的原因


您应该只使用它一次,并将结果存储在临时变量中,然后使用它。

您在
while
循环中多次调用
token.nextToken()
。这就是程序变得疯狂的原因


您应该只使用它一次,然后将结果存储在临时变量中,并在每次调用token.nextToken()时使用它。

,您将在标记化的字符串中获得下一个标记。因此,在您的代码中,您将在每个if语句中检查不同的字符串。您需要做的只是存储正确的标记并处理它。此外,您知道标记器中的哪个标记具有所需的数据,因此无需进行while循环,只需转到所需的标记即可。最后,y我们的if-else结构在我看来很奇怪,所以我改变了它,除非我遗漏了一些东西。下面我所做的是更好的方法。因此,用如下内容替换您的while循环:

String thisToken;

// the first token is the employee name so skip that one
token.nextToken();
// save the next token as its the one we want to look at
thisToken = token.nextToken();

outFile.print(thisToken);

if(thisToken.charAt(0)=='A'){
    outFile.print(thisToken);
    outFile.print("Accounting ");

}else if(thisToken.charAt(0)=='H'){
    outFile.print(thisToken);
    outFile.print("Human Resources ");

}else if(thisToken.charAt(0)=='P'){
    outFile.print(thisToken);
    outFile.print("Production ");

}else if(thisToken.charAt(0)=='S'){
    outFile.print(thisToken);
    outFile.print("Shipping");
}

每次调用token.nextToken()时,您将在标记化的字符串中获得下一个标记。因此,在您的代码中,您将在每个if语句中检查不同的字符串。您需要做的只是存储正确的标记并处理它。此外,您知道标记器中的哪个标记具有所需的数据,因此无需进行while循环,只需转到所需的标记即可。最后,y我们的if-else结构在我看来很奇怪,所以我改变了它,除非我遗漏了一些东西。下面我所做的是更好的方法。因此,用如下内容替换您的while循环:

String thisToken;

// the first token is the employee name so skip that one
token.nextToken();
// save the next token as its the one we want to look at
thisToken = token.nextToken();

outFile.print(thisToken);

if(thisToken.charAt(0)=='A'){
    outFile.print(thisToken);
    outFile.print("Accounting ");

}else if(thisToken.charAt(0)=='H'){
    outFile.print(thisToken);
    outFile.print("Human Resources ");

}else if(thisToken.charAt(0)=='P'){
    outFile.print(thisToken);
    outFile.print("Production ");

}else if(thisToken.charAt(0)=='S'){
    outFile.print(thisToken);
    outFile.print("Shipping");
}

我以为If语句只是检查了条件?谢谢,我会在这方面做更多的工作。@HermesTrismegistus。是的,你只检查了下一个令牌的条件。但是你读的不止这些。再次感谢,我会玩它。我以为If语句只是检查了条件?谢谢,我会在这方面做更多。@HermesTrismegistus。是的h您只检查了下一个令牌的条件。但是您阅读的内容不止这些。再次感谢您,我将使用它。每次您调用
token.nextToken()时
它将返回下一个令牌,然后前进到下一个令牌,准备再次调用。您可能希望每次迭代只调用一次。是的,这是完全正确的。每次调用
token.nextToken()
它将返回下一个令牌,然后前进到下一个令牌,准备再次调用。你可能只想在每次迭代中调用它一次。是的,那完全正确。你是!邋遢…,呵呵..冷静的家伙…你调用了
.nextToken
两次。啊,是的,我明白了,是If/Else而不是嵌套If。这很有意义。谢谢!哈哈我在做了一半的时候提出了最后一个答案…这应该可以解决你的问题你是!邋遢…,嘿嘿…冷静的家伙…你打了两次电话给
.nextToken
。啊,是的,我明白了,是If/Else而不是嵌套If。这很有意义。谢谢!哈哈,对不起,我在做了一半的时候提出了最后一个答案…这应该可以解决你的问题