Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Somehow it's not adding to an ArrayList of ArrayLists.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Somehow it's not adding to an ArrayList of ArrayLists.

    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?

    Attached Files Attached Files
    Last edited by javapenguin; January 28th, 2012 at 12:44 AM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.)

       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?
    Last edited by javapenguin; January 28th, 2012 at 04:55 PM.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Somehow it's not adding to an ArrayList of ArrayLists.

    \ invisible...
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Adding an object to Arraylist from text files
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: January 18th, 2012, 09:27 AM
  2. Creating ArrayLists
    By Khadafi in forum Java Theory & Questions
    Replies: 15
    Last Post: January 12th, 2012, 06:18 PM
  3. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  4. How to separate ArrayLists in memory?
    By wattieboi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:18 AM
  5. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM