1 Attachment(s)
Somehow it's not adding to an ArrayList of ArrayLists.
Code java:
try
{
chapterScanners[0] = new Scanner(new File("./s3c1.txt"));
}
catch(FileNotFoundException fnfe)
{
}
chapterTexts = new ArrayList<ArrayList<String>>();
chapterTexts.add(new ArrayList<String>());
Scanner lineScanner = null;
while(chapterScanners[0].hasNext())
{
//System.out.println(chapterScanners[0].next());
String temp = chapterScanners[0].next();
if (temp.equals("<Page1>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page1>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(0).add(var);
}
else if (temp.equals("<Page2>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page2>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(1).add(var);
}
else if (temp.equals("<Page3>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page3>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(2).add(var);
}
else if (temp.equals("<Page4>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page4>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(3).add(var);
}
else if (temp.equals("<Page5>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page5>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(4).add(var);
}
else if (temp.equals("<Page6>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page6>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(5).add(var);
}
else if (temp.equals("<Page7>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page7>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(0).add(var);
}
else if (temp.equals("<Page8>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page8>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(0).add(var);
}
else if (temp.equals("<Page9>"))
{
String var = "";
while (chapterScanners[0].hasNext() && !chapterScanners[0].nextLine().equals("</Page9>"))
{
String nextInputLine = chapterScanners[0].nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
var = var + lineScanner.nextLine();
}
var = var + "\n" + "\n";
}
chapterTexts.get(0).add(var);
}
}
It's not even reading in every page, I've found, either.
Why the Null Pointers and page ignoring?
It's ignoring all the odd numbered pages. That might be causing the Null Pointer. The question is, why is it doing that?
I do have a title thingy at the beginning, for the chapter heading
Chapter One
Chapter Heading
_____________________________
Like that.
Is that screwing it up?
Also, in addition to not reading in the odd pages, it doesn't appear to be reading in the words
<Page 2></Page2> <Page4></Page4><Page6></Page6> etc, either, though it prints out the
<Page1></Page1><Page3></Page3><Page5></Page5> but has no text.
Basically the even ones don't have the tags but have text and the odd ones have tags but no text.
Why is this happening?
^:)^^:)^~X(~X(~X(~X(~X(~X(~X(~X(~X(~X(~X(~X(~X(
Re: Somehow it's not adding to an ArrayList of ArrayLists.
One thing I noticed: you add one ArrayList of Strings to your ArrayList of ArrayLists of Strings, but then later in your code, you refer to the elements 1, 2, 3, 4, and 5 of that ArrayList..
For example:
chapterTexts.get(4).add(var);
But I only ever see you add one element to chapterTexts.
As for your other issue, I don't know exactly what the problem is... Could you post any errors and such that you get, and the program's output?
Re: Somehow it's not adding to an ArrayList of ArrayLists.
Why am I adding only 1 element?
I've noticed that, but don't know why.
Also, I kept redefining var every time, or I thought I did.
Wait, you're right, I'm only adding one to each of the ArrayLists. That's what comes of getting 1 hour of sleep.
I meant to add them to only the first ArrayList in the ArrayList of ArrayLists.
(I planned to add other chapters and stuff to the other parts so I don't have to keep track of multiple ArrayList variables.)
Also, is \ the escape character in Java or was it / ?
It's still going out of bounds.
I fixed it to only add to the first part. But it's still going out of bounds.
It's my else if structure. Somehow it's only adding it once.
I need to tell it to add it for every single time it encounters it. That's why it's adding only once I think.
But in another example of I had that I thought I'd gotten the concepts down.
This one is a much smaller program. (That thing I posted above was a subset of a much larger program.)
Code java:
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.File;
import java.util.Scanner;
import java.awt.GridLayout;
import java.util.NoSuchElementException;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.IOException;
public class FileReadingTesting
{
public static void main(String[] args)
{
Scanner console = null;
LineNumberReader lnr = null;
String areaText = "";
String area2Text = "";
String area3Text = "";
try{
console = new Scanner(new File("Silly.txt"));
}
catch(FileNotFoundException fnfe)
{
System.out.println("Not found.");
}
Scanner lineScanner = null;
while(console.hasNext())
{
//System.out.println(console.next());
String temp = console.next();
if (temp.equals("<Page1>"))
{
while (console.hasNext() && !console.nextLine().equals("</Page1>"))
{
String nextInputLine = console.nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
areaText = areaText + lineScanner.nextLine();
}
areaText = areaText + "\n" + "\n";
}
}
else if (temp.equals("<Page2>"))
{
while (console.hasNext() && !console.nextLine().equals("</Page2>"))
{
String nextInputLine = console.nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
area2Text = area2Text + lineScanner.nextLine();
}
area2Text = area2Text + "\n" + "\n";
}
}
else if (temp.equals("<Page3>"))
{
while (console.hasNext() && !console.nextLine().equals("</Page3>"))
{
String nextInputLine = console.nextLine();
lineScanner = new Scanner(nextInputLine);
while(lineScanner.hasNext())
{
area3Text = area3Text + lineScanner.nextLine();
}
area3Text = area3Text + "\n" + "\n";
}
}
}
System.out.println(areaText);
System.out.println(area2Text);
JFrame wenguin = new JFrame("Parsing test");
wenguin.setVisible(true);
JPanel panel = new JPanel();
wenguin.setContentPane(panel);
JTextArea area = new JTextArea(100,100);
JScrollPane pane = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextArea area2 = new JTextArea(100,100);
JScrollPane pane2 = new JScrollPane(area2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JTextArea area3 = new JTextArea(100,100);
JScrollPane pane3 = new JScrollPane(area3, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.setLayout(new GridLayout(3,1));
panel.add(pane);
panel.add(pane2);
panel.add(pane3);
char c = 'a';
String t = "";
area.setText(areaText);
area2.setText(area2Text);
area3.setText(area3Text);
wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
This one works. Why wouldn't the other one?
Is it where I place the part that told it to add to the ArrayList that's doing it?
Re: Somehow it's not adding to an ArrayList of ArrayLists.
Re: Somehow it's not adding to an ArrayList of ArrayLists.
Ok, like I suspected somewhat, it didn't like my chapter heading, which must have had a space or something and broken the syntax.
I'd noticed in the previous one that if I had an enter key in there on it's own or something, that it messed up.
I removed the chapter tile thing and it worked.
My code for this program was based off of the demo program I read to make page breaks. The one posted above.