Connecting Tech Pros Worldwide Forums | Help | Site Map

plz help me for this difficult project

Newbie
 
Join Date: Sep 2006
Posts: 23
#1: Oct 15 '06
i want to build a java program that can search for words in a text and can replace word by another one and the interface will be diveded as
1-a large space for a text that is chosen by user
2-a blank for written word
3-find command button
4-finf all command button
5-replace button
6-replace all
i think these packages will help
import java.util.regex.matcher;
import java.util.regex.pattern;
import java.util.stringtokenizer;
import java.util.vector;
import java.io.*;


plzzz help me

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 16 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

i want to build a java program that can search for words in a text and can replace word by another one and the interface will be diveded as
1-a large space for a text that is chosen by user
2-a blank for written word
3-find command button
4-finf all command button
5-replace button
6-replace all
i think these packages will help
import java.util.regex.matcher;
import java.util.regex.pattern;
import java.util.stringtokenizer;
import java.util.vector;
import java.io.*;


plzzz help me

If you are using regex, might as well do way with the StringTokenizer. Also replace that vector with an ArrayList.
I suppose the text comes from a file so a JFileChooser will do.
The best place to start for this is the interface. Play around with your swing and get the interface right. Help is easily available for more specific questions.
Newbie
 
Join Date: Sep 2006
Posts: 23
#3: Oct 16 '06

re: plz help me for this difficult project


i dont konw how to write a code in java and i must do this project ,can anyone do it for me,plzzzzz i need this so badly
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 16 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

i dont konw how to write a code in java and i must do this project ,can anyone do it for me,plzzzzz i need this so badly

When is the project due?
Newbie
 
Join Date: Sep 2006
Posts: 23
#5: Oct 16 '06

re: plz help me for this difficult project


after 2 weeks,on 29 october
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Oct 16 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

after 2 weeks,on 29 october

You've got plenty of time to do it right.
Let's start with the interface:
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.Container;
  4. public class Tamara extends JFrame implements ActionListener { //is allowed to listen for actions
  5.     private Container pane;
  6.     public Tamara() {
  7.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  8.         setTitle("Tamara Omar");
  9.         setSize(400, 400);
  10.         JPanel panel = new JPanel();
  11.  
  12.  
  13.  
  14.         JButton edit = makeButton("Find");
  15.         JButton replace = makeButton("Replace");
  16.         //add the buttons to the panel
  17.         panel.add(edit);
  18.         panel.add(replace);
  19.         //add the panel to the frame at the bottom
  20.         pane.add(panel, "South");
  21.         setVisible(true);
  22.     }
  23.     public JButton makeButton(String label) { //creates a button
  24.         JButton jb = new JButton(label);
  25.         jb.setActionCommand(label); //This message will be set in the action for the button
  26.         jb.addActionListener(this); //this (JFrame) will respond to this button's press
  27.         return jb;
  28.     }
  29.  
  30.     public void actionPerformed(ActionEvent action) {
  31.         String command = action.getActionCommand();
  32.         if(command.equals("Find")) {
  33.             JOptionPane.showMessageDialog(this, "Find");
  34.         }
  35.         else if(command.equals("Replace")) {
  36.             JOptionPane.showMessageDialog(this, "Replace");
  37.         }
  38.     }
  39.     public static void main(String[] args) {
  40.         new Tamara();
  41.     }
  42. }
  43.  
Look up anything you don't understand here. Post if you really won't find it in a textbook. If you feel that you get the hang of it, try to add all the buttons you need for the program and make sure they are responding to mouse click. Post when you are done. this should not take you more than 12 hours!
Newbie
 
Join Date: Sep 2006
Posts: 23
#7: Oct 17 '06

re: plz help me for this difficult project


i couldnt see the interface because i have this error
Tamara is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class Tamara extends JFrame implements ActionListener { //is allowed to listen for actions
Newbie
 
Join Date: Sep 2006
Posts: 23
#8: Oct 17 '06

re: plz help me for this difficult project


i think that there are two missing buttons (find all, replace all) and the bordered space for the text that the user will enter to search for the missing word in it
sorry for annoying u, and im so gratefull for helping me:)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Oct 17 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

i think that there are two missing buttons (find all, replace all) and the bordered space for the text that the user will enter to search for the missing word in it
sorry for annoying u, and im so gratefull for helping me:)

Ok one thing at a time. Can you compile and run the code above yet? Do a copy paste to take the code. It does not give an error on my compiler.


The two missing buttons will hopefully be added by you. We are going to look at the bordered space for the text next, after we've run and understood what we have so far.
Newbie
 
Join Date: Sep 2006
Posts: 23
#10: Oct 17 '06

re: plz help me for this difficult project


i added (replace all) and( find all) and there is no error :)
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
public class Tamara1 extends JFrame implements ActionListener { //is allowed to listen for actions
private Container pane;
public Tamara1() {
pane = getContentPane(); //pane will now replace getContentPane() from now onwards
setTitle("Tamara Omar");
setSize(400, 400);
JPanel panel = new JPanel();



JButton edit = makeButton("Find");
JButton edit1= makeButton("find all");
JButton replace1=makeButton("replace all");
JButton replace = makeButton("Replace");
//add the buttons to the panel
panel.add(edit1);
panel.add(replace1);
panel.add(edit);
panel.add(replace);
//add the panel to the frame at the bottom
pane.add(panel, "South");
setVisible(true);
}
public JButton makeButton(String label) { //creates a button
JButton jb = new JButton(label);
jb.setActionCommand(label); //This message will be set in the action for the button
jb.addActionListener(this); //this (JFrame) will respond to this button's press
return jb;
}

public void actionPerformed(ActionEvent action)
{
String command = action.getActionCommand();
if(command.equals("Find"))
{
JOptionPane.showMessageDialog(this, "Find");
}
else if(command.equals("Replace"))
{
JOptionPane.showMessageDialog(this, "Replace");
}
else if(command.equals("replace all"))
{
JOptionPane.showMessageDialog(this,"replace all");
}
else if(command.equals("find all"))
{
JOptionPane.showMessageDialog(this,"replace all");
}
}


public static void main(String[] args) {
new Tamara1();
}
}


what next?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#11: Oct 18 '06

re: plz help me for this difficult project


Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.Container;
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.util.Scanner;
  7. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  8.     private Container pane;
  9.     JTextArea area;
  10.     JFileChooser fc;
  11.     JPanel southPanel;
  12.     JPanel northPanel;
  13.     JScrollPane scrollPane;
  14.     static private final String newline = "\n";
  15.     public Tamara2() {
  16.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  17.  
  18.  
  19.         setTitle("Tamara Omar");
  20.         setSize(400, 400);
  21.  
  22.  
  23.  
  24.         //
  25.         fc = new JFileChooser();
  26.         area = new JTextArea(10,20);
  27.         scrollPane = new JScrollPane(area);
  28.  
  29.         area.setMargin(new Insets(5,5,5,5));
  30.         area.setEditable(true);
  31.  
  32.  
  33.  
  34.         //make buttons
  35.         JButton open = makeButton("Open");
  36.         JButton save = makeButton("Save");
  37.         JButton edit = makeButton("Find");
  38.         JButton edit1= makeButton("find all");
  39.         JButton replace1=makeButton("replace all");
  40.         JButton replace = makeButton("Replace");
  41.  
  42.         southPanel = new JPanel();
  43.         northPanel = new JPanel();
  44.         //add the buttons to the panel
  45.         southPanel.add(edit1);
  46.         southPanel.add(replace1);
  47.         southPanel.add(edit);
  48.         southPanel.add(replace);
  49.         //add the panel to the frame at the bottom
  50.  
  51.         northPanel.add(open);
  52.         northPanel.add(save);
  53.         pane.add(northPanel, "North");
  54.         pane.add(scrollPane, "Center");
  55.         pane.add(southPanel, "South");
  56.  
  57.         setVisible(true);
  58. }
  59. public JButton makeButton(String label) { //creates a button
  60.     JButton jb = new JButton(label);
  61.     jb.setActionCommand(label); //This message will be set in the action for the button
  62.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  63.     return jb;
  64. }
  65.  
  66. public void actionPerformed(ActionEvent action) {
  67.     String command = action.getActionCommand();
  68.     if(command.equals("Find")) {
  69.         JOptionPane.showMessageDialog(this, "Find");
  70.     }
  71.     else if(command.equals("Replace")) {
  72.         JOptionPane.showMessageDialog(this, "Replace");
  73.     }
  74.     else if(command.equals("replace all")) {
  75.         JOptionPane.showMessageDialog(this,"replace all");
  76.     }
  77.     else if(command.equals("find all"))    {
  78.         JOptionPane.showMessageDialog(this,"replace all");
  79.     }
  80.     else if(command.equals("Open")) {
  81.         int returnVal = fc.showOpenDialog(Tamara2.this);
  82.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  83.              File file = fc.getSelectedFile();
  84.              //This is where a real application would open the file.
  85.              Scanner inFile = null;
  86.              try {
  87.                  inFile = new Scanner(new FileReader(file));
  88.              }
  89.              catch(IOException iOE) {
  90.                  iOE.printStackTrace();
  91.              }
  92.              while (inFile.hasNext()) {
  93.                  String line = inFile.nextLine();
  94.                  area.append(line + newline);
  95.               }
  96.               inFile.close();
  97.           }
  98.           else {
  99.              area.append("Open command cancelled by user." + newline);
  100.          }
  101.          area.setCaretPosition(area.getDocument().getLength());
  102.     }
  103.  
  104. }
  105.  
  106.  
  107. public static void main(String[] args) {
  108.     new Tamara2();
  109. }
  110. }
And so we come to that area of yours.
I have made use of
http://java.sun.com/docs/books/tutorial/uiswing/components/examples/FileChooserDemo.java
For the FileChooser.
Note that I am trying to get the basics of the programming running so that you can then work on it to perfection later. Make sure you understand what is going on so far.
I think one of your buttons is giving the wrong message.
If you understand this, we move over to designing how the user enters their search words
Newbie
 
