-1

*これは技術的にはHOMEWORKですが、何らかの理由でタグを追加できません

コンパイラは特に specialPrintData() メソッドを指しているようですが、正直なところ、何が問題なのかわかりません。これは私のプログラムを完成させるための最後のステップなので、すべての助けを歓迎します!

import java.util.Scanner;
import java.io.File;

public class Lab07
{
   static final int MAX = 100;  // global constant

   static public int readFile( String [ ] fd, String fileName)  throws Exception
   {

   File f = new File( fileName );
   Scanner input = new Scanner( f );

   int count = 0;

   while( count < fd.length && input.hasNextLine( ) )
   {
       fd[count] = input.nextLine( );
       count++;
   }

   if (input.hasNext( ) )
   {
      System.out.println("File is larger than array. Program is terminating.");
      System.exit(0);
   }

   else
   {
      System.out.println("Read " + count + " int values from file.");
   }
   input.close( );

   return count;
}   

static public void printData( String [ ] fd, int count )
{
  for( int i = 0; i < count; i++)
  {
    System.out.println(  fd[i] );
  }
  System.out.println();
}

static public void specialPrintData( String [ ] fd, int count )
{
  for( int i = 3; i < count; i++)
  {
    System.out.printf(" %4s ", fd[i] );
  }
}

static public int countWords(String str)
{
    int count = 1;
    for (int i = 0; i <= str.length() - 1; i++)
    {
        if (str.charAt(i) == ' ' && str.charAt(i+1) != ' ')
        {
            count++;
        }
    }
    return count;
}

static public void printNicelyFormattedData( String [ ] fd, int count )
{
    int numKids = 0;

    for( int i = 0; i < count; i++)
    {   
        if (countWords( fd[i] ) > 2)
        {
            numKids = ((countWords( fd[i]) - 3));
        }

        String [ ] list1 = fd[i].split(" ");

        System.out.println( list1[0] + " and " + list1[1] + " " + list1[2] + " have " + numKids + " children:" + "\n");

        specialPrintData( list1, count);
    }

    System.out.println();
}


   static public void main( String args [ ] )  throws Exception
   {
    if( args.length < 1 )
    {
       System.out.println("Error running program!");
       System.out.println("Usage:  java Lab07   L7d.txt");
       System.exit( 0 );
    }

    String [ ] fileData = new String[MAX];
    int count = 0;

    count = readFile( fileData, args[0] );
    System.out.println( "Array of file data contains " + count + " lines of data:");
    printData( fileData, count );

    System.out.println("*******************************************************");
    System.out.println("Family Data for past and present presidential families:");
    printNicelyFormattedData( fileData, count );

}
}
4

2 に答える 2

2

メソッドでは、countWordsindex の文字にアクセスしていますi + 1。したがって、次のように事前に確認する必要があります。

if (i + 1 < str.length()) {

この問題を修正すると、NullPointerException. スタック トレースを見ると、コードのどの行で発生したかがわかります。

これを修正するには、 を に置き換える必要がありString[]ますArrayList<String>MAXリストは配列に似ているため、定数を取り除くことができますが、add何かを加えるたびに自動的に大きくなります。

于 2013-10-31T05:09:02.707 に答える
1

問題はあなたのprintNicelyFormattedData方法にあります。この行で

specialPrintData( list1, count);

String[] fdのカウントではなく、のカウントを渡していますString[] list

つまり、1 行の単語数ではなく、ファイル内の行数を渡しています。

その代わりに、これを使用してください

specialPrintData( list1, list1.length);

編集:

@Roland が述べたように、別の例外が発生する可能性がありますが、以下の場合のみです。

この行で StringIndexOutOfBoundsException が発生する可能性があります if (str.charAt(i) == ' ' && str.charAt(i+1) != ' ') の場合は、行の最後の文字が ' ' の場合のみです。最後の文字で失敗し、2 番目の条件がチェックされない

お役に立てれば。

于 2013-10-31T05:14:07.357 に答える