0

I am following Prinston's course Algorithms, Part I. As I came from .NET and just started to use Java, I have some issue with a piece of Java code and I can not find any related information. They provide with a code which I can use for reading a text file:

public static void main(String[] args) {

    In in = new In(args[0]);
    int n = in.readInt();
    ...
}

Exception thrown:

In cannot be resolved to a type

What is this In? Should I import some package or what should I do?

The whole code and description can be also seen here: http://coursera.cs.princeton.edu/algs4/assignments/collinear.html

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75
  • My guess is that In should be Int and new Int(). Ie, the class Integer as opposed to the primitive type. – Erik Nov 21 '16 at 12:43
  • Good question. What is this `In`? Syntactically it should be a class. But there is no such class in JDK, so it should be a custom class that obviously unavailable in your project. – AlexR Nov 21 '16 at 12:43

2 Answers2

5

You need algs4, provided by Princeton as well. When it's in the classpath, add

import edu.princeton.cs.algs4.In;

If that's the only class, you could use the source of In.java. But I doubt that's allowed on Coursera: when you submit code, it will probably be compiled on the server with algs4.jar in the classpath, so you should really use that one and not your own code.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • I've installed `algs4` and I can use their namespaces like `edu.princeton.cs.algs4.StdRandom`, `edu.princeton.cs.algs4.StdIn/Out`, but not the class from the question. Let me check the file.... – Giorgi Nakeuri Nov 21 '16 at 12:48
  • Hmmmm, I had to use `import edu.princeton.cs.algs4.In;`. They didn't mention it anywhere. Thank you!!! – Giorgi Nakeuri Nov 21 '16 at 12:56
  • @GiorgiNakeuri: thanks for the comment. I added the import statement to the answer,. – Thomas Weller Nov 21 '16 at 13:10
0

In.java seems to be a class that someone at princeton made, probably for that same course: http://algs4.cs.princeton.edu/12oop/In.java.

So, yes, you need to import the correct package and you need a jar with that class in your classpath. Search your course documentation for more hints.