Join Date: Oct 2006
Posts: 2
#12: Oct 18 '06

re: plz help me for this difficult project


im arabic and i wnat help you mintaining your project so you can ask me via mail
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#13: Oct 18 '06

re: plz help me for this difficult project


Quote:

Originally Posted by soharto

im arabic and i wnat help you mintaining your project so you can ask me via mail

Hi Soharto, nice time to join in. Wouldn't you think that if you help him/her in this forum it would benefit others who read this thread as well rather than through email. Imagine everyone in Tamara's class requesting that you help them through email for the same project!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#14: Oct 18 '06

re: plz help me for this difficult project


Signing off for the day now. You will see now that the rest of this exercise lies in manipulating methods of JTexArea and JTextComponent which should not take more than a day. You should now play around with those methods. Unless of course you've taken the email option.



God Bless
Newbie
 
Join Date: Sep 2006
Posts: 23
#15: Oct 18 '06

re: plz help me for this difficult project


no im gonna be with u all the way,but there r more than one error :
tamara2.java=cant resolve symbol
import java.util.Scanner ; and the error sign is under the S !!!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#16: Oct 19 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

no im gonna be with u all the way,but there r more than one error :
tamara2.java=cant resolve symbol
import java.util.Scanner ; and the error sign is under the S !!!

No need to panic for that. That means you are using a jdk version that is not 1.5 and so your compiler does not recognise the scanner. I've just changed that to using a combination of BufferedReader and FileReader. I also figured you might want a saving functionality after replacing some words so I added that too.
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. //import java.util.Scanner;
  8. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  9.     private Container pane;
  10.     JTextArea area;
  11.     JFileChooser fc;
  12.     JPanel southPanel;
  13.     JPanel northPanel;
  14.     JScrollPane scrollPane;
  15.     static private final String newline = "\n";
  16.     public Tamara2() {
  17.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  18.  
  19.  
  20.         setTitle("Tamara Omar");
  21.         setSize(400, 400);
  22.  
  23.  
  24.  
  25.         //
  26.         fc = new JFileChooser();
  27.         area = new JTextArea(10,20);
  28.         scrollPane = new JScrollPane(area);
  29.  
  30.         area.setMargin(new Insets(5,5,5,5));
  31.         area.setEditable(true);
  32.  
  33.  
  34.  
  35.         //make buttons
  36.         JButton open = makeButton("Open");
  37.         JButton save = makeButton("Save");
  38.         JButton edit = makeButton("Find");
  39.         JButton edit1= makeButton("find all");
  40.         JButton replace1=makeButton("replace all");
  41.         JButton replace = makeButton("Replace");
  42.  
  43.         southPanel = new JPanel();
  44.         northPanel = new JPanel();
  45.         //add the buttons to the panel
  46.         southPanel.add(edit1);
  47.         southPanel.add(replace1);
  48.         southPanel.add(edit);
  49.         southPanel.add(replace);
  50.         //add the panel to the frame at the bottom
  51.  
  52.         northPanel.add(open);
  53.         northPanel.add(save);
  54.         pane.add(northPanel, "North");
  55.         pane.add(scrollPane, "Center");
  56.         pane.add(southPanel, "South");
  57.  
  58.         setVisible(true);
  59. }
  60. public JButton makeButton(String label) { //creates a button
  61.     JButton jb = new JButton(label);
  62.     jb.setActionCommand(label); //This message will be set in the action for the button
  63.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  64.     return jb;
  65. }
  66.  
  67. public void actionPerformed(ActionEvent action) {
  68.     String command = action.getActionCommand();
  69.     if(command.equals("Find")) {
  70.         JOptionPane.showMessageDialog(this, "Find");
  71.     }
  72.     else if(command.equals("Replace")) {
  73.         JOptionPane.showMessageDialog(this, "Replace");
  74.     }
  75.     else if(command.equals("replace all")) {
  76.         JOptionPane.showMessageDialog(this,"replace all");
  77.     }
  78.     else if(command.equals("find all"))    {
  79.         JOptionPane.showMessageDialog(this,"replace all");
  80.     }
  81.     else if(command.equals("Open")) {
  82.         int returnVal = fc.showOpenDialog(Tamara2.this);
  83.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  84.              File file = fc.getSelectedFile();
  85.              //This is where a real application would open the file.
  86.              //Scanner inFile = null;
  87.              BufferedReader inFile = null;
  88.              try {
  89.                  inFile = new BufferedReader(new FileReader(file));
  90.                  String line = "";
  91.                  while ((line = inFile.readLine()) != null) {
  92.                     //String line = inFile.readLine();
  93.                     area.append(line + newline);
  94.                  }
  95.                  inFile.close();
  96.               }
  97.               catch(IOException iOE) {
  98.                               iOE.printStackTrace();
  99.              }
  100.           }
  101.           else {
  102.              area.append("Open command cancelled by user." + newline);
  103.          }
  104.          area.setCaretPosition(area.getDocument().getLength());
  105.     }
  106.     else if(command.equals("Save")) {
  107.         try {
  108.             int returnVal = fc.showSaveDialog(Tamara2.this);
  109.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  110.                 File file = fc.getSelectedFile();
  111.                 //FileOutputStream fos = new FileOutputStream(file);
  112.                 //DataOutputStream dos = new DataOutputStream(fos);
  113.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  114.  
  115.                 //This is where a real application would save the file.
  116.  
  117.                 int rows = area.getLineCount();
  118.  
  119.                 for(int i = 0; i < rows; i++) {
  120.                     int end = area.getLineEndOffset(i);
  121.                     int start = area.getLineStartOffset(i);
  122.                     int length = end - start;
  123.                     if(length <= 0) {
  124.                         length = 1;
  125.                     }
  126.                     String s = area.getText(start, (length-1));
  127.                     System.out.println(s);
  128.                     out.write(s);
  129.                     out.newLine();
  130.                     out.flush();
  131.  
  132.                 }
  133.                 out.close();
  134.  
  135.                 area.append("Saving: " + file.getName() + "." + newline);
  136.             }
  137.             else {
  138.                 area.append("Save command cancelled by user." + newline);
  139.             }
  140.         }
  141.         catch(Exception e) {
  142.             e.printStackTrace();
  143.         }
  144.         area.setCaretPosition(area.getDocument().getLength());
  145.     }
  146.  
  147.  
  148. }
  149.  
  150.  
  151. public static void main(String[] args) {
  152.     new Tamara2();
  153.     }
  154. }
Newbie
 
Join Date: Sep 2006
Posts: 23
#17: Oct 19 '06

re: plz help me for this difficult project


the compiler gives me no error but when i come to execute it gives me this msg:
exception in thread "main" java.lang.NoClassDefFoundError:Tamara
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#18: Oct 19 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

the compiler gives me no error but when i come to execute it gives me this msg:
exception in thread "main" java.lang.NoClassDefFoundError:Tamara

Did you change the name of the class? I have no problem with it here.
Newbie
 
Join Date: Sep 2006
Posts: 23
#19: Oct 19 '06

re: plz help me for this difficult project


ok done,but i have a question ,when the user choose open command it will open his files on his computer ,right?
now what next
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#20: Oct 19 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

ok done,but i have a question ,when the user choose open command it will open his files on his computer ,right?
now what next

