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 7 of 7

Thread: Array List of Array Lists working for first item but not for second.

  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

    Smile Array List of Array Lists working for first item but not for second.

    What am I doing differently in the first part, i.e., chapterTexts.get(0)) that I'm not doing in the second (i.e. chapterTexts.get(1)) ?

    Here's all the parts that could be effecting it:

     
    chapterScanners = new Scanner[19];
             try
             {
                chapterScanners[0] = new Scanner(new File("./s3c1.txt"));
             }
     
                catch(FileNotFoundException fnfe)
                {
                   System.out.println("Not found.");
                }
     
             try
             {
                chapterScanners[1] = new Scanner(new File("./s3c2.txt"));
             }
     
                catch(FileNotFoundException fnfe)
                {
                   System.out.println("Not found.");
                }
     
             //selectPictures = new JMenu("Select Pictures");
             chapterTexts = new ArrayList<ArrayList<String>>();
             chapterTexts.add(new 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(0).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(0).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(0).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(0).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(0).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);
                } 
     
     
             }
     
             System.out.println("Size: " + chapterTexts.get(0).size());
     
             lineScanner = null;
             while(chapterScanners[1].hasNext())
             {
                //System.out.println(chapterScanners[1].next());
                String temp = chapterScanners[1].next();
                if (temp.equals("<Page1>"))
                {
                   String var = "";
                   while (chapterScanners[1].hasNext() && !chapterScanners[1].nextLine().equals("</Page1>"))
                   {
                      String nextInputLine = chapterScanners[1].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("<Page2>"))
                {
                   String var = "";
                   while (chapterScanners[1].hasNext() && !chapterScanners[1].nextLine().equals("</Page2>"))
                   {
                      String nextInputLine = chapterScanners[1].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[1].hasNext() && !chapterScanners[1].nextLine().equals("</Page3>"))
                   {
                      String nextInputLine = chapterScanners[1].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("<Page4>"))
                {
                   String var = "";
                   while (chapterScanners[1].hasNext() && !chapterScanners[1].nextLine().equals("</Page4>"))
                   {
                      String nextInputLine = chapterScanners[1].nextLine();
     
                      lineScanner = new Scanner(nextInputLine);
     
                      while(lineScanner.hasNext())
                      {
                         var = var + lineScanner.nextLine();
                      }
     
                      var = var + "\n" + "\n";
                   }
                   chapterTexts.get(1).add(var);
                } 
     
             }
     
             System.out.println("Size: " + chapterTexts.get(1).size());
             for (int i =0; i < chapterTexts.get(1).size(); i++)
             {
                System.out.println(chapterTexts.get(1).get(i));
     
     
             }

    As it printed out all the text from the files, I don't think ti's a Scanner problem that's causing this so posting the documents won't help any.

    It says the size of the first one is 9, as it should be. But the size of the second is being reported as 1, though it should be 4. What's going wrong?




    I think it's my tags. What's going wrong to make it so that it picks up the tags as part of the read in in the second one but not in the first?

    I'm posting the documents after all.

    It's something very simple no doubt. It happened before in the last one too. Though I can't find the error this time.
    Attached Files Attached Files
    Last edited by javapenguin; February 14th, 2012 at 02:03 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array List of Array Lists working for first item but not for second.

    As you've been told countless times before- if you want help, you'll have to post an SSCCE that demonstrates exactly what you're talking about. No extra stuff, not hundreds of lines of code (or even dozens of lines of code), not attachments.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  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: Array List of Array Lists working for first item but not for second.

    Never mind, it was the very strict and annoying syntax of my parser that required a full line break plus an empty line between paragraphs but only a line break before the ending page tag.

    My only other problem for now, is how would you get something that had a JLabel and a JTable to be in a JPanel and add the JPanel such that it wouldn't have this huge space gap of empty space that isn't needed?

    i.e., I have all the backgrounds as green and the part that has the JTable has a bit that is gray at the end of the JTable. Though the larger the table, the less the gray.

    And it keeps making this weird gap between the title (for the JTable) i.e. the JLabel, and the JTable. I've tried GridLayout, BorderLayout, etc. The weird thing is that it sometimes will get better, or worse, when I add a bunch of panels below, the panel below it varies in size depending on how many pages are in the chapter. Sometimes this really increases the space gap even further and other times it makes it nearly to the tolerable point.

    Is there anything besides layout manager that you can use to get components to fit nicely? The layout manager alone doesn't seem to always do it properly. If a JPanel has GridLayout but some of the sub panels that are components of that JPanel have BorderLayout for them, will that cause the spacing to sometimes not be neat? This is more of a design question than a coding question.

    Also, I was trying to get a JPanel that only contains a flashing JLabel to be a bit smaller. Right now it's too large.

    How do you set the size of components and get them to keep that size is what I'm wondering. The setSize() method appears to be useless and doesn't do anything. It only seems to affect JFrames and JInternalFrames and the like.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array List of Array Lists working for first item but not for second.

    I'm not repeating myself anymore. You know the drill by now. SSCCE (not your whole program) and a specific question, or we can't help you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  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: Array List of Array Lists working for first item but not for second.

    OK, what are UI's used for? Like the setUI() methods? I know UI stands for User Interface but is there ever a reason to design your own UI or are they only there as "helper classes"? If you can make custom UI's, could you use a UI to change how your GUI looks?

    What is a UI used for?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array List of Array Lists working for first item but not for second.

    What did the API tell you? Yes, depends what you mean, and yes. Again, what did the API tell you?

    Also, what does that have to do with any question about ArrayLists?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    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: Array List of Array Lists working for first item but not for second.

    Nothing really. I guess a more pressing question would be to ask how to make this parser thing so that it won't abort if it encounters only 1 line break, instead of two

    i.e.

    text:
    "Bla bla bla bla bla bla bla bla bla bla bla bla bla bla

    Yak Yak Yak Yak Yak
    La La La La La "

    Will only ouput
    Bla bla bla bla bla bla bla bla bla bla bla bla bla bla

    Yak Yak Yak Yak Yak

    Either that, or with the page tag thing, it'll read it in all at once and ignore the tags, hence why the size was only 1. (Unless that was because I had a line break before the ending tag like this

    <Page1>

    Bla bla bla bla

    Bla bla bla bla

    </Page1>

    As a result of those weird annoyances, it often only adds 1 thing to the ArrayList or it adds only part of the text to it, even though it might get all the pages.


     String var = "";
                   while (chapterScanners[2].hasNext() && !chapterScanners[2].nextLine().equals("</Page7>"))
                   {
                      String nextInputLine = chapterScanners[2].nextLine();
     
                      lineScanner = new Scanner(nextInputLine);
     
                      while(lineScanner.hasNext())
                      {
                         var = var + lineScanner.nextLine();
                      }
     
                      var = var + "\n" + "\n";
                   }
                   chapterTexts.get(2).add(var);
                }

    How do I tell it, in the code above, that it's ok to read in a single line break?

    For instance:

    Bla bla bla bla bla bla bla bla bla bla bla bla bla
    I wish the tab key worked here!

    I think is only getting the first line

Similar Threads

  1. method to return the position of the smallest item in an array
    By izzahmed in forum Java Theory & Questions
    Replies: 5
    Last Post: November 4th, 2011, 04:18 AM
  2. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  3. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 09:21 AM
  4. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM
  5. Array Lists, PLEASE HALP
    By Iamthecheese in forum Threads
    Replies: 10
    Last Post: September 7th, 2010, 08:43 PM