2016-04-06 4 views
1

私はLinuxのコマンドラインからJavaプログラムにテキストファイルを受け入れようとしていますが、コンパイラはそのエラーをタイトルに記述します。このエラーは、 "String fileName = args [0];"という行で発生します。なぜ誰が知っていますか? は、ここに私のコードです:ArrayIndexOutOfBoundsException:JavaのLinuxコマンドラインからファイルを読み取ろうとしているときに0が発生しますか?

public class Parsons_Decoder 
{ 
    // method: main 
    // purpose: receives key-phrase and sequence of integers and 
    // prints the secret message to the screen. 
    public static void main(String[] args) throws IOException 
    { 
    String fileName = args[0]; 

    // reads incoming file (if it exists) and saves the key-phrase to 
    // String variable "keyPhrase" 
    File testFile = new File(fileName); 

    if(!testFile.exists()) 
    { 
     System.out.println("\nThis file does not exist.\n"); 
     System.exit(0); 
    } 

    Scanner inputFile = new Scanner(args[0]); 
    String keyPhrase = inputFile.nextLine(); 

    // creates an ArrayList and stores the sequence of integers into it 
    ArrayList<Integer> numArray = new ArrayList<Integer>(); 

    while(inputFile.hasNextInt()) 
    { 
     numArray.add(inputFile.nextInt()); 
    } 

    // decodes and prints the secret message to the screen 
    System.out.println(); 
    System.out.print("Your secret message is: "); 

    for(int i = 0; i < numArray.size(); i++) 
    { 
     int num = numArray.get(i); 
     System.out.print(keyPhrase.charAt(num)); 
    } 
    System.out.println("\n"); 

    //keyboard.close(); 
    inputFile.close(); 
    } 
} 
+1

どのようにプログラムを実行しますか。議論のある完全なコマンド – 3kings

+0

あなたはCLIで何を書いていますか? – Gendarme

+0

残念ですが、コマンドラインは次のようにする必要があります:java Parsons_Dedocer thelonewanderer

答えて

4

更新:

あなたの教授は、次のようなコマンドを使用して、標準入力を持つファイルを読み込むためにあなたを求めている:

java Diaz_Decoder < secretText1.txt 

あなたmain()メソッドは、次のようになります。

public static void main(String[] args) throws IOException { 
    // create a scanner using stdin 
    Scanner sc = new Scanner(System.in); 

    String keyPhrase = inputFile.nextLine(); 

    // creates an ArrayList and stores the sequence of integers into it 
    ArrayList<Integer> numArray = new ArrayList<Integer>(); 

    while (inputFile.hasNextInt()) { 
     numArray.add(inputFile.nextInt()); 
    } 

    // decodes and prints the secret message to the screen 
    System.out.println(); 
    System.out.print("Your secret message is: "); 

    for (int i = 0; i < numArray.size(); i++) { 
     int num = numArray.get(i); 
     System.out.print(keyPhrase.charAt(num)); 
    } 

    System.out.println("\n"); 
} 
+0

ありがとう、それはうまくいきましたが、私の教授が "<"を使った例を教えてくれましたが、これを行う方法はありますか? – thelonewanderer

+0

彼は本当にこれを書きましたか?おそらく、彼は一般的なファイルの略語を使用していたでしょうか? 'java Parsons_Decoder '? –

+0

彼は私たちのプログラムとのやりとりのサンプルを提供しました。ここにはその部分の手順のスクリーンショットがあります:https://drive.google.com/file/d/0BwujWiqVRKKsMzdRZDRPcTFQVkk/view?usp=sharing – thelonewanderer

1

あなたの説明とあなたが提供したリンク(コメントではありません)は、実行時に "標準入力"(STDIN)でファイルの内容を受け入れるプログラムを作成する必要がありますリダイレクトを使用してPOSIXスタイルのシェルコマンドラインとして使用します。

これが本当に必要な場合は、引数として与えられたファイルを読むだけではなく、プログラムをSTDINから読み込むように変更する必要があります。ここで重要な概念は、 "<"がプログラムの引数リストで利用できないということです。これは、Javaプロセスを実行しているシェル(Bash、Kshなど)と、右側のファイルと左側のプロセスとの間の「パイプ」設定によって消費されます。この場合、プロセスはプログラムを実行するJavaプロセスです。

でその標準を読むことができるJavaプログラムを記述する方法についていくつかのアイデアを得るために、「JavaのSTDIN」を検索してみてください。

ところで、リダイレクトを実行すると、プログラムがArrayIndexOutOfBoundErrorでクラッシュした場合この方法では、それにはまだバグがあります。シェルがコマンドラインの処理を終えた後で、ファイル引数が0の場合をテストして処理する必要があります。完全なマークが必要な場合は、エラーとエッジのケースを処理する必要があります。

関連する問題