The user can search for words that he/she types in on the textarea or search for words in a file on their computer. Here is an example with the find button. Note all the changes made to the code so far. Please make sure you are analysing the approach critically and try to spot some improvements.
Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.util.ArrayList;
  8. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  9.     private Container pane;
  10.     JTextArea area;
  11.     JFileChooser fc;
  12.     JPanel southPanel;
  13.     JPanel northPanel;
  14.     JPanel fieldsPanel;
  15.     JPanel buttonsPanel;
  16.     JScrollPane scrollPane;
  17.     JTextField findText;
  18.     JTextField replaceText;
  19.     static private final String newline = "\n";
  20.  
  21.     private int lastPosition = 0;
  22.     public Tamara2() {
  23.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  24.         setTitle("Tamara Omar");
  25.         setSize(400, 400);
  26. //
  27.         fc = new JFileChooser();
  28.         area = new JTextArea(10,20);
  29.         area.setDocument(new DefaultStyledDocument());
  30.         scrollPane = new JScrollPane(area);
  31.  
  32.         area.setMargin(new Insets(5,5,5,5));
  33.         area.setEditable(true);
  34.  
  35.         //make buttons
  36.         JButton open = makeButton("Open");
  37.         JButton save = makeButton("Save");
  38.         JButton edit = makeButton("Find");
  39.         JButton findAll = makeButton("Find All");
  40.         JButton replaceAll = makeButton("Replace All");
  41.         JButton replace = makeButton("Replace");
  42.         findText = new JTextField(10);
  43.         replaceText = new JTextField(10);
  44.  
  45.  
  46.         southPanel = new JPanel();
  47.         northPanel = new JPanel();
  48.         fieldsPanel = new JPanel();
  49.         buttonsPanel = new JPanel();
  50.         southPanel.setLayout(new GridLayout(2, 1));
  51.         //add the buttons to the panel
  52.         buttonsPanel.add(findAll);
  53.         buttonsPanel.add(replaceAll);
  54.         buttonsPanel.add(edit);
  55.         buttonsPanel.add(replace);
  56.  
  57.         //add the fields to the panel
  58.         fieldsPanel.add(new JLabel("Find:"));
  59.         fieldsPanel.add(findText);
  60.         fieldsPanel.add(new JLabel("Replace with:"));
  61.         fieldsPanel.add(replaceText);
  62.  
  63.  
  64.         //add the panel to the frame at the bottom
  65.         southPanel.add(fieldsPanel);
  66.         southPanel.add(buttonsPanel);
  67.  
  68.         northPanel.add(open);
  69.         northPanel.add(save);
  70.         pane.add(northPanel, "North");
  71.         pane.add(scrollPane, "Center");
  72.         pane.add(southPanel, "South");
  73.  
  74.         setVisible(true);
  75. }
  76. public JButton makeButton(String label) { //creates a button
  77.     JButton jb = new JButton(label);
  78.     jb.setActionCommand(label); //This message will be set in the action for the button
  79.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  80.     return jb;
  81. }
  82.  
  83. public void actionPerformed(ActionEvent action) {
  84.     String command = action.getActionCommand();
  85.     if(command.equals("Find")) {
  86.         area.setSelectedTextColor(Color.blue);
  87.         String searchString = findText.getText();
  88.         ArrayList matches = findAll(area, searchString);
  89.         if(matches.size() == 0) {
  90.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  91.         }
  92.         else {
  93.             for(int i = 0; i < matches.size(); i++) {
  94.                 int x = (int)(Integer)matches.get(i);
  95.                 area.setCaretPosition(x);
  96.                 area.moveCaretPosition(x + searchString.length());
  97.                 area.requestFocus();
  98.  
  99.             }
  100.         }
  101.     }
  102.     else if(command.equals("Replace")) {
  103.         JOptionPane.showMessageDialog(this, "Replace");
  104.     }
  105.     else if(command.equals("Replace All")) {
  106.         JOptionPane.showMessageDialog(this,"Replace All");
  107.     }
  108.     else if(command.equals("Find All"))    {
  109.         JOptionPane.showMessageDialog(this,"Find All");
  110.     }
  111.     else if(command.equals("Open")) {
  112.         int returnVal = fc.showOpenDialog(Tamara2.this);
  113.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  114.              File file = fc.getSelectedFile();
  115.              BufferedReader inFile = null;
  116.              try {
  117.                  inFile = new BufferedReader(new FileReader(file));
  118.                  String line = "";
  119.                  while ((line = inFile.readLine()) != null) {
  120.                     area.append(line + newline);
  121.                  }
  122.                  inFile.close();
  123.               }
  124.               catch(IOException iOE) {
  125.                               iOE.printStackTrace();
  126.              }
  127.           }
  128.           else {
  129.              area.append("Open command cancelled by user." + newline);
  130.          }
  131.  
  132.     }
  133.     else if(command.equals("Save")) {
  134.         try {
  135.             int returnVal = fc.showSaveDialog(Tamara2.this);
  136.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  137.                 File file = fc.getSelectedFile();
  138.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  139.                 int rows = area.getLineCount();
  140.                 for(int i = 0; i < rows; i++) {
  141.                     int end = area.getLineEndOffset(i);
  142.                     int start = area.getLineStartOffset(i);
  143.                     int length = end - start;
  144.                     if(length <= 0) {
  145.                         length = 1;
  146.                     }
  147.                     String s = area.getText(start, (length-1));
  148.                     out.write(s);
  149.                     out.newLine();
  150.                     out.flush();
  151.  
  152.                 }
  153.                 out.close();
  154.  
  155.  
  156.             }
  157.             else {
  158.                 area.append("Save command cancelled by user." + newline);
  159.             }
  160.         }
  161.         catch(Exception e) {
  162.             e.printStackTrace();
  163.         }
  164.         area.setCaretPosition(area.getDocument().getLength());
  165.     }
  166.  
  167.  
  168. }
  169. public ArrayList findAll(JTextArea area, String text) {
  170.     ArrayList startPositions = new ArrayList();
  171.     int length = text.length();
  172.     int rows = area.getLineCount();
  173.     try {
  174.         int all = area.getLineEndOffset((rows - 1));
  175.         for(int i = 0; i < (all - length); i++) {
  176.             String x = area.getText(i, length);
  177.             if(x.equalsIgnoreCase(text)) {
  178.                 startPositions.add(new Integer(i));
  179.             }
  180.         }
  181.     }
  182.     catch(BadLocationException e) {
  183.         e.printStackTrace();
  184.     }
  185.     return startPositions;
  186.  
  187. }
  188.  
  189.  
  190. public static void main(String[] args) {
  191.     new Tamara2();
  192.     }
  193. }
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#21: Oct 19 '06

re: plz help me for this difficult project


Hey Tamara if you are still there, you might have noticed that the search I've done is actually for findAll. So edit the actionPerformed method to something like this
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent action) {
  2.     String command = action.getActionCommand();
  3.     if(command.equals("Find")) {
  4.         JOptionPane.showMessageDialog(this,"Find");
  5.     }
  6.     else if(command.equals("Replace")) {
  7.         JOptionPane.showMessageDialog(this, "Replace");
  8.     }
  9.     else if(command.equals("Replace All")) {
  10.         JOptionPane.showMessageDialog(this,"Replace All");
  11.     }
  12.     else if(command.equals("Find All"))    {
  13.         area.setSelectedTextColor(Color.blue);
  14.         String searchString = findText.getText();
  15.         ArrayList matches = findAll(area, searchString);
  16.         if(matches.size() == 0) {
  17.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  18.         }
  19.         else {
  20.             try {
  21.                 for(int i = 0; i < matches.size(); i++) {
  22.                     int x = (int)(Integer)matches.get(i);
  23.                     //area.setCaretPosition(x);
  24.                     //area.moveCaretPosition(x + searchString.length());
  25.                     int z = x + searchString.length();
  26.                     area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  27.                     area.requestFocus();
  28.                 }
  29.             }
  30.             catch(Exception e) {
  31.                 e.printStackTrace();
  32.             }
  33.  
  34.         }
  35.  
  36.     }
  37.     else if(command.equals("Open")) {  
The Find all button should be the one activated now and should now be selecting all occurrences of the search string. If you've done that right then that will be as far as we will go today. Unless you have any questions concerning what we've done so far?
Newbie
 
Join Date: Sep 2006
Posts: 23
#22: Oct 19 '06

re: plz help me for this difficult project


i hate the errors ,
tamara2.java:94: inconvertable types
found:java.lang.integer
required:int int x=(int)(Integer)matches.get(i);
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#23: Oct 19 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

i hate the errors ,
tamara2.java:94: inconvertable types
found:java.lang.integer
required:int int x=(int)(Integer)matches.get(i);

Must be the 1.5 autoboxing that is not available on your compiler.
change the line to
Expand|Select|Wrap|Line Numbers
  1. int x = ((Integer)matches.get(i)).intValue();
The more errors one gets, the better one gets at programming so they are not too bad after all.
Newbie
 
Join Date: Sep 2006
Posts: 23
#24: Oct 20 '06

re: plz help me for this difficult project


it still the replace and replace all doesnt work,and when i open any file it doesnt appear in the text area, and there is an important thing which is these packages should be included (they are for searching as regular expressions u know like we can put in the search area /much/ so it brings much or /[abcd]/ so it matches a or b or c or d !!! i think these packages for this use)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.*;

this is the latest version from our program :)
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Container;
import java.io.*;
import java.awt.*;
import java.util.ArrayList;
public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
private Container pane;
JTextArea area;
JFileChooser fc;
JPanel southPanel;
JPanel northPanel;
JPanel fieldsPanel;
JPanel buttonsPanel;
JScrollPane scrollPane;
JTextField findText;
JTextField replaceText;
static private final String newline = "\n";

private int lastPosition = 0;
public Tamara2() {
pane = getContentPane(); //pane will now replace getContentPane() from now onwards
setTitle("Tamara Omar");
setSize(400, 400);
//
fc = new JFileChooser();
area = new JTextArea(10,20);
area.setDocument(new DefaultStyledDocument());
scrollPane = new JScrollPane(area);

area.setMargin(new Insets(5,5,5,5));
area.setEditable(true);

//make buttons
JButton open = makeButton("Open");
JButton save = makeButton("Save");
JButton edit = makeButton("Find");
JButton findAll = makeButton("Find All");
JButton replaceAll = makeButton("Replace All");
JButton replace = makeButton("Replace");
findText = new JTextField(10);
replaceText = new JTextField(10);


southPanel = new JPanel();
northPanel = new JPanel();
fieldsPanel = new JPanel();
buttonsPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 1));
//add the buttons to the panel
buttonsPanel.add(findAll);
buttonsPanel.add(replaceAll);
buttonsPanel.add(edit);
buttonsPanel.add(replace);

//add the fields to the panel
fieldsPanel.add(new JLabel("Find:"));
fieldsPanel.add(findText);
fieldsPanel.add(new JLabel("Replace with:"));
fieldsPanel.add(replaceText);


//add the panel to the frame at the bottom
southPanel.add(fieldsPanel);
southPanel.add(buttonsPanel);

northPanel.add(open);
northPanel.add(save);
pane.add(northPanel, "North");
pane.add(scrollPane, "Center");
pane.add(southPanel, "South");

setVisible(true);
}
public JButton makeButton(String label) { //creates a button
JButton jb = new JButton(label);
jb.setActionCommand(label); //This message will be set in the action for the button
jb.addActionListener(this); //this (JFrame) will respond to this button's press
return jb;
}

public void actionPerformed(ActionEvent action) {
String command = action.getActionCommand();
if(command.equals("Find")) {
area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}
}
else if(command.equals("Replace")) {
area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}

}
else if(command.equals("Replace All")) { area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}

}
else if(command.equals("Find All")) { area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}

}
else if(command.equals("Open")) {
int returnVal = fc.showOpenDialog(Tamara2.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedReader inFile = null;
try {
inFile = new BufferedReader(new FileReader(file));
String line = "";
while ((line = inFile.readLine()) != null) {
area.append(line + newline);
}
inFile.close();
}
catch(IOException iOE) {
iOE.printStackTrace();
}
}
else {
area.append("Open command cancelled by user." + newline);
}

}
else if(command.equals("Save")) {
try {
int returnVal = fc.showSaveDialog(Tamara2.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
int rows = area.getLineCount();
for(int i = 0; i < rows; i++) {
int end = area.getLineEndOffset(i);
int start = area.getLineStartOffset(i);
int length = end - start;
if(length <= 0) {
length = 1;
}
String s = area.getText(start, (length-1));
out.write(s);
out.newLine();
out.flush();

}
out.close();


}
else {
area.append("Save command cancelled by user." + newline);
}
}
catch(Exception e) {
e.printStackTrace();
}
area.setCaretPosition(area.getDocument().getLength ());
}


}
public ArrayList findAll(JTextArea area, String text) {
ArrayList startPositions = new ArrayList();
int length = text.length();
int rows = area.getLineCount();
try {
int all = area.getLineEndOffset((rows - 1));
for(int i = 0; i < (all - length); i++) {
String x = area.getText(i, length);
if(x.equalsIgnoreCase(text)) {
startPositions.add(new Integer(i));
}
}
}
catch(BadLocationException e) {
e.printStackTrace();
}
return startPositions;

}


