Java 编译错误:找不到符号:In、StdIn 和 StdOut

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16207427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:47:32  来源:igfitidea点击:

compile error: cannot find symbol: In, StdIn and StdOut

java

提问by Jason Kim

The code is from http://algs4.cs.princeton.edu/11model/BinarySearch.java.htmlfor Algorithms textbook.

代码来自 http://algs4.cs.princeton.edu/11model/BinarySearch.java.html算法教科书。

import java.util.Arrays;

public class BinarySearch {

    // precondition: array a[] is sorted
    public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] whitelist = In.readInts(args[0]);

        Arrays.sort(whitelist);

        // read key; print if not in whitelist
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (rank(key, whitelist) == -1)
                StdOut.println(key);
        }
    }
}

I get this error

我收到这个错误

$ javac BinarySearch.java 
BinarySearch.java:44: cannot find symbol
symbol  : variable In
location: class BinarySearch
        int[] whitelist = In.readInts(args[0]);
                          ^
BinarySearch.java:49: cannot find symbol
symbol  : variable StdIn
location: class BinarySearch
        while (!StdIn.isEmpty()) {
                ^
BinarySearch.java:50: cannot find symbol
symbol  : variable StdIn
location: class BinarySearch
            int key = StdIn.readInt();
                      ^
BinarySearch.java:52: cannot find symbol
symbol  : variable StdOut
location: class BinarySearch
                StdOut.println(key);
                ^
4 errors

采纳答案by Jon Skeet

Classes StdIn, StdOutand Inaren't part of the standard Java libraries. They're support classes provided to go along with the Princeton course.

Classes StdInStdOut并且In不是标准 Java 库的一部分。它们是与普林斯顿课程一起提供的支持课程。

From the 1.1 Programming Modelpage linked in the source code:

从源代码中链接的1.1 编程模型页面:

Standard input and standard output. StdIn.javaand StdOut.javaare libraries for reading in numbers and text from standard input and printing out numbers and text to standard output. Our versions have a simpler interface than the corresponding Java ones (and provide a few tecnical improvements).

...

In.javaand Out.javaare object-oriented versions that support multiple input and output streams, including reading from a file or URL and writing to a file.

标准输入和标准输出。StdIn.javaStdOut.java是用于从标准输入读取数字和文本并将数字和文本打印到标准输出的库。我们的版本具有比相应 Java 版本更简单的界面(并提供一些技术改进)。

...

In.java并且Out.java是面向对象的版本,支持多个输入和输出流,包括从文件或 URL 读取和写入文件。

So if you want to use the binary search code as-is, you'll need to download those files.

因此,如果您想按原样使用二进制搜索代码,则需要下载这些文件。

回答by Dheeraj Joshi

You can replace then with

你可以用然后替换

Output:

输出:

 System.out.println(key);

Input

输入

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String Key= null;
try {
     Key = reader.readLine();
} catch (IOException e) {
   e.printStackTrace();
} 

They are using custom libraries to print the value to console(Presumably) You can redirect the output to console.

他们正在使用自定义库将值打印到控制台(大概)您可以将输出重定向到控制台。

回答by Amir Kost

You should use System.inand System.outinstead of StdInand StdOut.
Create an ObjectInputStreamwrapping the System input stream the following way:
ObjectInputStream ois = new ObjectInputStream(System.in);
It has a readIntmethod, amd to check if it is empty, you must catch the EOFException.

您应该使用System.inandSystem.out而不是StdInand StdOut。通过以下方式
创建一个ObjectInputStream包装系统输入流:
ObjectInputStream ois = new ObjectInputStream(System.in);
它有一个readInt方法 amd 来检查它是否为空,您必须捕获EOFException.

回答by Gladstone24

StdIn and In are custom Libraries that are included within the algs4 class download. Execute the program with the command java-algs4 instead of just java and it should work.

StdIn 和 In 是包含在 algs4 类下载中的自定义库。使用命令 java-algs4 而不仅仅是 java 执行程序,它应该可以工作。

回答by lohenzoo

If you have already set up the environment recommended for the course, only add this lines to your java file

如果您已经设置了课程推荐的环境,只需将此行添加到您的 java 文件中

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;