0

I have a question on how to extract letters from File. This is an example line from the file I read.

ui Parker 8

I read 3 tokens and am supposed to extract 2 letters from the first token but not sure how to do it. The code references 5 attributes which are part of an object that was declared earlier and 3 tokens in the file. The first letter determines if the student is undergraduate or graduate and the second letter determines if the student is instate or out of state.

  while(fileScan.hasNext())
           {

            classStatus = fileScan.next();

            if(classStatus.charAt(0) == 'u' || classStatus.charAt(0) == 'U')
            {
                classStatus= "underGrad";
            }
            else
            {
                classStatus= "Grad";
            }

            studentName = fileScan.next();
            resident = fileScan.next();


            numberOfCredits = fileScan.nextInt();
            double tuitionBill = fileScan.nextDouble();


            StudentNode aNode = new StudentNode(classStatus,studentName,resident);
4

1 に答える 1

0

だから、重要な部分はあなたがjava.lang.Scanner(fileScanのタイプ)を使っているということですよね?

ファイルが適切に整理されている可能性が高いため、次のようになります。

トークン [スペース] トークン [スペース] トークン

各行を文字列として読み取り、読み取った各行のスペースを効果的に分割できます。Scanner正規表現を使用して、より具体的にはuseDelimiter()メソッドを使用して、この分割を実現できます。良い参考文献は、使用に関するこの他の SO の質問ですuseDelimiter()

とにかく、続行するには、fileScan.next()各トークンを文字列として取得するために使用します。次に、上記のようなsubstringメソッドまたはcharAtメソッドを使用して、必要な文字を選択します。

于 2013-02-21T21:45:28.680 に答える