如何从Bash shell脚本传递多个整数到Java的System.in?

huangapple go评论95阅读模式
英文:

How to pass multiple integers from Bash shell script to Java System.in?

问题

我需要在Bash shell脚本中从Java程序中传递两个整数。

这是我的Java代码。

import java.util.Scanner;

public class ConsoleApp {
  public static void main(String[] args) {
    int x = getInt();
    int y = getInt();
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }

  private static int getInt() {
    Scanner s = new Scanner(System.in);
    return s.nextInt();
  }
}

这是Bash脚本,脚本的名称是 run.sh

#!/usr/bin/env bash

declare -r AppName=ConsoleApp

function compile() {
  javac $AppName.java
}

function execute() {
  echo 1 2 | java $AppName
}

compile; execute;
$ chmod a+x run.sh
$ ./run.sh

执行后,出现以下错误消息。

Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at ConsoleApp.getInt(ConsoleApp.java:13)
        at ConsoleApp.main(ConsoleApp.java:6)

目前,我知道它可以读取第一个整数 x,但无法读取第二个整数 y

英文:

I need to pass two integer numbers from Bash shell script to my Java program.

Here is my Java code.

import java.util.Scanner;

public class ConsoleApp {
  public static void main(String[] args) {
    int x = getInt();
    int y = getInt();
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }

  private static int getInt() {
    Scanner s = new Scanner(System.in);
    return s.nextInt();
  }
}

Here it's the Bash script and the name of script is run.sh.

#!/usr/bin/env bash

declare -r AppName=ConsoleApp

function compile() {
  javac $AppName.java
}

function execute() {
  echo 1 2 | java $AppName
}

compile; execute;
$ chmod a+x run.sh
$ ./run.sh

After I execute, it occurs the following error message.

Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at ConsoleApp.getInt(ConsoleApp.java:13)
        at ConsoleApp.main(ConsoleApp.java:6)

For now, I know it can read the first integer number x but can't read the second integer number y.

答案1

得分: 2

你必须对两个数字使用同一个扫描器:

Scanner s = new Scanner(System.in);
int x = s.nextInt();
int y = s.nextInt();
System.out.println("x = " + x);
System.out.println("y = " + y);

否则,第一个扫描器会从System.in中消耗比第一个数字更多的数据,并将剩余数据存储在内部缓冲区中进行进一步处理。如果您切换到另一个扫描器,那些数据将会丢失,新的扫描器将再次从System.in读取,但没有更多数据可供读取。

或者,通过args参数将这两个数字作为命令行参数传递可能会更简便。

英文:

You have to use the same scanner for both numbers:

  Scanner s = new Scanner(System.in);
  int x = s.nextInt();
  int y = s.nextInt();
  System.out.println("x = " + x);
  System.out.println("y = " + y);

Otherwise the first scanner will consume more data from System.in than the first number and store the remaining data in an internal buffer for further processing. If you then switch to another scanner that data is lost and the new scanner will read from System.in again but there is nothing more left.

Alternatively, it may be easier to pass the two numbers as command line arguments via the args argument.

答案2

得分: 1

创建一个单独的Scanner

import java.util.Scanner;

public class ConsoleApp {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int x = getInt(s);
    int y = getInt(s);
    System.out.println("x = " + x);
    System.out.println("y = " + y);
  }

  private static int getInt(Scanner s) {
    return s.nextInt();
  }
}

在你的 Bash 脚本中使用 heredoc

#!/usr/bin/env bash

declare -r AppName=ConsoleApp

function compile() {
  javac $AppName.java
}

function execute() {
  java $AppName <<EOF
1 2
EOF
}

compile; execute;
英文:

Create a single Scanner.

import java.util.Scanner;

public class ConsoleApp {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int x = getInt(s);
    int y = getInt(s);
    System.out.println(&quot;x = &quot; + x);
    System.out.println(&quot;y = &quot; + y);
  }

  private static int getInt(Scanner s) {
    return s.nextInt();
  }
}

Use a heredoc in your Bash script.

#!/usr/bin/env bash

declare -r AppName=ConsoleApp

function compile() {
  javac $AppName.java
}

function execute() {
  java $AppName &lt;&lt;EOF
1 2
EOF
}

compile; execute;

huangapple
  • 本文由 发表于 2020年8月31日 12:41:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63664885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定