Results 1 to 16 of 16

Thread: Not working when file is imported

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved Not working when file is imported

    Ok. In my the text editor I have been working on, I have methods that select or find parts of text. This works fine, but when you open a file, it doesn't.

    Here is the code to open a file:
    Code:
     public void processOpen()
     {
    	 /* process Opening a new file
    	 */
    	 returnVal = chooser.showOpenDialog(this);
    	 getFile = chooser.getSelectedFile().getPath();
    	 try {
    		 textArea2.read(new FileReader(getFile),null);
    	 }
    	 catch(IOException e)
    	 {
    		 e.printStackTrace();
    	 }
    	
    	 
     }
    Here is the code for finding text:
    Code:
      public void processFind()
     {
    	 /*  When the find method is invoked show an input dialg
    	  * process the string inputed and highlight the text
    	 */
    	String searchString = JOptionPane.showInputDialog("Enter the string to search for");
    	if (getFile == null)
    	{
    		String val = textArea2.getText();
    		int left = val.indexOf(searchString);
    		int right = left + searchString.length();
    		/*  Use the setSelectionStar() and setSelectionEnd()
    		*  methods to highlight the text
    		*/
    		textArea2.setSelectionStart(left);
    		textArea2.setSelectionEnd(right);
    	}
    	else if (getFile != null)
    	{
    	}
    	else 
    	{
    	}
     }

    And here is the code to select all:
    Code:
     public void processSelectAll()
     {
    	 /*  When Select all is clicked by the user
    	  *  process the starting and ending of the 
    	  *  text area and select all of it
    	  */
    	 String val = textArea2.getText();
    	 int left = val.indexOf(val);
    	 int right = left + val.length();
    	 /*  select it
    	  */
    	 textArea2.setSelectionStart(left);
    	 textArea2.setSelectionEnd(right);
    
     }


    I don't know why there would be conflict when I set the contents of the file to the text area.
    Can anyone help me with this?
    Last edited by System_Error; Dec 22nd, 2004 at 08:43 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Not working when file is imported

    I am assuming that textArea2 in this line(textArea2.read(new FileReader(getFile),null)) is a JTextArea. I never heard of a read() method being contained in that class. And if there is then it looks like you are just setting the text of the JTextArea to the whole file.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Is there a different way of doing it, so that everything agrees?

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Not working when file is imported

    Perhaps you could get the String that you want to match from the processFind() method then do somthing like the following.
    Code:
      import java.util.*; 
      import java.util.regex.*; 
     
      Scanner s = new Scanner(wholefilestring);  
      Pattern p = Pattern.compile(selectedstring);
      String result = s.findInLine(p); 
      jta.append(result);
    A Scanner's constructor takes many arguements one being a File.
    http://java.sun.com/j2se/1.5.0/docs/api/

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    I tryed that, but I'm getting an error:
    Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 26....Then it list the path of the file and points to one of the "\" charaters seperating the path.

    Code:
      public void processFind()
     {
    	 
    	String searchString = JOptionPane.showInputDialog("Enter the string to search for");
    	if (getFile == null)
    	{
    		String val = textArea2.getText();
    		int left = val.indexOf(searchString);
    		int right = left + searchString.length();
    		
    		textArea2.setSelectionStart(left);
    		textArea2.setSelectionEnd(right);
    	}
    	else if (getFile != null)
    	{
    		Scanner s = new Scanner(getFile);
    		Pattern p = Pattern.compile(getFile);
    		String result = s.findInLine(p);
    		textArea2.append(result);
    	}
    	else 
    	{
    	}
     }

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Hold on I made a mistake...The Pattern is suppose to be the search string...

  7. #7

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Now that I got it fixed. It compiles and runs fine, but how would I get it to highlight?

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    I tried this and got a null pointer exception...

    Code:
    else if (getFile != null)
    	{
    		String val = textArea2.getText();
    		Scanner s = new Scanner(getFile);
    		Pattern p = Pattern.compile(searchString);
    		String result = s.findInLine(p);
    		int left = val.indexOf(result);
    		int right = left  + searchString.length();
    		textArea2.setSelectionStart(left);
    		textArea2.setSelectionEnd(right);
    	}

  9. #9

  10. #10

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    It does'nt really say at which line it's being thrown...It gives me this:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    (then it gives a list of a bunch of stuff like:
    at java.langlString.indexOf(Unkown Source)
    there is a ton of them, so I won't be able to post them)

  11. #11
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Not working when file is imported

    Yeah that's one thing that used to drive me crazy. When an exception is thrown from awt dos goes nuts. The following worked for me using a .txt file with the contents "What is the line where the exception is being thrown?."
    Code:
     import java.io.*; 
     import java.util.*; 
     import java.util.regex.*;
     
     public class Search{
      public static void main(String[] args){
      try{
       Scanner s = new Scanner(new File("C:" + File.separator + "SearchTest.txt"));
       Pattern p = Pattern.compile("the exception");
       String match = s.findInLine(p);
       System.out.println(match); 
      }catch(IOException io){System.err.println(io);}	
      }
     }

  12. #12

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Ok..The code is fine, but now how could we highlight the text? Or is this even possible the way that I coded the importing of the file? I mean, I don't know how you could apply the any of the select methods to this..Do you think I will have to resort to finding a new way to import the file instead of reading it like I did?

  13. #13

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    I just tried the code for the select all method and it works. So if that was able to highlight the text from a file then we should be able to figure out a way for the find() method to work.

  14. #14

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Dilenger, your going to kill me for this...The reason why it wasn't working is because the cursor wasn't in the text area, and when it is, it works...Sorry for all your trouble. But thanks for trying to help.

  15. #15

  16. #16

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Not working when file is imported

    Well that was nice to know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width