Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error with my filter program

2 views
Skip to first unread message

SamuelXiao

unread,
Sep 7, 2008, 1:03:21 PM9/7/08
to
My program is doing filtering. The user input a file and the file
contains a java code with comment //, /*, */. My goal is output to a
file that the code should be without comment line;
e.g.
class ALIST { /* The given // class (the user-defined */
String name, address; // type )"
}

public class Question1{ /*The parts
in white will // be same */
public static void main( String args[ ] ) { // for all Java programs
ALIST list [ ] = new ALIST[3]; // Define an array
//for (int j=0; j<list.length; j++) {
//list[ j] = new ALIST(); // Create ALIST object
//list[ j].name = "name" + j; // Initialization
//list[ j].address = "address" + j;
list[0] = new ALIST();
list[1] = new ALIST();
list[0].name = "name0";
list[1].name = "name1";
-------------so on-------------------------

The output.txt should be:
class ALIST {
String name, address;
}

public class Question1 {
public static void main( String args[ ] ) {
ALIST list [ ] = new ALIST[2];


list[0] = new ALIST();
list[1] = new ALIST();
list[0].name = "name0";
list[1].name = "name1";
------------so on---------------------------

The // may come before /* and vice verse, so I have to check whether
ignore the whole line or only after the /* (upto */ part), /* and */
may not be in the same line

-----------------------------------------Here is my
code------------------------------------
import java.util.Scanner;
import java.io.*;
public class FilterProgram {
public static void main(String[] args){
try{
String read;
Scanner readfile = new Scanner(System.in);
read = readfile.nextLine();
File openfile = new File(read);
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("output.txt")));

if(!openfile.exists()){
throw new FileNotFoundException("File not found" +
read);
}
Scanner scanfile = new Scanner(openfile);
String tryread;
String before = null;
while((tryread = scanfile.nextLine())!=null){
int doubleslash = tryread.indexOf("//");
int block1 = tryread.indexOf("/*");
int block2 = -1;
if((doubleslash >= 0) || block1>=0){
if(doubleslash<block1){
tryread = tryread.substring(0,doubleslash);
System.out.println("doubleslash");
}
else
{
block2 = tryread.indexOf("/*");
before = tryread.substring(0,block1);
if(block2 < 0 || block2<=block1 + 1){
do{
tryread = scanfile.nextLine();
block2 = tryread.indexOf("*/");

}while(block2<0);
}
tryread = before + tryread.substring(block2 +
2);
}
System.out.println("block2 can't be reached");
}
pw.println(tryread);

}
System.out.println("here3");
readfile.close();
pw.close();
}
catch(FileNotFoundException ex){
System.err.println(ex.getMessage());
System.exit(1);
}
catch(IOException ex){
System.err.println(ex.getMessage());
System.exit(2);
}

}

}

when I type input.txt, the compiler complains:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1
at java.lang.String.substring(String.java:1938)
at FilterProgram.main(FilterProgram.java:35)
Java Result: 1
How can I fix it and can anyone tell the problem?

Bo Vance

unread,
Sep 7, 2008, 2:29:50 PM9/7/08
to
SamuelXiao wrote:
> My program is doing filtering.
>
> when I type input.txt, the compiler complains:
> Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
> String index out of range: -1
> at java.lang.String.substring(String.java:1938)
> at FilterProgram.main(FilterProgram.java:35)
> Java Result: 1
> How can I fix it and can anyone tell the problem?

SamuelXiao,
Thank you for posting a compilable example.
Here is a simplified version.

public class Scratch {

public static void main(String[] args) {

String tryread =
"public class Question1{ \t\t/*The parts";


int doubleslash = tryread.indexOf("//");
int block1 = tryread.indexOf("/*");
int block2 = -1;
if ((doubleslash >= 0) || block1 >= 0) {
if (doubleslash < block1) {
tryread = tryread.substring(0, doubleslash);
System.out.println("doubleslash");
}
}
}
}

at the line: "if (doubleslash < block1) {"
the debugger shows values for:
tryread = "public class Question1{ \t\t/*The parts"
doubleslash = -1
block1 = 30

When I run this program I get:


Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -1

at java.lang.String.substring(Unknown Source)
at Scratch.main(Scratch.java:11)

Bo Vance

unread,
Sep 7, 2008, 3:29:48 PM9/7/08
to
SamuelXiao wrote:
> My program is doing filtering. The user input a file and the file
> contains a java code with comment //, /*, */. My goal is output to a
> file that the code should be without comment line;
> e.g.
> class ALIST { /* The given // class (the user-defined */
> String name, address; // type )"
> }
>

What about this:

class Scratch {
public String comment =
"/* marks the beginning of a multi-line comment," +
"and the end is marked with */."
}

>
> The // may come before /* and vice verse, so I have to check whether
> ignore the whole line or only after the /* (upto */ part), /* and */
> may not be in the same line
>