public static void main(String[] args) {
new Tamara2();
}
}
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#25: Oct 20 '06

re: plz help me for this difficult project


Quote:
it still the replace and replace all doesnt work,and when i open any file it doesnt appear in the text area, and there is an important thing which is these packages should be included (they are for searching as regular expressions u know like we can put in the search area /much/ so it brings much or /[abcd]/ so it matches a or b or c or d !!! i think these packages for this use)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.*;
The classes in java.util.regex are for regular expressions. We will use these to do the search in the find, findAll, methods. You should look at the following to get a good understanding of regular expressions. I had not used them yet because I thought you might not have had a chance to look at them yet.
These are probably the best resources for them: http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://java.sun.com/docs/books/tutorial/essential/regex/
If you look at this you will realise that use of regular expressions replaces StringTokenizer and so there is no point in using both of them.
Concerning the Vector, is it absolutely neccessary that it be used because it's one of those classes (like StringTokenizer) which should now be avoided. I have used ArrayList instead but can always replace it with vector if you really must use the vector.

What type of files are refusing to be opened in the text area? I can open .txt, .java, .doc(text only),. Remember we only want to search for text.

You seem to have misunderstood the last post. Only the findAll is supposed to be working right now. The current should be something like this:

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.util.ArrayList;
  8. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  9.     private Container pane;
  10.     JTextArea area;
  11.     JFileChooser fc;
  12.     JPanel southPanel;
  13.     JPanel northPanel;
  14.     JPanel fieldsPanel;
  15.     JPanel buttonsPanel;
  16.     JScrollPane scrollPane;
  17.     JTextField findText;
  18.     JTextField replaceText;
  19.     static private final String newline = "\n";
  20.  
  21.     private int lastPosition = 0;
  22.     public Tamara2() {
  23.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  24.         setTitle("Tamara Omar");
  25.         setSize(400, 400);
  26. //
  27.         fc = new JFileChooser();
  28.         area = new JTextArea(10,20);
  29.         area.setDocument(new DefaultStyledDocument());
  30.         scrollPane = new JScrollPane(area);
  31.  
  32.         area.setMargin(new Insets(5,5,5,5));
  33.         area.setEditable(true);
  34.  
  35.         //make buttons
  36.         JButton open = makeButton("Open");
  37.         JButton save = makeButton("Save");
  38.         JButton edit = makeButton("Find");
  39.         JButton findAll = makeButton("Find All");
  40.         JButton replaceAll = makeButton("Replace All");
  41.         JButton replace = makeButton("Replace");
  42.         findText = new JTextField(10);
  43.         replaceText = new JTextField(10);
  44.  
  45.  
  46.         southPanel = new JPanel();
  47.         northPanel = new JPanel();
  48.         fieldsPanel = new JPanel();
  49.         buttonsPanel = new JPanel();
  50.         southPanel.setLayout(new GridLayout(2, 1));
  51.         //add the buttons to the panel
  52.         buttonsPanel.add(findAll);
  53.         buttonsPanel.add(replaceAll);
  54.         buttonsPanel.add(edit);
  55.         buttonsPanel.add(replace);
  56.  
  57.         //add the fields to the panel
  58.         fieldsPanel.add(new JLabel("Find:"));
  59.         fieldsPanel.add(findText);
  60.         fieldsPanel.add(new JLabel("Replace with:"));
  61.         fieldsPanel.add(replaceText);
  62.  
  63.  
  64.         //add the panel to the frame at the bottom
  65.         southPanel.add(fieldsPanel);
  66.         southPanel.add(buttonsPanel);
  67.  
  68.         northPanel.add(open);
  69.         northPanel.add(save);
  70.         pane.add(northPanel, "North");
  71.         pane.add(scrollPane, "Center");
  72.         pane.add(southPanel, "South");
  73.  
  74.         setVisible(true);
  75. }
  76. public JButton makeButton(String label) { //creates a button
  77.     JButton jb = new JButton(label);
  78.     jb.setActionCommand(label); //This message will be set in the action for the button
  79.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  80.     return jb;
  81. }
  82.  
  83. public void actionPerformed(ActionEvent action) {
  84.     String command = action.getActionCommand();
  85.     if(command.equals("Find")) {
  86.         JOptionPane.showMessageDialog(this,"Find");
  87.     }
  88.     else if(command.equals("Replace")) {
  89.         JOptionPane.showMessageDialog(this, "Replace");
  90.     }
  91.     else if(command.equals("Replace All")) {
  92.         JOptionPane.showMessageDialog(this,"Replace All");
  93.     }
  94.     else if(command.equals("Find All"))    {
  95.         area.setSelectedTextColor(Color.blue);
  96.         String searchString = findText.getText();
  97.         ArrayList matches = findAll(area, searchString);
  98.         if(matches.size() == 0) {
  99.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  100.         }
  101.         else {
  102.             try {
  103.                 for(int i = 0; i < matches.size(); i++) {
  104.                     int x = ((Integer)matches.get(i)).intValue();
  105.                     //area.setCaretPosition(x);
  106.                     //area.moveCaretPosition(x + searchString.length());
  107.                     int z = x + searchString.length();
  108.                     area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  109.                     area.requestFocus();
  110.                 }
  111.                 JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
  112.             }
  113.             catch(Exception e) {
  114.                 e.printStackTrace();
  115.             }
  116.  
  117.         }
  118.  
  119.     }
  120.     else if(command.equals("Open")) {
  121.         int returnVal = fc.showOpenDialog(Tamara2.this);
  122.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  123.              File file = fc.getSelectedFile();
  124.              BufferedReader inFile = null;
  125.              try {
  126.                  inFile = new BufferedReader(new FileReader(file));
  127.                  String line = "";
  128.                  while ((line = inFile.readLine()) != null) {
  129.                     area.append(line + newline);
  130.                  }
  131.                  inFile.close();
  132.               }
  133.               catch(IOException iOE) {
  134.                               iOE.printStackTrace();
  135.              }
  136.           }
  137.           else {
  138.              area.append("Open command cancelled by user." + newline);
  139.          }
  140.  
  141.     }
  142.     else if(command.equals("Save")) {
  143.         try {
  144.             int returnVal = fc.showSaveDialog(Tamara2.this);
  145.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  146.                 File file = fc.getSelectedFile();
  147.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  148.                 int rows = area.getLineCount();
  149.                 for(int i = 0; i < rows; i++) {
  150.                     int end = area.getLineEndOffset(i);
  151.                     int start = area.getLineStartOffset(i);
  152.                     int length = end - start;
  153.                     if(length <= 0) {
  154.                         length = 1;
  155.                     }
  156.                     String s = area.getText(start, (length-1));
  157.                     out.write(s);
  158.                     out.newLine();
  159.                     out.flush();
  160.  
  161.                 }
  162.                 out.close();
  163.  
  164.  
  165.             }
  166.             else {
  167.                 area.append("Save command cancelled by user." + newline);
  168.             }
  169.         }
  170.         catch(Exception e) {
  171.             e.printStackTrace();
  172.         }
  173.         area.setCaretPosition(area.getDocument().getLength());
  174.     }
  175.  
  176.  
  177. }
  178.  
  179. //********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
  180. public ArrayList findAll(JTextArea area, String text) {
  181.     ArrayList startPositions = new ArrayList();
  182.     int length = text.length();
  183.     int rows = area.getLineCount();
  184.     try {
  185.         int all = area.getLineEndOffset((rows - 1));
  186.         for(int i = 0; i < (all - length); i++) {
  187.             String x = area.getText(i, length);
  188.             if(x.equalsIgnoreCase(text)) {
  189.                 startPositions.add(new Integer(i));
  190.             }
  191.         }
  192.     }
  193.     catch(BadLocationException e) {
  194.         e.printStackTrace();
  195.     }
  196.     return startPositions;
  197.  
  198. }
  199.  
  200.  
  201. public static void main(String[] args) {
  202.     new Tamara2();
  203.     }
  204. }
FindAll is supposed to load the start positions of the found words in the ArrayList(Vector). After you've gone through the fisrt of those regex tutorials, try to write and post an implementation of findAll that uses regular expressions. If you take more than 2 hours to get it right, just post what you'll have then and we'll look at it together.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#26: Oct 20 '06

re: plz help me for this difficult project


Sounds like you are begginning to panick so here is the ReplaceAll that uses regular expressions.

1) add
Expand|Select|Wrap|Line Numbers
  1. import java.util.regex.*;
to the list of imports.

2) add the following method to the class

Expand|Select|Wrap|Line Numbers
  1. //************Does a replace all based on regular expressions*******************
  2. public void replaceAll(JTextArea area, String find, String replace) {
  3.      Pattern p = Pattern.compile(find);
  4.  
  5.     Matcher m = p.matcher(area.getText());
  6.     StringBuffer sb = new StringBuffer();
  7.     boolean result = m.find();
  8.     while(result) {
  9.         m.appendReplacement(sb, replace);
  10.         result = m.find();
  11.     }
  12.     m.appendTail(sb);
  13.     area.setText(sb.toString());
  14.  
  15. }
3.) the n command for replace all
Expand|Select|Wrap|Line Numbers
  1. else if(command.equals("Replace All")) {
  2.        String find = findText.getText();
  3.        String replace = replaceText.getText();
  4.        replaceAll(area, find, replace);
  5.        area.requestFocus();
  6.        //JOptionPane.showMessageDialog(this,"Replace All");
  7. }
