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

Thread: Not able to read from a textfiel the way it's supposed to.

  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 Not able to read from a textfiel the way it's supposed to.

    I had been trying to make digital books. However, I wanted to split it into pages and had been wondering if there was a way to store entire chapters and use tags, like html, sort of, to tell it what to put into the text area for each page. I tried something like this and it's kind of working, though it's losing the first word in between each of the tags. I don't care if the tags themselves aren't read in. But how do I get the first words in? This may also lead to Scanner redundancy reading, but would reduce space wise the amount I'd have to use.

       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());
                if (console.next().equals("<Page1>"))
                {
     
                   while (!console.next().equals("</Page1>"))
                   {
                      String nextInputLine = console.nextLine();
     
                      lineScanner = new Scanner(nextInputLine);
     
                      while(lineScanner.hasNext())
                      {
                         areaText = areaText + lineScanner.nextLine();
                      }
     
                      areaText = areaText + "\n";
                   }
                }
     
             }
             System.out.println(areaText);
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }

    It is giving me this:
     

    is a sample paragraph. It is being used to see if I can only scan part of a document and stop at line breaks. Hopefully I can.
    it is being used to check to see if I can also stop when a page is done. Let's see if it works.




    But I wanted it to give me this:
     

    This is a sample paragraph. It is being used to see if I can only scan part of a document and stop at line

    breaks. Hopefully I can.

    Also, it is being used to check to see if I can also stop when a page is done. Let's see if it works.




    How do I fix that?
    Attached Files Attached Files


  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: Not able to read from a textfiel the way it's supposed to.

    I haven't used the console much, but maybe the console is skipping the first word because of this line:

    while (!console.next().equals("</Page1>"))

    Particulary the console.next() part. To repeat, I'm not entirely sure about this, but maybe because you're calling console.next in this line to check the first word, it is skipping past the first word when you tell it to read the entire line. So, maybe you need to assign console.next() to a String variable before testing its value, so that you can add it back into the line when you call console.nextLine().

    Hopefully that makes sense?
    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. The Following User Says Thank You to snowguy13 For This Useful Post:

    javapenguin (January 27th, 2012)

  4. #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: Not able to read from a textfiel the way it's supposed to.

    I added some lines to the text file. It's getting better but it's still not quite working.

       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())
             {
                int t = 0;
                //System.out.println(console.next());
     
     
                if (console.next().equals("<Page1>"))
                {
     
                   String str = console.next();
                   if (!str.matches("\\s*</Page1>\\s*"))
                   {
                      while (!console.next().equals("</Page1>"))
                      {
                         // areaText = areaText + " " + str;
     
                         String nextInputLine = console.nextLine();
     
     
     
     
     
     
     
     
                         lineScanner = new Scanner(nextInputLine);
     
                         while(lineScanner.hasNext())
                         {
                            areaText = areaText + lineScanner.nextLine();
                         }
     
                         areaText = areaText + "\n";
                      }
                   }
                }
             }
     
     
             System.out.println(areaText);
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
     
     
     
     
       }

    I added a new line: I can almost get it to work, but it won't quite get the tabs.

    Output:
     

    a sample paragraph. It is being used to see if I can only scan part of a document and stop at line breaks. Hopefully I can.
    it is being used to check to see if I can also stop when a page is done. Let's see if it works.
    can almost get it to work, but it won't quite get the tabs.



  5. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    next()
    Finds and returns the next complete token from this scanner.
    So, be careful while using next(), that will skips the tokens. Instead of using like this, change your
    while (!console.next().equals("</Page1>"))
    this line into
    while (console.hasNext() && !console.nextLine().equals("</Page1>"))

  6. The Following User Says Thank You to Mr.777 For This Useful Post:

    javapenguin (January 27th, 2012)

  7. #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: Not able to read from a textfiel the way it's supposed to.

    Ok, now it's ignroing everything in between <Page1> and </Page1>

    and continuing on with the other things and is printing out:

     

    </Page1>
    <Page2>

    </Page2>
    <Page3>
    </Page3>




       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())
             {
                int t = 0;
                //System.out.println(console.next());
     
     
                if (console.next().equals("<Page1>"))
                {
     
                   String str = console.next();
                   if (!str.matches("\\s*</Page1>\\s*"))
                   {
                      while (console.hasNext() && !console.nextLine().equals("</Page1>"))
                      {
                         // areaText = areaText + " " + str;
     
                         String nextInputLine = console.nextLine();
     
     
     
     
     
     
     
     
                         lineScanner = new Scanner(nextInputLine);
     
                         while(lineScanner.hasNext())
                         {
                            areaText = areaText + lineScanner.nextLine();
                         }
     
                         areaText = areaText + "\n";
                      }
                   }
                }
             }
     
     
             System.out.println(areaText);
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
     
     
     
     
       }

    I'm worried I have redundancy. I originally had that second Scanner so that it would recognize line breaks and add a "/n" every time it found a line break.

    However, now I fear it's using nextLine() twice for every loop or something and losing all the data.
    Last edited by javapenguin; January 26th, 2012 at 11:53 PM.

  8. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    Remove this
    if (!str.matches("\\s*</Page1>\\s*"))
    statement.

  9. The Following User Says Thank You to Mr.777 For This Useful Post:

    javapenguin (January 27th, 2012)

  10. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    You must see how your code is actually behaving. Don't just stick with the famous "Hit and Miss". Try to think yourself why it's actually ignoring the internal part, coz you are telling it to. It's not the JVM's fault but your code's.
    Remove the if as already told and try running your program. You've changed many things in your this code. Add the above given while condition in your very first code you have posted here.

  11. The Following User Says Thank You to Mr.777 For This Useful Post:

    javapenguin (January 27th, 2012)

  12. #8
    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: Not able to read from a textfiel the way it's supposed to.

    It worked just fine but when I decided to see what would happen if I added a new line just below the first one but didn't have a blank line, it went back to the bad output again.

       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());
                if (console.next().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";
                   }
                }
     
             }
             System.out.println(areaText);
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }

    However, this error happens if a line is like:


    Bla bla bla bla bla bla [Enter key]
    Bla bla bla bla bla bla

    Or something like that.

    If it wraps on its own, then it's fine.
    Last edited by javapenguin; January 27th, 2012 at 12:34 AM.

  13. #9
    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: Not able to read from a textfiel the way it's supposed to.

    Ok, when I tried it with the second text area, this time using <Page2> </Page2> as the edges, and I wanted it not to read in anything between <Page1> and </Page1> at all, it gave me nothing for the second text area, though the first was fine.

       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());
                if (console.next().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";
                   }
                }
     
             }
     
             while(console.hasNext())
             {
                //System.out.println(console.next());
                if (console.next().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";
                   }
                }
     
             }
     
             System.out.println(areaText);
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }

  14. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    Should i answer Post#08 or Post#09 ?

  15. #11
    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: Not able to read from a textfiel the way it's supposed to.

    Post 8 isn't as import as Post 9. Post 9 should be answered first.

  16. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    Remember that
    while(console.hasNext())
    this code hasNext() until it reaches to the end of file. I hope you got the clue now why it's not showing the results between <page2> and </page2>

  17. #13
    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: Not able to read from a textfiel the way it's supposed to.

    Not quite. I added the lines:


    if (console.next().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";
    }
    }


    to the code above in that while loop and it still wouldn't work.

    It throws a NoSuchElementException in fact.

    Ok, I changed it from an if to an else if and now the exception is gone but it's still returning nothing.

       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());
                if (console.next().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 (console.next().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";
                   }
                }
     
     
             }
     
     
     
     
     
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }
    Last edited by javapenguin; January 27th, 2012 at 02:02 AM.

  18. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

       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())
                       {
                          areaText = areaText + lineScanner.nextLine();
                       }
     
                       areaText = areaText + "\n" + "\n";
                    }
                 } 
             }
     
             while(console.hasNext())
             {
                System.out.println("Hi");
                if (console.next().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";
                   }
                }
     
             }
     
             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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }
    Your code has been modified. Try this code and see..... Try to understand it as well.

  19. The Following User Says Thank You to Mr.777 For This Useful Post:

    javapenguin (January 27th, 2012)

  20. #15
    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: Not able to read from a textfiel the way it's supposed to.

    What is this part for:

    while(console.hasNext())
    {
    System.out.println("Hi");
    if (console.next().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";
    }
    }

    That must have been the old code. It makes sense I guess now.
    Last edited by javapenguin; January 27th, 2012 at 02:35 AM.

  21. #16
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Not able to read from a textfiel the way it's supposed to.

    Yeah, remove or comment out this code. New code will be
       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())
                       {
                          areaText = areaText + lineScanner.nextLine();
                       }
     
                       areaText = areaText + "\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);
             wenguin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
     
     
       }

Similar Threads

  1. Replies: 6
    Last Post: November 25th, 2011, 02:58 PM
  2. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  3. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM
  4. I dont see why this program is not doing what it is supposed to
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2011, 08:24 AM
  5. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM