[英]intelliJ IDEA run class with command line argument
I'm going through the Algorithms book by Sedgewick and I can't seem to make my IDE run their programs. 我正在阅读Sedgewick的《算法》一书,但似乎无法让我的IDE运行他们的程序。 The program starts but won't take the passed argument.
程序启动,但不接受传递的参数。 Specifically I want it to open the tiny.txt file, which I set in Program arguments section but it's just ignored...
具体来说,我希望它打开tiny.txt文件,该文件是我在“程序参数”部分中设置的,但被忽略了...
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut;
public class Selection
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length; // array length
for (int i = 0; i < N; i++)
{ // Exchange a[i] with smallest entry in a[i+1...N).
int min = i; // index of minimal entr.
for (int j = i+1; j < N; j++)
if (less(a[j], a[min])) min = j;
exch(a, i, min);
} }
// See page 245 for less(), exch(), isSorted(), and main().
private static boolean less(Comparable v, Comparable w)
{ return v.compareTo(w) < 0; }
private static void exch(Comparable[] a, int i, int j)
{ Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
private static void show(Comparable[] a)
{ // Print the array, on a single line.
for (int i = 0; i < a.length; i++)
StdOut.print(a[i] + " ");
StdOut.println();
}
public static boolean isSorted(Comparable[] a)
{ // Test whether the array entries are in order.
for (int i = 1; i < a.length; i++)
if (less(a[i], a[i-1])) return false;
return true;
}
public static void main(String[ ] args)
{ // Read strings from standard input, sort them, and print.
String[] a = In.readStrings();
sort(a);
assert isSorted(a);
show(a);
}
}
You seem to want standard input redirection. 您似乎想要标准输入重定向。 Unfortunately, IntelliJ doesn't support that.
不幸的是,IntelliJ不支持该功能。
When you supply a command-line argument, all that does is that each word from the argument is passed as a string in the args
argument of the main
method. 提供命令行参数时,所有要做的就是将参数中的每个单词作为字符串传递给
main
方法的args
参数中。 You can then write some code in main
to open a BufferedReader on that file. 然后,您可以在
main
编写一些代码以在该文件上打开BufferedReader。
The alternative is to open a terminal window (or Command Prompt window) and type the command line java package.name.ClassName < filename.ext
. 替代方法是打开终端窗口(或命令提示符窗口),然后输入命令行
java package.name.ClassName < filename.ext
。 The command processor or shell interprets the <
character as a request to redirect standard input to the supplied file. 命令处理器或外壳程序将
<
字符解释为将标准输入重定向到提供的文件的请求。
Alright this is what needs to be done if you want to use Algorithms code from within intelliJ IDEA on mac OS Sierra: 好吧,如果要在Mac OS Sierra上的intelliJ IDEA中使用Algorithms代码,则需要执行以下操作:
1, make sure to get and run algs4.app from their website 1,确保从他们的网站获取并运行algs4.app
2, use intelliJ terminal to navigate to algorithms2/out/production/algorithms2 where the .class files reside 2,使用intelliJ终端导航到.class文件所在的algorithm2 / out / production / algorithms2
3, type in terminal: $ java-algs4 Selection < /Users/mistakeNot/Desktop/Java/algorithms2/tiny.txt 3,输入终端:$ java-algs4 Selection </Users/mistakeNot/Desktop/Java/algorithms2/tiny.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.