Newbie
 
Join Date: Sep 2006
Posts: 23
#27: Oct 20 '06

re: plz help me for this difficult project


import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Container;
import java.io.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.regex.*;
public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
private Container pane;
JTextArea area;
JFileChooser fc;
JPanel southPanel;
JPanel northPanel;
JPanel fieldsPanel;
JPanel buttonsPanel;
JScrollPane scrollPane;
JTextField findText;
JTextField replaceText;
static private final String newline = "\n";

private int lastPosition = 0;
public Tamara2() {
pane = getContentPane(); //pane will now replace getContentPane() from now onwards
setTitle("Tamara Omar");
setSize(400, 400);
//
fc = new JFileChooser();
area = new JTextArea(10,20);
area.setDocument(new DefaultStyledDocument());
scrollPane = new JScrollPane(area);

area.setMargin(new Insets(5,5,5,5));
area.setEditable(true);

//make buttons
JButton open = makeButton("Open");
JButton save = makeButton("Save");
JButton edit = makeButton("Find");
JButton findAll = makeButton("Find All");
JButton replaceAll = makeButton("Replace All");
JButton replace = makeButton("Replace");
findText = new JTextField(10);
replaceText = new JTextField(10);


southPanel = new JPanel();
northPanel = new JPanel();
fieldsPanel = new JPanel();
buttonsPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 1));
//add the buttons to the panel
buttonsPanel.add(findAll);
buttonsPanel.add(replaceAll);
buttonsPanel.add(edit);
buttonsPanel.add(replace);

//add the fields to the panel
fieldsPanel.add(new JLabel("Find:"));
fieldsPanel.add(findText);
fieldsPanel.add(new JLabel("Replace with:"));
fieldsPanel.add(replaceText);


//add the panel to the frame at the bottom
southPanel.add(fieldsPanel);
southPanel.add(buttonsPanel);

northPanel.add(open);
northPanel.add(save);
pane.add(northPanel, "North");
pane.add(scrollPane, "Center");
pane.add(southPanel, "South");

setVisible(true);
}
public JButton makeButton(String label) { //creates a button
JButton jb = new JButton(label);
jb.setActionCommand(label); //This message will be set in the action for the button
jb.addActionListener(this); //this (JFrame) will respond to this button's press
return jb;
}

public void actionPerformed(ActionEvent action) {
String command = action.getActionCommand();
if(command.equals("Find")) {
area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}
}
else if(command.equals("Replace")) {
area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}

}
else if(command.equals("Replace All")) {
String find = findText.getText();
String replace = replaceText.getText();
replaceAll(area, find, replace);
area.requestFocus();
//JOptionPane.showMessageDialog(this,"Replace All");
}

}
else if(command.equals("Find All")) { area.setSelectedTextColor(Color.blue);
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
area.setCaretPosition(x);
area.moveCaretPosition(x + searchString.length());
area.requestFocus();

}
}

}
else if(command.equals("Open")) {
int returnVal = fc.showOpenDialog(Tamara2.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedReader inFile = null;
try {
inFile = new BufferedReader(new FileReader(file));
String line = "";
while ((line = inFile.readLine()) != null) {
area.append(line + newline);
}
inFile.close();
}
catch(IOException iOE) {
iOE.printStackTrace();
}
}
else {
area.append("Open command cancelled by user." + newline);
}

}
else if(command.equals("Save")) {
try {
int returnVal = fc.showSaveDialog(Tamara2.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
int rows = area.getLineCount();
for(int i = 0; i < rows; i++) {
int end = area.getLineEndOffset(i);
int start = area.getLineStartOffset(i);
int length = end - start;
if(length <= 0) {
length = 1;
}
String s = area.getText(start, (length-1));
out.write(s);
out.newLine();
out.flush();

}
out.close();


}
else {
area.append("Save command cancelled by user." + newline);
}
}
catch(Exception e) {
e.printStackTrace();
}
area.setCaretPosition(area.getDocument().getLength ());
}


}
public ArrayList findAll(JTextArea area, String text) {
ArrayList startPositions = new ArrayList();
int length = text.length();
int rows = area.getLineCount();
try {
int all = area.getLineEndOffset((rows - 1));
for(int i = 0; i < (all - length); i++) {
String x = area.getText(i, length);
if(x.equalsIgnoreCase(text)) {
startPositions.add(new Integer(i));
}
}
}
catch(BadLocationException e) {
e.printStackTrace();
}
return startPositions;

}
public void replaceAll(JTextArea area, String find, String replace) {
Pattern p = Pattern.compile(find);

Matcher m = p.matcher(area.getText());
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while(result) {
m.appendReplacement(sb, replace);
result = m.find();
}
m.appendTail(sb);
area.setText(sb.toString());

}


public static void main(String[] args) {
new Tamara2();
}
}



i did what u told me to do:( and this gives a lot of errors)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#28: Oct 21 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar


i did what u told me to do:( and this gives a lot of errors)

No you did not. If you did, your code would be looking like this:

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.util.ArrayList;
  8. import java.util.regex.*;
  9. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  10.     private Container pane;
  11.     JTextArea area;
  12.     JFileChooser fc;
  13.     JPanel southPanel;
  14.     JPanel northPanel;
  15.     JPanel fieldsPanel;
  16.     JPanel buttonsPanel;
  17.     JScrollPane scrollPane;
  18.     JTextField findText;
  19.     JTextField replaceText;
  20.     Matcher matcher;
  21.     private String searchText = "";
  22.     static private final String newline = "\n";
  23.  
  24.     private int lastPosition = 0;
  25.     public Tamara2() {
  26.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  27.         setTitle("Tamara Omar");
  28.         setSize(400, 400);
  29. //
  30.         fc = new JFileChooser();
  31.         area = new JTextArea(10,20);
  32.         area.setDocument(new DefaultStyledDocument());
  33.         scrollPane = new JScrollPane(area);
  34.  
  35.         area.setMargin(new Insets(5,5,5,5));
  36.         area.setEditable(true);
  37.  
  38.         //make buttons
  39.         JButton clear = makeButton("Clear");
  40.         JButton open = makeButton("Open");
  41.         JButton save = makeButton("Save");
  42.         JButton edit = makeButton("Find");
  43.         JButton findAll = makeButton("Find All");
  44.         JButton replaceAll = makeButton("Replace All");
  45.         JButton replace = makeButton("Replace");
  46.         findText = new JTextField(10);
  47.         replaceText = new JTextField(10);
  48.  
  49.  
  50.         southPanel = new JPanel();
  51.         northPanel = new JPanel();
  52.         fieldsPanel = new JPanel();
  53.         buttonsPanel = new JPanel();
  54.         southPanel.setLayout(new GridLayout(2, 1));
  55.         //add the buttons to the panel
  56.         buttonsPanel.add(findAll);
  57.         buttonsPanel.add(replaceAll);
  58.         buttonsPanel.add(edit);
  59.         buttonsPanel.add(replace);
  60.  
  61.         //add the fields to the panel
  62.         fieldsPanel.add(new JLabel("Find:"));
  63.         fieldsPanel.add(findText);
  64.         fieldsPanel.add(new JLabel("Replace with:"));
  65.         fieldsPanel.add(replaceText);
  66.  
  67.  
  68.         //add the panel to the frame at the bottom
  69.         southPanel.add(fieldsPanel);
  70.         southPanel.add(buttonsPanel);
  71.  
  72.         northPanel.add(open);
  73.         northPanel.add(save);
  74.         northPanel.add(clear);
  75.  
  76.         pane.add(northPanel, "North");
  77.         pane.add(scrollPane, "Center");
  78.         pane.add(southPanel, "South");
  79.  
  80.         setVisible(true);
  81. }
  82. public JButton makeButton(String label) { //creates a button
  83.     JButton jb = new JButton(label);
  84.     jb.setActionCommand(label); //This message will be set in the action for the button
  85.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  86.     return jb;
  87. }
  88.  
  89. public void actionPerformed(ActionEvent action) {
  90.     String command = action.getActionCommand();
  91.     if(command.equals("Find")) {
  92.         JOptionPane.showMessageDialog(this,"Find");
  93.     }
  94.     else if(command.equals("Replace")) {
  95.         JOptionPane.showMessageDialog(this, "Replace");
  96.     }
  97.     else if(command.equals("Replace All")) {
  98.         String find = findText.getText();
  99.         String replace = replaceText.getText();
  100.         replaceAll(area, find, replace);
  101.         area.requestFocus();
  102.         //JOptionPane.showMessageDialog(this,"Replace All");
  103.     }
  104.     else if(command.equals("Find All"))    {
  105.         area.setSelectedTextColor(Color.blue);
  106.         area.getHighlighter().removeAllHighlights();
  107.         String searchString = findText.getText();
  108.         ArrayList matches = findAll(area, searchString);
  109.         if(matches.size() == 0) {
  110.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  111.         }
  112.         else {
  113.             try {
  114.                 for(int i = 0; i < matches.size(); i++) {
  115.                     int x = ((Integer)matches.get(i)).intValue();
  116.                     //area.setCaretPosition(x);
  117.                     //area.moveCaretPosition(x + searchString.length());
  118.                     int z = x + searchString.length();
  119.                     area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  120.  
  121.                     area.requestFocus();
  122.                 }
  123.                 JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
  124.             }
  125.             catch(Exception e) {
  126.                 e.printStackTrace();
  127.             }
  128.  
  129.         }
  130.  
  131.     }
  132.     else if(command.equals("Open")) {
  133.         int returnVal = fc.showOpenDialog(Tamara2.this);
  134.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  135.              File file = fc.getSelectedFile();
  136.              BufferedReader inFile = null;
  137.              try {
  138.                  inFile = new BufferedReader(new FileReader(file));
  139.                  String line = "";
  140.                  while ((line = inFile.readLine()) != null) {
  141.                     area.append(line + newline);
  142.                  }
  143.                  inFile.close();
  144.               }
  145.               catch(IOException iOE) {
  146.                               iOE.printStackTrace();
  147.              }
  148.           }
  149.           else {
  150.              area.append("Open command cancelled by user." + newline);
  151.          }
  152.  
  153.     }
  154.     else if(command.equals("Save")) {
  155.         try {
  156.             int returnVal = fc.showSaveDialog(Tamara2.this);
  157.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  158.                 File file = fc.getSelectedFile();
  159.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  160.                 int rows = area.getLineCount();
  161.                 for(int i = 0; i < rows; i++) {
  162.                     int end = area.getLineEndOffset(i);
  163.                     int start = area.getLineStartOffset(i);
  164.                     int length = end - start;
  165.                     if(length <= 0) {
  166.                         length = 1;
  167.                     }
  168.                     String s = area.getText(start, (length-1));
  169.                     out.write(s);
  170.                     out.newLine();
  171.                     out.flush();
  172.  
  173.                 }
  174.                 out.close();
  175.  
  176.  
  177.             }
  178.             else {
  179.                 area.append("Save command cancelled by user." + newline);
  180.             }
  181.         }
  182.         catch(Exception e) {
  183.             e.printStackTrace();
  184.         }
  185.         area.setCaretPosition(area.getDocument().getLength());
  186.     }
  187.     if(command.equals("Clear")) {
  188.         area.setText("");
  189.     }
  190.  
  191.  
  192. }
  193.  
  194. //********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
  195. public ArrayList findAll(JTextArea area, String text) {
  196.     ArrayList startPositions = new ArrayList();
  197.     int length = text.length();
  198.     int rows = area.getLineCount();
  199.     try {
  200.         int all = area.getLineEndOffset((rows - 1));
  201.         for(int i = 0; i < (all - length); i++) {
  202.             String x = area.getText(i, length);
  203.             if(x.equalsIgnoreCase(text)) {
  204.                 startPositions.add(new Integer(i));
  205.             }
  206.         }
  207.     }
  208.     catch(BadLocationException e) {
  209.         e.printStackTrace();
  210.     }
  211.     return startPositions;
  212.  
  213. }
  214. //************Does a replace all based on regular expressions*******************
  215. public void replaceAll(JTextArea area, String find, String replace) {
  216.     Pattern p = Pattern.compile(find);
  217.     matcher = p.matcher(area.getText());
  218.     StringBuffer sb = new StringBuffer();
  219.     sb.append(matcher.replaceAll(replace));
  220.     area.setText(sb.toString());
  221. }
  222.  
  223.  
  224.  
  225. public static void main(String[] args) {
  226.     new Tamara2();
  227.     }
  228. }