Have you precisely defined the problem?

So, You Need to Write a Program but Don't Know How to Start
<http://home.earthlink.net/~patricia_shanahan/beginner.html>

The Java Language Specification, Third Edition
<http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html>

Specifically Chapters 2 and 3
<http://java.sun.com/docs/books/jls/third_edition/html/grammars.html>
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html>

The Java™ Programming Language Compiler, javac
<http://java.sun.com/javase/6/docs/technotes/guides/javac/index.html>

SamuelXiao

unread,
Sep 8, 2008, 4:57:43 AM9/8/08
to

oh, sorry, I forget to indicate that to simplify the problem it is
assumed that the patterns "//","/*","*/" will not appear inside the
double quote of the print(), println() and printf() methods.

SamuelXiao

unread,
Sep 8, 2008, 8:37:45 AM9/8/08
to
On Sep 8, 3:29 am, Bo Vance <bowman.va...@invalid.invalid> wrote:

I have found out the bugs and fixed the problem but, I still don't
know what is different between the following two statement:
1. while(scanfile.hasNext())
2.while((tryread = scanfile.nextLine())!=null)
In fact, for the 1st one, the compiler will not complain but the 2nd
will. Why?

John B. Matthews

unread,
Sep 8, 2008, 11:22:51 AM9/8/08
to
In article
<6b1a8a88-6116-414d...@a8g2000prf.googlegroups.com>,
SamuelXiao <foolsm...@gmail.com> wrote:

> On Sep 8, 3:29 am, Bo Vance <bowman.va...@invalid.invalid> wrote:

[preserving Bo's excellent advice]


> > Have you precisely defined the problem?
> >
> > So, You Need to Write a Program but Don't Know How to Start
> > <http://home.earthlink.net/~patricia_shanahan/beginner.html>
> >
> > The Java Language Specification, Third Edition
> > <http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html>
> >
> > Specifically Chapters 2 and 3
> > <http://java.sun.com/docs/books/jls/third_edition/html/grammars.html>
> > <http://java.sun.com/docs/books/jls/third_edition/html/lexical.html>
> >
> > The Java Programming Language Compiler, javac
> > <http://java.sun.com/javase/6/docs/technotes/guides/javac/index.html>
>

> I have found out the bugs and fixed the problem[...]

Excellent! Consider sharing your results.

> I still don't know what is different between the following two
> statement:
> 1. while(scanfile.hasNext())
> 2.while((tryread = scanfile.nextLine())!=null)
> In fact, for the 1st one, the compiler will not complain but the 2nd
> will. Why?

The methods scanfile.hasNext() and scanfile.nextLine() work as
advertised. The problem was that the logic in your code called substring
on the result using a negative index. This caused a predictable
IndexOutOfBoundsException, as documented in String:

<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

Your more serious problem is that Scanner is designed to break "its
input into tokens using a delimiter pattern," which is an awkward fit
for your problem, absent an obvious delimiter.

An elementary approach is to start with a program that simply copies
standard input to standard output:

/**
* Copy stdin to stdout, byte by byte.
* @author John B. Matthews
*/
import java.io.*;
public class FilterStream {

public static void main(String[] args) throws IOException {
BufferedInputStream in = new BufferedInputStream(System.in);
int c;

while ((c = in.read()) != -1) {
// your filtering code goes here
System.out.write(c);
}
}
}

This is fine, but it has limitations with character encoding and line
termination. Fortunately, the platform provides a alternative:

/**
* Copy stdin to stdout, line by line.
* @author John B. Matthews
*/
import java.io.*;
public class FilterLine {

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String s;

while ((s = in.readLine()) != null) {
// your filtering code goes here
System.out.println(s);
}
}
}

Once compiled, you can invoke the filter on your test input and see your
results immediately.

$ java FilterLine < test.txt

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

0 new messages