-2

マイファイルに含まれるもの:

public class MyC {
public void MyMethod()
        {
            **System.out.println("My method has been accessed")** // ; is expected

            System.out.println("My method has been accessed");
}

}

このファイルでEclipseコンパイラを呼び出すと、次のように返されます。

Code: compiler.err.expected
Kind: ERROR
Line Number: 4
End position: 99
Position: 99

しかし、不足している文字列「;」を挿入しようとすると 4行目の99の位置で、「インデックスが範囲外の例外」になります。

ファイルを手動でカウントしましたが、インデックス99がファイルに存在していません。この問題を修正する方法。

これが特定の位置で置き換えられる私のプログラムです:

try {



             int num[] = {4}; //Line Numbers

             String[] VALUES = new String[] {";"}; //Correct Solutions

             //String[] VALUES1 = new String[] {"ic"}; //To Replace With

             int [] StartIndex ={99};

             int [] EndIndex ={99};
             FileInputStream fs= new FileInputStream("C:\\Users\\Antish\\Desktop\\MyC1.java");
             BufferedReader br = new BufferedReader(new InputStreamReader(fs));

             FileWriter writer1 = new FileWriter("C:\\Users\\Antish\\Desktop\\Test_File1.txt");

             String line;
             String line1 = null;

             String done = null;
             Integer count =0;

             line = br.readLine();
             count++;

             while(line!=null){


                 boolean exists = false;
                 for(int index =0;index<num.length;index++){
                     if(count == num[index]){ //Line Count Equals



                             exists = true;
                             StringBuffer buf = new StringBuffer(line);

                             buf.replace(StartIndex[index], EndIndex[index], VALUES[index]);//Get Positions From Array oF Indexes
                             done = buf.toString();
                             writer1.write(done+System.getProperty("line.separator"));



                     }
                 }

                 if (!exists)
                     writer1.write(line+System.getProperty("line.separator"));

                 line = br.readLine();

                 count++;


                 }

上記のプログラムを実行すると、次のようになります。

ここに画像の説明を入力してください

プログラムは正常に動作し、特定のインデックスの文字列を置き換えます。私の懸念は、Javaコンパイラがファイルからそのような大きな位置を返す理由です。

4

1 に答える 1

1
int [] StartIndex ={99};

値が 99 の 1 つの要素を持つ配列を作成します。何をしようとしているのかはわかりませんが、おそらく代わりに次のようにしたいと思います。

int[] StartIndex = new int[99];
于 2013-01-12T13:58:49.063 に答える