java - 将命令作为文本文件传递给程序

标签 java linux terminal

我正在尝试将命令作为文本文件传递到我的程序中。我创建了一个名为 commands_in_english.txt 的文件并尝试传递说明,但它给我一条错误消息。不确定我是否传递了错误的文件,或者我的文本文件中是否遗漏了某些内容。任何帮助将不胜感激。

命令应作为文件传递给程序,每行一条指令。英文说明位于 文件 commands_in_english.txt 中。我通过传递指令来调用程序,如下所示:

java ConsoleRobot < commands_in_english.txt

这是我要将命令传递给的程序:

import java.util.Scanner;
public class ConsoleRobot extends SmarterRobot
{
   public static void main(String [] args)
   {
      World yard = new World();

  SmarterRobot ringo = new SmarterRobot();
  yard.add(ringo,5,4);

  yard.addBeeper(5,9);
  yard.addBeeper(4,5);

  yard.addBeeper(9,4);
  yard.addBeeper(9,5);

  Scanner scan = new Scanner(System.in);
  System.out.println("Enter a command: | Introduzca un comando:");
  String command = scan.nextLine();
  command = command.toLowerCase();

        while (!command.equals("stop") && !command.equals("detener"))
  {
  System.out.println("command = "+command);

  if ( command.equals("forward") || command.equals("adelante"))
  {
        Scanner reader = new Scanner(System.in);
        System.out.println("How far should the robot move?"); 
        int input = reader.nextInt();  
        ringo.moveNumOfTimes(input);
      }

      else if ( command.equals("right") || command.equals("derecha"))
   {
       ringo.turnRight();
       }

       else if ( command.equals("left") || command.equals("izquierda"))
  {
     ringo.turnLeft();
      }

       else if ( command.equals("collect") || command.equals("recoger"))
      {
    ringo.pickBeeper();
      }

   else if ( command.equals("drop") || command.equals("soltar"))
   {
    ringo.putBeeper();
   }

      else
   {
     System.out.println("I don't understand! | No entiendo!");
   }

      System.out.println("Enter a command: | Introduzca un comando:");
  command = scan.nextLine();
}
    System.out.println("Finished | Terminado");

   }
}

最佳答案

通常,当你想为你没有使用“<”的程序传递配置文件时。

尽量不加“<”就通过

java ConsoleRobot commands_in_english.txt

关于java - 将命令作为文本文件传递给程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600174/

相关文章:

java - Java 服务包装器的 JVM 错误

java - 是否有办法让 Rest 服务调用返回纯字符串?

linux - 如何从 linux 服务器中的以下日志中提取内容

windows - 终端 Vim (Cygwin) 中的退格键

python - 大多数终端命令不起作用

java - 使用搜索检索 LDAP 信息时出错

linux - df --mac 的总命令?

linux - 如果在 docker 构建期间添加了该目录,如何修复拒绝访问该目录的权限?

Haskell ncurses

java - Java输入编译错误