1) Replace All working and using regex
2)Find All working but not yet using regex package

Your task was to make find All work using regex package
Newbie
 
Join Date: Sep 2006
Posts: 23
#29: Oct 21 '06

re: plz help me for this difficult project


plz im going crazy,i dont know what to do !! when i type a word and press the button find (the word disappears)!!!
and when i type an regular expression and press find it doesnt work
and when i open a word document the text area become dark and doesnt open it, i think we dont need for an open command

but now tell me what to do:( this is the code ,is it right till now?

import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.Container;
import java.io.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.regex.*;
public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
private Container pane;
JTextArea area;
JFileChooser fc;
JPanel southPanel;
JPanel northPanel;
JPanel fieldsPanel;
JPanel buttonsPanel;
JScrollPane scrollPane;
JTextField findText;
JTextField replaceText;
Matcher matcher;
private String searchText = "";
static private final String newline = "\n";

private int lastPosition = 0;
public Tamara2() {
pane = getContentPane(); //pane will now replace getContentPane() from now onwards
setTitle("Tamara Omar");
setSize(400, 400);
//
fc = new JFileChooser();
area = new JTextArea(10,20);
area.setDocument(new DefaultStyledDocument());
scrollPane = new JScrollPane(area);

area.setMargin(new Insets(5,5,5,5));
area.setEditable(true);

//make buttons
JButton clear = makeButton("Clear");
JButton open = makeButton("Open");
JButton save = makeButton("Save");
JButton edit = makeButton("Find");
JButton findAll = makeButton("Find All");
JButton replaceAll = makeButton("Replace All");
JButton replace = makeButton("Replace");
findText = new JTextField(10);
replaceText = new JTextField(10);


southPanel = new JPanel();
northPanel = new JPanel();
fieldsPanel = new JPanel();
buttonsPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 1));
//add the buttons to the panel
buttonsPanel.add(findAll);
buttonsPanel.add(replaceAll);
buttonsPanel.add(edit);
buttonsPanel.add(replace);

//add the fields to the panel
fieldsPanel.add(new JLabel("Find:"));
fieldsPanel.add(findText);
fieldsPanel.add(new JLabel("Replace with:"));
fieldsPanel.add(replaceText);


//add the panel to the frame at the bottom
southPanel.add(fieldsPanel);
southPanel.add(buttonsPanel);

northPanel.add(open);
northPanel.add(save);
northPanel.add(clear);

pane.add(northPanel, "North");
pane.add(scrollPane, "Center");
pane.add(southPanel, "South");

setVisible(true);
}
public JButton makeButton(String label) { //creates a button
JButton jb = new JButton(label);
jb.setActionCommand(label); //This message will be set in the action for the button
jb.addActionListener(this); //this (JFrame) will respond to this button's press
return jb;
}

public void actionPerformed(ActionEvent action) {
String command = action.getActionCommand();
if(command.equals("Find")) {String find = findText.getText();
String replace = replaceText.getText();
replaceAll(area, find, replace);
area.requestFocus();
//JOptionPane.showMessageDialog(this,"find");

}
else if(command.equals("Replace")) {String find = findText.getText();
String replace = replaceText.getText();
replaceAll(area, find, replace);
area.requestFocus();
//JOptionPane.showMessageDialog(this,"Replace ");

}
else if(command.equals("Replace All")) {
String find = findText.getText();
String replace = replaceText.getText();
replaceAll(area, find, replace);
area.requestFocus();
//JOptionPane.showMessageDialog(this,"Replace All");
}
else if(command.equals("Find All")) {
area.setSelectedTextColor(Color.blue);
area.getHighlighter().removeAllHighlights();
String searchString = findText.getText();
ArrayList matches = findAll(area, searchString);
if(matches.size() == 0) {
JOptionPane.showMessageDialog(this, searchString+" Was not found");
}
else {
try {
for(int i = 0; i < matches.size(); i++) {
int x = ((Integer)matches.get(i)).intValue();
//area.setCaretPosition(x);
//area.moveCaretPosition(x + searchString.length());
int z = x + searchString.length();
area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.b lue));

area.requestFocus();
}
JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
}
catch(Exception e) {
e.printStackTrace();
}

}

}
else if(command.equals("Open")) {
int returnVal = fc.showOpenDialog(Tamara2.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedReader inFile = null;
try {
inFile = new BufferedReader(new FileReader(file));
String line = "";
while ((line = inFile.readLine()) != null) {
area.append(line + newline);
}
inFile.close();
}
catch(IOException iOE) {
iOE.printStackTrace();
}
}
else {
area.append("Open command cancelled by user." + newline);
}

}
else if(command.equals("Save")) {
try {
int returnVal = fc.showSaveDialog(Tamara2.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
int rows = area.getLineCount();
for(int i = 0; i < rows; i++) {
int end = area.getLineEndOffset(i);
int start = area.getLineStartOffset(i);
int length = end - start;
if(length <= 0) {
length = 1;
}
String s = area.getText(start, (length-1));
out.write(s);
out.newLine();
out.flush();

}
out.close();


}
else {
area.append("Save command cancelled by user." + newline);
}
}
catch(Exception e) {
e.printStackTrace();
}
area.setCaretPosition(area.getDocument().getLength ());
}
if(command.equals("Clear")) {
area.setText("");
}


}

//********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
public ArrayList findAll(JTextArea area, String text) {
ArrayList startPositions = new ArrayList();
int length = text.length();
int rows = area.getLineCount();
try {
int all = area.getLineEndOffset((rows - 1));
for(int i = 0; i < (all - length); i++) {
String x = area.getText(i, length);
if(x.equalsIgnoreCase(text)) {
startPositions.add(new Integer(i));
}
}
}
catch(BadLocationException e) {
e.printStackTrace();
}
return startPositions;

}
//************Does a replace all based on regular expressions*******************
public void replaceAll(JTextArea area, String find, String replace) {
Pattern p = Pattern.compile(find);
matcher = p.matcher(area.getText());
StringBuffer sb = new StringBuffer();
sb.append(matcher.replaceAll(replace));
area.setText(sb.toString());
}



public static void main(String[] args) {
new Tamara2();
}
}
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#30: Oct 21 '06

re: plz help me for this difficult project


For now just restrict to opening .txt, .java, .html.(Until you get the basics of the programme working.) If we do not put the open command then we only limit the search to text that has been entered only. I included to make the program more interesting. For example you can edit your own files by replacing some words with others and saving the changes.

