Results 1 to 3 of 3
Thread: FileScanner Question
- 10-18-2010, 04:21 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
FileScanner Question
Hi Everyone.
I am working on a file expansion program that adds files together
Basically the format of the file is:
some text
#include "anotherfile.txt"
and I have to add the files together and produce one output file.
My program does this fine- BUT - it needs to ignore any "#include" directive that is NOT at the beginning of a line.
Is there a way to accomplish this? Here is my code:
Thank you!!while (scan.hasNext())
{
temp = scan.next();
// If it finds another "#include" indicator, it reads the
// next filename and calls itself.
if (temp.equals("#include"))
{
fname = scan.next();
if (fname.charAt(0)!= '"')
{
System.out.println("No quoted filename.");
System.exit(0);
}
fname = fname.substring(1,fname.length()-1);
if (calledfiles.contains(fname))
{
System.out.println("Infinite Loop Detected- Aborting.");
System.exit(0);
}
try
{
scan2 = fileScanner(fname);
}
catch(FileNotFoundException e)
{
System.out.println("File Read Error");
System.exit(0);
}
// ReadIncluded is the function that contains this code- it is recursive
ReadIncluded(scan2,fname);
if (scan.hasNextLine())
{
scan.nextLine();
}
}
else
{
// file is an array that contains the completed file. any text that is not #include or a filename is added here.
file.add(temp);
}
-
Rather than use Scanner#next() how about using Scanner#nextLine(), and then use a specific String method to see if the current temp String starts with your key String.
Oh, by the way, welcome to the forum! A suggestion, when posting code, don't use quote tags, but rather use code tags.
This is easier to read:
Java Code:public MyGui(MyModel mymodel) { this.model = mymodel; JButton addBtn = new JButton("Add"); JButton subtractBtn = new JButton("Subtract"); addBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addActionPerformed(); } }); subtractBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { subtractActionPerformed(); } });
Than this:
Much luck!public MyGui(MyModel mymodel) {
this.model = mymodel;
JButton addBtn = new JButton("Add");
JButton subtractBtn = new JButton("Subtract");
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addActionPerformed();
}
});
subtractBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
subtractActionPerformed();
}
});
- 10-18-2010, 06:18 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Question about what this do.
By Syfer in forum New To JavaReplies: 1Last Post: 07-03-2010, 08:35 AM -
Question about JMF
By Supamagier in forum Advanced JavaReplies: 0Last Post: 05-23-2009, 11:04 AM -
Question!
By anjogomez in forum Java AppletsReplies: 2Last Post: 02-21-2009, 03:24 AM -
JNI question
By javaplus in forum New To JavaReplies: 0Last Post: 12-24-2007, 10:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks