1

I'm piping a text file into a java program. This is what the text file "input.txt" holds:

4
5

That is, the numbers 4 and 5 in seperated lines. I want the program to read those numbers and print them out. This code should work:

public class readFile { 
  public static void main(String[] args) { 

    while (!StdIn.isEmpty()) {
      System.out.println( StdIn.readInt() );
    }
  }
}

But when I do the following in DrJava:

run readFile < input.txt

I don't get

4
5

but I get a box asking me for input instead of printing the numbers out: http://oi50.tinypic.com/3585kxc.jpg

What am I doing wrong?

Martin Ellis
  • 9,603
  • 42
  • 53
helgso
  • 11
  • 1
  • 5
  • 1
    What is `StdIn`? I don't use DrJava and I've never seen that object before. – Makoto Mar 17 '13 at 20:57
  • I assume StdIn is the class of the same name used in the Sedgewick et. al Java courses. – Martin Ellis Mar 17 '13 at 20:58
  • yes, it is created by Princeton, see here: http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdIn.html – helgso Mar 17 '13 at 21:06
  • 2
    The problem seems to be that DrJava does not implement input stream redirection. Or that `StdIn` does not actually read the standard input stream of the program, despite its name. More likely the first one. – Joni Mar 17 '13 at 21:10
  • Do you know of a light program like DrJava that implements this? – helgso Mar 17 '13 at 21:27

4 Answers4

1

This is really a very old way to work. Java allows you to read file from a file system. Below is a simple example of a method that reads file from a file system and returns you a list of all strings in file.(no check on file that doesn't exist but you can add it yourself):

Imports:

import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

The function:

public List<String> readFile(String filePath) throws IOException {
    List<String> result = new ArrayList<String>();
    try {
       FileInputStream streamer = new FileInputStream(filePath);
       InputStreamReader streamReader = new InputStreamReader(streamer);
       BufferedReader reader = new BufferedReader(streamReader);
       String line;
       while ((line = reader.readLine()) != null) {
           result.add(line.replace("\n", ""));
       }
    }
    catch(Exception e){
        e.printStackTrace();
    }

    return result;
}

So you can write a java program that takes an argument that is a path to your file and read it. After that you can just print your strings from the list

Yonatan Simson
  • 2,395
  • 1
  • 24
  • 35
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

Found this link which might help with the solution.

inputoutput-redirection-in-drjava

Extracts:

The Interactions Pane in DrJava is not a DOS command line or Unix-type shell, and it does not support I/O redirection.

Based on the above my solution for what you want is:

import java.io.*;

public class ReadFile  {

      public static void readFromStream(String is) throws IOException {
      BufferedReader br = new BufferedReader(new FileReader(is));
        String line;
        while((line = br.readLine()) != null) {
          System.out.println(line);
        }
        br.close();
      }

   public static void main(String args[]) throws IOException {
       readFromStream(args[0]);
   }
}

On Dr.Java (if Dr.Java supports taking main arguments) :

 java Readfile  Q1.txt
kabir
  • 61
  • 3
0

Here is the text file I tried to redirect into DrJava:

Q1.txt

10
8 0
5 2
6 0
6 4
9 6
9 5

From the Dos command line I typed:

C:\Users\USER\algs4\Week1>java QuickFindUF < Q1.txt

This resulted in the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: edu/princeton/cs/algs
4/StdIn
        at QuickFindUF.main(QuickFindUF.java:172)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdIn
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

I got the same result when typing the same command in the DrJava "Interactions dialog box at the bottom.

I tried piping from command line directly typing after the command line:

10
8
0
...
^z

It appears that you need ^D (ctrl+D) when doing the same in DrJava

Eventually I had hunch that using java-algs4.bat might be the solution:

..\bin\java-algs4.bat QuickFindUF < Q1.txt

This is how to do it in Windows from the DOS command line.

I would be delighted to figure out how this is done in eclipse as well

Yonatan Simson
  • 2,395
  • 1
  • 24
  • 35
0

To pipe a text file into a java program using Dr Java, you need to read the file from standard input by using the class edu.princeton.cs.algs4.In. Your code would have to look like this:

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

public class readFile { 
  public static void main(String[] args) { 
    //Read file
    In in = new In(args[0]);
    while (!in.isEmpty()) {
      StdOut.println( in.readInt() );
    }
  }
}

In this way, you won't get the box asking you for an input.

Laura Rozo
  • 33
  • 4