Here is how you should have done the find (notice I've changed it to find next). Perhaps now you can change the findAll to use the regular expressions. When I run on my machine, I am able to match with the patterns

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.util.ArrayList;
  8. import java.util.regex.*;
  9. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  10.     private Container pane;
  11.     JTextArea area;
  12.     JFileChooser fc;
  13.     JPanel southPanel;
  14.     JPanel northPanel;
  15.     JPanel fieldsPanel;
  16.     JPanel buttonsPanel;
  17.     JScrollPane scrollPane;
  18.     JTextField findText;
  19.     JTextField replaceText;
  20.     Matcher matcher;
  21.     Pattern pattern;
  22.     private String previous = "";
  23.     static private final String newline = "\n";
  24.  
  25.     private int lastPosition = 0;
  26.     public Tamara2() {
  27.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  28.         setTitle("Tamara Omar");
  29.         setSize(400, 400);
  30. //
  31.         fc = new JFileChooser();
  32.         area = new JTextArea(10,20);
  33.         area.setDocument(new DefaultStyledDocument());
  34.         scrollPane = new JScrollPane(area);
  35.  
  36.         area.setMargin(new Insets(5,5,5,5));
  37.         area.setEditable(true);
  38.  
  39.         //make buttons
  40.         JButton clear = makeButton("Clear");
  41.         JButton open = makeButton("Open");
  42.         JButton save = makeButton("Save");
  43.         JButton edit = makeButton("Find Next");
  44.         JButton findAll = makeButton("Find All");
  45.         JButton replaceAll = makeButton("Replace All");
  46.         JButton replace = makeButton("Replace");
  47.         findText = new JTextField(10);
  48.         replaceText = new JTextField(10);
  49.  
  50.  
  51.         southPanel = new JPanel();
  52.         northPanel = new JPanel();
  53.         fieldsPanel = new JPanel();
  54.         buttonsPanel = new JPanel();
  55.         southPanel.setLayout(new GridLayout(2, 1));
  56.         //add the buttons to the panel
  57.         buttonsPanel.add(findAll);
  58.         buttonsPanel.add(replaceAll);
  59.         buttonsPanel.add(edit);
  60.         buttonsPanel.add(replace);
  61.  
  62.         //add the fields to the panel
  63.         fieldsPanel.add(new JLabel("Find:"));
  64.         fieldsPanel.add(findText);
  65.         fieldsPanel.add(new JLabel("Replace with:"));
  66.         fieldsPanel.add(replaceText);
  67.  
  68.  
  69.         //add the panel to the frame at the bottom
  70.         southPanel.add(fieldsPanel);
  71.         southPanel.add(buttonsPanel);
  72.  
  73.         northPanel.add(open);
  74.         northPanel.add(save);
  75.         northPanel.add(clear);
  76.  
  77.         pane.add(northPanel, "North");
  78.         pane.add(scrollPane, "Center");
  79.         pane.add(southPanel, "South");
  80.  
  81.         setVisible(true);
  82. }
  83. public JButton makeButton(String label) { //creates a button
  84.     JButton jb = new JButton(label);
  85.     jb.setActionCommand(label); //This message will be set in the action for the button
  86.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  87.     return jb;
  88. }
  89.  
  90. public void actionPerformed(ActionEvent action) {
  91.     String command = action.getActionCommand();
  92.     if(command.equals("Find Next")) {
  93.         int pos = 0;
  94.         try {
  95.             String s = findText.getText();
  96.             if(previous.equalsIgnoreCase(s)) {
  97.                 pos = findNext(area, s);
  98.             }
  99.             else {
  100.                 pos = find(area, s);
  101.             }
  102.             if(pos == -1) {
  103.                 JOptionPane.showMessageDialog(this,s+" was not found");
  104.             }
  105.             else {
  106.                 int z = pos + s.length();
  107.                 area.getHighlighter().removeAllHighlights();
  108.                 area.getHighlighter().addHighlight(pos, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  109.                 area.requestFocus();
  110.             }
  111.  
  112.         }
  113.         catch(Exception e) {
  114.             e.printStackTrace();
  115.         }
  116.     }
  117.     else if(command.equals("Replace")) {
  118.         JOptionPane.showMessageDialog(this, "Replace");
  119.     }
  120.     else if(command.equals("Replace All")) {
  121.         String find = findText.getText();
  122.         String replace = replaceText.getText();
  123.         replaceAll(area, find, replace);
  124.         area.requestFocus();
  125.         //JOptionPane.showMessageDialog(this,"Replace All");
  126.     }
  127.     else if(command.equals("Find All"))    {
  128.         area.setSelectedTextColor(Color.blue);
  129.         area.getHighlighter().removeAllHighlights();
  130.         String searchString = findText.getText();
  131.         ArrayList matches = findAll(area, searchString);
  132.         if(matches.size() == 0) {
  133.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  134.         }
  135.         else {
  136.             try {
  137.                 for(int i = 0; i < matches.size(); i++) {
  138.                     int x = ((Integer)matches.get(i)).intValue();
  139.                     //area.setCaretPosition(x);
  140.                     //area.moveCaretPosition(x + searchString.length());
  141.                     int z = x + searchString.length();
  142.                     area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  143.  
  144.                     area.requestFocus();
  145.                 }
  146.                 JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
  147.             }
  148.             catch(Exception e) {
  149.                 e.printStackTrace();
  150.             }
  151.  
  152.         }
  153.  
  154.     }
  155.     else if(command.equals("Open")) {
  156.         int returnVal = fc.showOpenDialog(Tamara2.this);
  157.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  158.              File file = fc.getSelectedFile();
  159.              BufferedReader inFile = null;
  160.              try {
  161.                  inFile = new BufferedReader(new FileReader(file));
  162.                  String line = "";
  163.                  while ((line = inFile.readLine()) != null) {
  164.                     area.append(line + newline);
  165.                  }
  166.                  inFile.close();
  167.               }
  168.               catch(IOException iOE) {
  169.                               iOE.printStackTrace();
  170.              }
  171.           }
  172.           else {
  173.              area.append("Open command cancelled by user." + newline);
  174.          }
  175.  
  176.     }
  177.     else if(command.equals("Save")) {
  178.         try {
  179.             int returnVal = fc.showSaveDialog(Tamara2.this);
  180.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  181.                 File file = fc.getSelectedFile();
  182.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  183.                 int rows = area.getLineCount();
  184.                 for(int i = 0; i < rows; i++) {
  185.                     int end = area.getLineEndOffset(i);
  186.                     int start = area.getLineStartOffset(i);
  187.                     int length = end - start;
  188.                     if(length <= 0) {
  189.                         length = 1;
  190.                     }
  191.                     String s = area.getText(start, (length-1));
  192.                     out.write(s);
  193.                     out.newLine();
  194.                     out.flush();
  195.  
  196.                 }
  197.                 out.close();
  198.  
  199.  
  200.             }
  201.             else {
  202.                 area.append("Save command cancelled by user." + newline);
  203.             }
  204.         }
  205.         catch(Exception e) {
  206.             e.printStackTrace();
  207.         }
  208.         area.setCaretPosition(area.getDocument().getLength());
  209.     }
  210.     if(command.equals("Clear")) {
  211.         area.setText("");
  212.     }
  213.  
  214.  
  215. }
  216.  
  217. //********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
  218. public ArrayList findAll(JTextArea area, String text) {
  219.     ArrayList startPositions = new ArrayList();
  220.     int length = text.length();
  221.     int rows = area.getLineCount();
  222.     try {
  223.         int all = area.getLineEndOffset((rows - 1));
  224.         for(int i = 0; i < (all - length); i++) {
  225.             String x = area.getText(i, length);
  226.             if(x.equalsIgnoreCase(text)) {
  227.                 startPositions.add(new Integer(i));
  228.             }
  229.         }
  230.     }
  231.     catch(BadLocationException e) {
  232.         e.printStackTrace();
  233.     }
  234.     return startPositions;
  235.  
  236. }
  237. //************Does a replace all based on regular expressions*******************
  238. public void replaceAll(JTextArea area, String find, String replace) {
  239.     Pattern p = Pattern.compile(find);
  240.     matcher = p.matcher(area.getText());
  241.     StringBuffer sb = new StringBuffer();
  242.     sb.append(matcher.replaceAll(replace));
  243.     area.setText(sb.toString());
  244.     matcher = null;
  245. }
  246. public int find(JTextArea area, String find) {
  247.     boolean found = false;
  248.     int pos = 0;
  249.     pattern = Pattern.compile(find);
  250.     previous = find;
  251.     matcher = pattern.matcher(area.getText());
  252.     found = matcher.find(0);
  253.     if(!found) {
  254.         pos = -1;
  255.         lastPosition = 0;
  256.     }
  257.     else {
  258.         pos = matcher.start();
  259.         lastPosition = pos + find.length();
  260.     }
  261.     return pos;
  262. }
  263.  
  264. public int findNext(JTextArea area, String find) {
  265.     boolean found = false;
  266.     int pos = 0;
  267.     //pattern = Pattern.compile(find);
  268.     //previous = find;
  269.     //matcher = pattern.matcher(area.getText());
  270.     found = matcher.find(lastPosition);
  271.     if(!found) {
  272.         pos = -1;
  273.         lastPosition = 0;
  274.     }
  275.     else {
  276.         pos = matcher.start();
  277.         lastPosition = pos + find.length();
  278.     }
  279.     return pos;
  280. }
  281.  
  282.  
  283.  
  284. public static void main(String[] args) {
  285.     new Tamara2();
  286.     }
  287. }
Newbie
 
Join Date: Sep 2006
Posts: 23
#31: Oct 21 '06

re: plz help me for this difficult project


everything is more than great but im still not able to find by regular expressions,i will run this program on the university computer so if it worked it will be good but if not i will tell the instructer that i couldnt handle it
so for now im very sorry if i was a big trouble for u and thaaaaaaaaanks alot for everything u have done and maybe i will need u soon in another project:)
thanks again
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#32: Oct 23 '06

re: plz help me for this difficult project


Quote:

Originally Posted by tamara omar

everything is more than great but im still not able to find by regular expressions,i will run this program on the university computer so if it worked it will be good but if not i will tell the instructer that i couldnt handle it
so for now im very sorry if i was a big trouble for u and thaaaaaaaaanks alot for everything u have done and maybe i will need u soon in another project:)
thanks again

