0

そのような要素がないという例外は理解していますが、何が間違っているのかわかりません。Tokenizer を使用して、「A-902」や「S-823」などのトークンを読み取り、0 の文字を識別して、従業員が所属している部門を特定できるようにする必要があります。Information.txt には、次のようなエントリが含まれています。

ジェーン・リバーズ、A-902、2001 年 5 月 16 日、1、16.25
ボブ・コックス、S-823、1990 年 6 月 21 日、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();

    }



    }
4

2 に答える 2

3

ループ内token.nextToken()で何度も呼び出しています。whileそれがプログラムを狂わせる原因です。

一度だけ使用し、結果を一時変数に格納して使用する必要があります。

于 2013-06-21T16:53:35.190 に答える
0

token.nextToken() を呼び出すたびに、トークン化した文字列内の次のトークンを取得します。したがって、コードでは、すべての if ステートメントで異なる文字列をチェックしています。あなたがする必要があるのは、正しいトークンを保存してそれを処理することだけです。また、トークナイザー内のどのトークンが必要なデータを持っているかを知っているので、while ループは必要なく、必要なトークンに移動するだけです。最後に、あなたの 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");
}
于 2013-06-21T17:05:41.197 に答える