ArrayIndexOutOfBoundsException:在Java中尝试从Linux命令行读入文件时发生0?

ArrayIndexOutOfBoundsException:在Java中尝试从Linux命令行读入文件时发生0?,java,linux,Java,Linux,所以我试图从Linux命令行接受一个文本文件到我的Java程序中,但是编译器给出了标题中提到的错误。它表示错误发生在“stringfilename=args[0];”行。有人知道为什么吗? 这是我的密码: public class Parsons_Decoder { // method: main // purpose: receives key-phrase and sequence of integers and // prints the secret message to t

所以我试图从Linux命令行接受一个文本文件到我的Java程序中,但是编译器给出了标题中提到的错误。它表示错误发生在“stringfilename=args[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();
  }
}
公共类Parsons\u解码器
{
//方法:主
//用途:接收关键短语和整数序列
//将机密消息打印到屏幕上。
公共静态void main(字符串[]args)引发IOException
{
字符串文件名=args[0];
//读取传入文件(如果存在)并将关键短语保存到
//字符串变量“keyphase”
文件testFile=新文件(文件名);
如果(!testFile.exists())
{
System.out.println(“\n此文件不存在。\n”);
系统出口(0);
}
扫描仪输入文件=新扫描仪(args[0]);
字符串keyphase=inputFile.nextLine();
//创建ArrayList并将整数序列存储到其中
ArrayList numArray=新的ArrayList();
while(inputFile.hasNextInt())
{
add(inputFile.nextInt());
}
//解码并将机密消息打印到屏幕上
System.out.println();
System.out.print(“您的秘密消息是:”);
对于(int i=0;i
更新:

您的教授要求您使用如下命令读取带有stdin的文件:

java Diaz_Decoder < secretText1.txt
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");
}

更新:

您的教授要求您使用如下命令读取带有stdin的文件:

java Diaz_Decoder < secretText1.txt
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");
}

根据您的描述和您提供的链接(应该在问题中,而不是注释中),您的教授希望您编写一个程序,在使用重定向作为POSIX样式的shell命令行运行时,通过“标准in”(STDIN)接受文件的内容


如果这确实是一个要求,那么您不能仅仅读取作为参数给出的文件,而是需要更改您的程序,使其从STDIN读取。这里的关键概念是,“基于您的描述和您提供的链接(应该在问题中,而不是注释中),您的教授希望您编写一个程序,当使用重定向作为POSIX样式的shell命令行运行时,通过“标准in”(STDIN)接受文件的内容



如果这确实是一个要求,那么您不能仅仅读取作为参数给出的文件,而是需要更改您的程序,使其从STDIN读取。这里的关键概念是“如何运行程序”。带参数的完整命令请在CLI中编写什么?很抱歉,命令行应该是:java Parsons_DedocerGet,去掉
,除非<是必需的,因为OP应该处理STDIN。如何运行该程序。带参数的完整命令请在CLI中编写什么?很抱歉,命令行应该是:java Parsons_deDocTarget去掉
,除非<是必需的,因为OP应该处理STDIN。谢谢,这样做效果更好,但我的教授为我们提供了一个使用“你确定这是他写的吗?也许他是在用速记来表示一个通用文件,例如,
java Parsons_Decoder
?他给我们提供了一个示例,说明了我们与程序的交互应该是什么样子的,下面是该部分说明中的一个屏幕截图:我相信他输入了一个错误,并不打算让
查看提供的链接,这可能是有意为之,给出的示例显示了以Java进程的形式呈现给标准的文件的内容。这是否是一个硬性要求是OP必须要解决的问题。谢谢,这样做效果更好,但我的教授给我们提供了一个使用“你确定这是他写的吗?也许他是在用速记来表示一个通用文件,例如,
java Parsons_Decoder
?他给我们提供了一个示例,说明了我们与程序的交互应该是什么样子的,下面是该部分说明中的一个屏幕截图:我相信他输入了一个错误,并不打算让
查看提供的链接,这可能是有意为之,给出的示例显示了以Java进程的形式呈现给标准的文件的内容。这是否是一项硬性要求,是OP必须解决的问题。