Here is how you were supposed to do the find All and the replace.
Personally I think the program needs to be improved before it is submitted (Starting with the interface).

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import javax.swing.text.*;
  3. import java.awt.event.*;
  4. import java.awt.Container;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.util.ArrayList;
  8. import java.util.regex.*;
  9. public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
  10.     private Container pane;
  11.     JTextArea area;
  12.     JFileChooser fc;
  13.     JPanel southPanel;
  14.     JPanel northPanel;
  15.     JPanel fieldsPanel;
  16.     JPanel buttonsPanel;
  17.     JScrollPane scrollPane;
  18.     JTextField findText;
  19.     JTextField replaceText;
  20.     Matcher matcher;
  21.     Pattern pattern;
  22.     private String previous = "";
  23.     static private final String newline = "\n";
  24.     private int lastPosition = 0;
  25.     public Tamara2() {
  26.         pane = getContentPane(); //pane will now replace getContentPane() from now onwards
  27.         setTitle("Tamara Omar");
  28.         setSize(400, 400);
  29. //
  30.         fc = new JFileChooser();
  31.         area = new JTextArea(10,20);
  32.         area.setDocument(new DefaultStyledDocument());
  33.         scrollPane = new JScrollPane(area);
  34.  
  35.         area.setMargin(new Insets(5,5,5,5));
  36.         area.setEditable(true);
  37.  
  38.         //make buttons
  39.         JButton clear = makeButton("Clear");
  40.         JButton open = makeButton("Open");
  41.         JButton save = makeButton("Save");
  42.         JButton edit = makeButton("Find Next");
  43.         JButton findAll = makeButton("Find All");
  44.         JButton replaceAll = makeButton("Replace All");
  45.         JButton replace = makeButton("Replace");
  46.         findText = new JTextField(10);
  47.         replaceText = new JTextField(10);
  48.  
  49.  
  50.         southPanel = new JPanel();
  51.         northPanel = new JPanel();
  52.         fieldsPanel = new JPanel();
  53.         buttonsPanel = new JPanel();
  54.         southPanel.setLayout(new GridLayout(2, 1));
  55.         //add the buttons to the panel
  56.         buttonsPanel.add(findAll);
  57.         buttonsPanel.add(replaceAll);
  58.         buttonsPanel.add(edit);
  59.         buttonsPanel.add(replace);
  60.  
  61.         //add the fields to the panel
  62.         fieldsPanel.add(new JLabel("Find:"));
  63.         fieldsPanel.add(findText);
  64.         fieldsPanel.add(new JLabel("Replace with:"));
  65.         fieldsPanel.add(replaceText);
  66.  
  67.  
  68.         //add the panel to the frame at the bottom
  69.         southPanel.add(fieldsPanel);
  70.         southPanel.add(buttonsPanel);
  71.  
  72.         northPanel.add(open);
  73.         northPanel.add(save);
  74.         northPanel.add(clear);
  75.  
  76.         pane.add(northPanel, "North");
  77.         pane.add(scrollPane, "Center");
  78.         pane.add(southPanel, "South");
  79.  
  80.         setVisible(true);
  81. }
  82. public JButton makeButton(String label) { //creates a button
  83.     JButton jb = new JButton(label);
  84.     jb.setActionCommand(label); //This message will be set in the action for the button
  85.     jb.addActionListener(this); //this (JFrame) will respond to this button's press
  86.     return jb;
  87. }
  88.  
  89. public void actionPerformed(ActionEvent action) {
  90.     String command = action.getActionCommand();
  91.     if(command.equals("Find Next")) {
  92.         int pos = 0;
  93.         try {
  94.             String s = findText.getText();
  95.             if(previous.equalsIgnoreCase(s)) {
  96.                 pos = findNext(area, s);
  97.             }
  98.             else {
  99.                 pos = find(area, s);
  100.             }
  101.             if(pos == -1) {
  102.                 JOptionPane.showMessageDialog(this,s+" was not found");
  103.             }
  104.             else {
  105.                 int z = pos + s.length();
  106.                 area.getHighlighter().removeAllHighlights();
  107.                 area.getHighlighter().addHighlight(pos, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  108.                 area.requestFocus();
  109.             }
  110.  
  111.         }
  112.         catch(Exception e) {
  113.             e.printStackTrace();
  114.         }
  115.     }
  116.     else if(command.equals("Replace")) {
  117.         if(!replace(area, findText.getText(), replaceText.getText()) ) {
  118.             JOptionPane.showMessageDialog(this, "Text to replace was not found");
  119.         }
  120.     }
  121.     else if(command.equals("Replace All")) {
  122.         String find = findText.getText();
  123.         String replace = replaceText.getText();
  124.         replaceAll(area, find, replace);
  125.         area.requestFocus();
  126.         //JOptionPane.showMessageDialog(this,"Replace All");
  127.     }
  128.     else if(command.equals("Find All"))    {
  129.         area.setSelectedTextColor(Color.blue);
  130.         area.getHighlighter().removeAllHighlights();
  131.         String searchString = findText.getText();
  132.         ArrayList matches = findAll(area, searchString);
  133.         if(matches.size() == 0) {
  134.             JOptionPane.showMessageDialog(this, searchString+" Was not found");
  135.         }
  136.         else {
  137.             try {
  138.                 for(int i = 0; i < matches.size(); i++) {
  139.                     int x = ((Integer)matches.get(i)).intValue();
  140.                     //area.setCaretPosition(x);
  141.                     //area.moveCaretPosition(x + searchString.length());
  142.                     int z = x + searchString.length();
  143.                     area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
  144.  
  145.                     area.requestFocus();
  146.                 }
  147.                 JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
  148.             }
  149.             catch(Exception e) {
  150.                 e.printStackTrace();
  151.             }
  152.  
  153.         }
  154.  
  155.     }
  156.     else if(command.equals("Open")) {
  157.         int returnVal = fc.showOpenDialog(Tamara2.this);
  158.         if (returnVal == JFileChooser.APPROVE_OPTION) {
  159.              File file = fc.getSelectedFile();
  160.              BufferedReader inFile = null;
  161.              try {
  162.                  inFile = new BufferedReader(new FileReader(file));
  163.                  String line = "";
  164.                  while ((line = inFile.readLine()) != null) {
  165.                     area.append(line + newline);
  166.                  }
  167.                  inFile.close();
  168.               }
  169.               catch(IOException iOE) {
  170.                               iOE.printStackTrace();
  171.              }
  172.           }
  173.           else {
  174.              area.append("Open command cancelled by user." + newline);
  175.          }
  176.  
  177.     }
  178.     else if(command.equals("Save")) {
  179.         try {
  180.             int returnVal = fc.showSaveDialog(Tamara2.this);
  181.             if(returnVal == JFileChooser.APPROVE_OPTION) {
  182.                 File file = fc.getSelectedFile();
  183.                 BufferedWriter out = new BufferedWriter(new FileWriter(file));
  184.                 int rows = area.getLineCount();
  185.                 for(int i = 0; i < rows; i++) {
  186.                     int end = area.getLineEndOffset(i);
  187.                     int start = area.getLineStartOffset(i);
  188.                     int length = end - start;
  189.                     if(length <= 0) {
  190.                         length = 1;
  191.                     }
  192.                     String s = area.getText(start, (length-1));
  193.                     out.write(s);
  194.                     out.newLine();
  195.                     out.flush();
  196.  
  197.                 }
  198.                 out.close();
  199.  
  200.  
  201.             }
  202.             else {
  203.                 area.append("Save command cancelled by user." + newline);
  204.             }
  205.         }
  206.         catch(Exception e) {
  207.             e.printStackTrace();
  208.         }
  209.         area.setCaretPosition(area.getDocument().getLength());
  210.     }
  211.     if(command.equals("Clear")) {
  212.         area.setText("");
  213.     }
  214.  
  215.  
  216. }
  217.  
  218. //********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
  219. public ArrayList findAll(JTextArea area, String text) {
  220.     ArrayList startPositions = new ArrayList();
  221.     int pos = 0;
  222.     boolean found = false;
  223.     pattern = Pattern.compile(text);
  224.     matcher = pattern.matcher(area.getText());
  225.     while(pos != -1) {
  226.         found = matcher.find(lastPosition);
  227.         if(!found) {
  228.             pos = -1;
  229.         }
  230.         else {
  231.             pos = matcher.start();
  232.             startPositions.add(new Integer(pos));
  233.             lastPosition = pos + text.length();
  234.         }
  235.     }
  236.     return startPositions;
  237.  
  238. }
  239. //************Does a replace all based on regular expressions*******************
  240. public void replaceAll(JTextArea area, String find, String replace) {
  241.     Pattern p = Pattern.compile(find);
  242.     matcher = p.matcher(area.getText());
  243.     StringBuffer sb = new StringBuffer();
  244.     sb.append(matcher.replaceAll(replace));
  245.     area.setText(sb.toString());
  246.     matcher = null;
  247. }
  248. public int find(JTextArea area, String find) {
  249.     boolean found = false;
  250.     int pos = 0;
  251.     pattern = Pattern.compile(find);
  252.     previous = find;
  253.     matcher = pattern.matcher(area.getText());
  254.     found = matcher.find(0);
  255.     if(!found) {
  256.         pos = -1;
  257.         lastPosition = 0;
  258.     }
  259.     else {
  260.         pos = matcher.start();
  261.         lastPosition = pos + find.length();
  262.     }
  263.     return pos;
  264. }
  265.  
  266. public int findNext(JTextArea area, String find) {
  267.     boolean found = false;
  268.     int pos = 0;
  269.     found = matcher.find(lastPosition);
  270.     if(!found) {
  271.         pos = -1;
  272.         lastPosition = 0;
  273.     }
  274.     else {
  275.         pos = matcher.start();
  276.         lastPosition = pos + find.length();
  277.     }
  278.     return pos;
  279. }
  280. public boolean replace (JTextArea area, String find, String replace) {
  281.     boolean found = false;
  282.     int pos = 0;
  283.     pattern = Pattern.compile(find);
  284.     previous = find;
  285.     matcher = pattern.matcher(area.getText());
  286.     found = matcher.find(0);
  287.     if(!found) {
  288.         pos = -1;
  289.         lastPosition = 0;
  290.     }
  291.     else {
  292.         pos = matcher.start();
  293.         lastPosition = pos + find.length();
  294.         area.replaceRange(replace, pos, (pos + find.length()));
  295.     }
  296.     return found;
  297. }
  298.  
  299.  
  300.  
  301. public static void main(String[] args) {
  302.     new Tamara2();
  303.     }
  304. }
Reply