• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Java Programming help

Status
Not open for further replies.
Hey guys, I need a little help with my Java programming assignment. The main point of the program is to scan a text file, output an excerpt of it, and then output the whole text file. I got everything i got to do in my head but I can't seem to implement it in code.

The main problem is that I can't output the excerpt and the whole text file at the same time. When I call the function to output the excerpt and the whole text, only one function runs properly - the function I call first.

I don't really know what I'm doing wrong here. I hope you guys can help. Here's the source code.

/*
Program Description:

Program takes a line of text from a text file (excerpt) and outputs it on the console. The program also prints the
entire text file from which the excerpt was taken. The name of the text file to be used is to be inputted as a command line argument.

*/

import java.io.*;
import java.util.Random;
import java.io.FileReader;
import java.io.File;

public class Main

{
BufferedReader reader = null;
File filename = null;
String line;
LineNumberReader lnr = null;
FileReader fr = null;

int numOfLines=0;


public Main (String nameOfTextFile)

{

try

{
filename = new File(nameOfTextFile);
reader = new BufferedReader(new FileReader(filename));
fr = new FileReader(filename);
lnr = new LineNumberReader(fr);

}

catch(FileNotFoundException e)
{
System.out.println("Error: text file does not exist");
}

catch(Exception e)
{
System.out.println("General Errors");
}


}

public void printAllLines ()

{

try
{

line = reader.readLine();

while (line != null)
{
System.out.println(line);
line = reader.readLine();
}

reader.close ();
}

catch(Exception e)
{
System.out.println("Error: file manipulation error");
}


}


public void getExcerpt ()

{

try
{

int startingLineNum=0;
int endingLineNum=0;
int lineCount=0;
Random rand = new Random();

line = reader.readLine();

while (line != null)
{

System.out.println(line);
line = reader.readLine();
numOfLines ++;
}



while (line != null)
{

System.out.println(line);
line = reader.readLine();
numOfLines ++;
}



startingLineNum = rand.nextInt(numOfLines);
endingLineNum = rand.nextInt(numOfLines);


}

catch(Exception e)
{
System.out.println("Error: file manipulation error");
}

finally
{

}
}


public static void main(String[] args) throws Exception

{

try
{

Main excerptDemo = new Main (args[0]);
System.out.println ("\nText File Excerpt:\n");
excerptDemo.getExcerpt ();

System.out.println ("\nExcerpt taken from: \"" +excerptDemo.filename.getName() + "\"\n");
excerptDemo.printAllLines ();

//the two bolded methods are the methods that can't seem to work together

}

catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Error: Name of text file was not specified in command line argument");
}

}




}
 
in the excerpt method you are reading the entire file. Then using the same bufferedreader you are trying to read that file again, so the while in the 2nd method never gets executed because line = null.

If you need a more through explanation let me know.
 
sableholic said:
in the excerpt method you are reading the entire file. Then using the same bufferedreader you are trying to read that file again, so the while in the 2nd method never gets executed because line = null.

If you need a more through explanation let me know.

To expand on his explanation, you're closing the inputstream so you're no longer reading from the file. Thus, all values returned are null. What you should do is reset the inputstream after reading the excerpt (i.e. close the stream then set the reader to be a new instance of bufferedreader exactly as it was before.)

Also, looking at it, your second loop in getExcerpt() never even runs as it will already have encountered the end of the file before falling out of the first loop.
 
Hydrogen Bluebird said:
Thanks for the responses guys. I'll try the things you guys suggested.

Get in the habit of closing readers/writers in the finally block, checking if they are null first and then attempting to close them. Finally will always be executed regardless of error at the end of a try block. Also for most cases you really shouldn't catch Exception, also print out the error with your generic error message.
 
Hydrogen Bluebird said:
Thanks for the responses guys. I'll try the things you guys suggested.

What I'd really suggest doing is reading all the data into an ArrayList then randomly select an index from the Arraylist for getExcerpt(). Finally, just iterate through the Arraylist and print out each item in the ArrayList for printAllLines().
 
wow, it actually worked. I could call both functions now. Thanks guys.

I'M pretty new to java so I stumble into these kind of things from time to time.

GAF saves me again.
 
sableholic said:
Get in the habit of closing readers/writers in the finally block, checking if they are null first and then attempting to close them. Finally will always be executed regardless of error at the end of a try block. Also for most cases you really shouldn't catch Exception, also print out the error with your generic error message.
I agree. Something you might consider is writing your own Exception classes (this is easy) and catching only those. You can also throw an exception with an error message (such as "throw new MyException("This is an error")). In the "catch-block" you can then use the getMessage method of the Exception class to print your message, e.g.:
Code:
catch(MyException e)
{
	System.out.println(e.getMessage);
}

edit: Here's an example of an exception class, you can pretty much use this block and just have to change the name:
Code:
public class PuzzleException extends Exception
{
	PuzzleException() { }
	
	PuzzleException(String message)
	{
		super(message);
	}
}
If you want, you can write two more constructors for a Throwable parameter ("MyException(String message, Throwable cause)") but this should do.
 
Status
Not open for further replies.
Top Bottom