4

I am taking a course Algorithms 1 by Sedgwick (Princeton University) and trying to read an integer from standard input which represents the number of integer pairs that will be input , then a list of integer pairs from standard input.

For example:

3                     //first integer represents number of pairs

12  34                  //list of integer pairs

23  56

34  78

I was hoping to type:

java  myProg   3  12   34  23  56   34  78  

at the command prompt in windows command line and read first the 3, then in a loop and process each pair in turn.

The code given in the video is this:

package getArgs;

public class getInput {

public static void main(String[] args) {
    int N = StdIn.readInt();   //read first integer 
    while (!Stdin.isEmpty()){  // loop through pairs
        int p = StdIn.readInt();
        int q = StdIn.readInt();
        //process p and q      //process each pair

    }


}

Question:

  1. In the code is Stdin.readInt() actual java or pseudocode?

  2. If it is not pseudocode do I need to import a library to access StdIn?

  3. If it is pseudocode do I use a Scanner class to replace StdIn.readInt()?

The rest of the program works , that is the union find class algorithm, this driver to test the program was not explained well and confused me a little.

Here is my revised attempt - it is ugly

public class getInput {



public static void main(String[] args) {

    int i = 0;  //counter;
    int N = 0;  


    N = Integer.parseInt(args[0]);  //Read first Integer 
    System.out.println(N );         //check it works
    i = i + 1;

    while ( i < N * 2 ){

        int p = Integer.parseInt(args[i]);  //get first of the next pair
        i=i + 1;
        int q = Integer.parseInt(args[i]);  //get second of the next pair
        System.out.println(p + "  " + q );  //check


    }


}

This works but is uglier than the StdIn - can anyone rewrite it using StdIn so that java recognizes StdIn?

3
  • "If it is pseudocode do I use a Scanner class to replace StdIn.readInt()?" -- Yes, you can create a Scanner that wraps System.in and use it to read console input. Commented Oct 12, 2015 at 18:11
  • 1
    Note that 10 12 34 23 56 34 78 in java myProg 10 12 34 23 56 34 78 command are arguments of java program. First argument is name of class where java should search for main method, rest of arguments are moved on String[] args array. Standard input is stream which is connected to java process, which is referred via System.in. By default this stream reads data from console. Commented Oct 12, 2015 at 18:11
  • You seem to be asking multiple unrelated questions now. Accept the answer about where StdIn comes from and then ask a new question. Commented Oct 12, 2015 at 19:57

6 Answers 6

2

OK I got it working and the comment by snickers was correct. The StdIn class was written by Sedgewick and published in his earlier book. I simply created class StdIn and StdOut and copied the code from the internet into the empty class files, then it worked.To summarise it is a classses created by a professor to simplify reading from the command lines.

Sign up to request clarification or add additional context in comments.

Comments

1

Let's try with below code by invoking java getInput 10 12 34 23 56 34 78 and see what will happen.

public class getInput {

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("arg " + i + ": " + arg);
        }
    }

}

This relates to what Pshemo has already commented.

Comments

1

(with the research that @snickers10m did!)

In the code is Stdin.readInt() actual java or pseudocode?

Actual Java code. From the docs Stdin.readInt does this:

Reads the next token from standard input, parses it as an integer, and returns the integer.

If it is not pseudocode do I need to import a library to access StdIn?

Depends on how Stdin.java or Stdin.class is delivered to you. If it is in the same package you don't need and an import for it, you should just have Stdin.class on your classpath.

(Edit: googled some more, found http://introcs.cs.princeton.edu/java/15inout/)

From the docs:

To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program.

If it is pseudocode do I use a Scanner class to replace StdIn.readInt()?

N/A as it is actual code

Making your code work with StdIn

(Following edits to the question.)

You should move your program out of the package into the same package as StdIn

Comments

0

Check out this link here which shows that Stdin is actually a wrapper class for Scanner. If you have it, you can use it, otherwise go with Scanner. If you don't have it, just take it from that link and compile it as part of your application.

However you wouldn't need any of it if you are sending them as args[], in which can you can reference the string values as args1, args[2]... etc.

Comments

0

In the code is Stdin.readInt() actual java or pseudocode?

That is pseudo-code. Stdin is not a Java class from the JRE.

If it is pseudocode do I use a Scanner class to replace StdIn.readInt()?

You'd be better off using System's InputStream. Here's a loose example of how to implement it:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String");
String s = br.readLine();
System.out.print("Enter Integer:");
try{
    int i = Integer.parseInt(br.readLine());
    // ...
} catch(NumberFormatException nfe){
    nfe.printStackTrace();
    System.exit(-1);
}

1 Comment

StdIn is a class. It's not part of the Java API, but it's a class made by the intro to computer programming professor at his university. See my answer.
0

Std (Standard library) is provided for students it's not part of "installed java libraries" so you can download the Std library and declare it in your path and then It works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.