単語数が異なる可能性のあるテキスト ファイルからコマンドを読み込もうとしています。配列が範囲外の例外を取得しないように、IF ステートメントを使用しようとしています。誰が私が間違っているのか教えてもらえますか? ありがとうございました。これが発生するコードのセクションです。
read = new Scanner(new File("Phone.txt"));
//this will loop thru file until end of file
while (read.hasNext())
{
whole = read.nextLine().trim();
String[] tokens = whole.split(" ");//array to split up line
command = tokens[0].toLowerCase();//all lines will have at least one word
if (tokens.length > 0)//if more than one word, 2nd will be name
{
name1st.setFirst(tokens[1]);
}
if (tokens.length > 1)//if more than one word, 3rd will be phone #
{
phone = tokens[2];
}
switch (command)
{
case "add":
nameList.add(name1st, phone);
break;
case "locate":
nameList.getValue(name1st);
break;
case "remove":
nameList.remove(name1st);
break;
case "print":
nameList.display();
}//end switch
}//end while