if(e.getSource()==btn1)
         {   
				
				paragraph=txtAreaArticle.getText();
				
				if (paragraph == "\n\n")
				{	
					return 1 && countPara+1;	
					txtField1.setText(countPara);
				}

         }

i wan set count how many paragraph for a article in my textArea. Then the number of paragraph will display on textfield. any one can help me?

Recommended Answers

All 15 Replies

Are you doing bit operations to count? Create a variable called nrParagraphs and increment it, nrParagraphs = nrParagraphs + 1, each time \n\n is read in. Alternatively, you could increment it every time a tab was read in.

Strings in Java cannot be compared using '=='. You need to use the equals method of the String class. So basically

if (paragraph == "\n\n")

convert this to

if (paragraph.equals("\n"))

Also are paragraphs always going to be with two new lines ("\n\n"), if not what you could do is:

if (paragraph.startsWith("\n"))

So that this could handle one or multiple new lines as well ;)

Are you doing bit operations to count? Create a variable called nrParagraphs and increment it, nrParagraphs = nrParagraphs + 1, each time \n\n is read in. Alternatively, you could increment it every time a tab was read in.

if (paragraph.equals("\n"))
				{	
					nrParagraphs = nrParagraphs + 1;	
					txtField1.setText(nrParagraphs);

PartOfSpeech.java:91: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (int)
txtField1.setText(nrParagraphs);

how to fix tis part as well to count ar?

the problem here is setText() method takes a string as an input, and 'nParagraphs' is an int.

Solution: Converting the int to String first.

txtField1.setText(String.valueOf(nrParagraphs));

the problem here is setText() method takes a string as an input, and 'nParagraphs' is an int.

Solution: Converting the int to String first.

txtField1.setText(String.valueOf(nrParagraphs));

if like tat, how i count the number of paragraph from text area?

import javax.swing.*;
   import java.awt.*;
	import java.util.*;
   import java.awt.event.*;
   import java.io.*;

    public class PartOfSpeech extends JFrame implements ActionListener
   {   
      private JButton btn1,btn2;
		private JTextArea txtAreaArticle;
      private JTextField txtField1,txtField2,txtField3;
      private JMenuBar jmb;
      private JMenu fileMenu;
      private JMenuItem out;
   
       public PartOfSpeech()
      {
         setTitle("PART OF SPEECH SYSTEM");
         Container con=getContentPane();
         con.setLayout(new BorderLayout());
      
         jmb=new JMenuBar();
         fileMenu=new JMenu("File");
         out=new JMenuItem("Exit");
         fileMenu.add(out);
         out.addActionListener(this);
         jmb.add(fileMenu);
         setJMenuBar(jmb);
      
         JPanel top=new JPanel();
         con.add(top,BorderLayout.NORTH);
			
			txtAreaArticle = new JTextArea(15,40);
			top.add(txtAreaArticle);
			
			JPanel middle=new JPanel();
         con.add(middle,BorderLayout.CENTER);
         middle.setLayout(new GridLayout(3,6,50,50));
     
         JLabel label1=new JLabel("NUMBER OF PARAGRAPH");
         middle.add(label1);
         txtField1=new JTextField(10);
			txtField1.setEditable(false);
         middle.add(txtField1);
     
         JLabel label2=new JLabel("NUMBER OF SENTENCE");
         middle.add(label2);
         txtField2=new JTextField(10);
         txtField2.setEditable(false);
         middle.add(txtField2);
      
         JLabel label3=new JLabel("NUMBER OF WORD");
         middle.add(label3);
         txtField3=new JTextField(10);
         txtField3.setEditable(false);
         middle.add(txtField3);
            
         JPanel bottom=new JPanel();
         con.add(bottom,BorderLayout.SOUTH);
         bottom.setLayout(new FlowLayout());
      
         btn1=new JButton("Generate");
         bottom.add(btn1);
         btn1.addActionListener(this);
      
         btn2=new JButton("Clear");
         bottom.add(btn2);
         btn2.addActionListener(this);
      
      
         setSize(500,600);
         setVisible(true);
      
      }
   
       public void actionPerformed(ActionEvent e)
      {
		
			String paragraph;
			int nrParagraphs;

         
         if(e.getSource()==btn1)
         {   
				int paragraph = 0;
				paragraph=txtAreaArticle.getText();
				
				paragraph = countLinesIn(paragraph);
				
				txtField1.setText(String.valueOf(nrParagraphs));

         }  
			
         if(e.getSource()==btn2)
         { 
        		txtAreaArticle.setText("");
				txtField1.setText(" ");
            txtField2.setText(" ");
            txtField3.setText(" ");	 
         }
      
         if(e.getSource()==out)
         {
            System.exit(0);
         }
      }  
     
     	 
   	 public static void main (String[]args)
      {
        PartOfSpeech sys=new PartOfSpeech();
         sys.setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
		
      public int countLinesIn(String article)
    {
        int count = 0;
        try
        {
            BufferedReader reader = new BufferedReader(article);
            String line = null;
            do
            {
                line = reader.readLine();
                if (line != null
                    && line.indexOf("*") == -1
                    && line.indexOf("//") == -1
                    && line.length() > 0)
                    count++;
            }
            while (line != null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return count;
    }
  }
PartOfSpeech.java:85: paragraph is already defined in actionPerformed(java.awt.event.ActionEvent)
				int paragraph = 0;
				    ^
PartOfSpeech.java:86: incompatible types
found   : java.lang.String
required: int
				paragraph=txtAreaArticle.getText();
				                                ^
PartOfSpeech.java:88: countLinesIn(java.lang.String) in PartOfSpeech cannot be applied to (int)
				paragraph = countLinesIn(paragraph);
				            ^
PartOfSpeech.java:120: cannot find symbol
symbol  : constructor BufferedReader(java.lang.String)
location: class java.io.BufferedReader
            BufferedReader reader = new BufferedReader(article);
                                    ^
4 errors

i code the system again, can anybody fix the error for me?

I had just suggested to convert the value of the int before setting it as the text of the text field, using String.valueOf() and not to convert the variable.

And for the errors here are the causes:
1. paragraph is already defined

String paragraph;
int paragraph = 0;

Defining the same variable with two different types may make the compiler not know when to use what and go mad eventually. ;)

2. incompatible types

paragraph=txtAreaArticle.getText();

when you say getText be assured that the JVM will return text and not int so please be a good boy and assign what it returns to a string and not int ;)

3. countLinesIn(java.lang.String) in PartOfSpeech cannot be applied to (int)

public int countLinesIn(String article)
...
then
paragraph = countLinesIn(paragraph); ????

This is where you don't trust yourself. You write a method that takes a string as an input and then, surprisingly, put an int param into it, and then, even more surprisingly, assign, what the method returns to the same variable that you gave it as input. What are you doing here please let me know ?

4. cannot find symbol
symbol : constructor BufferedReader(java.lang.String)

BufferedReader reader = new BufferedReader(article);

Hmm.. here the compiler tells you that it cannot find a method in the BufferedReader class that creates a BufferedReader object that takes a String as an input. Here are the docs for the BufferedReader class tell me whether you can find one ?

Here you try to create a BuffereReader object by giving it a string to eat, but a BufferedReader object cannot be created by giving it a string it needs to gulp down a Reader to do so.

Solution, for the first the three errors : Remove this

int paragraph = 0;

Solution for the last error : Read the docs sited above.

import javax.swing.*;
   import java.awt.*;
	import java.util.*;
   import java.awt.event.*;
   import java.io.*;
	import java.io.BufferedReader;


    public class PartOfSpeech extends JFrame implements ActionListener
   {   
      private JButton btn1,btn2;
		private JTextArea txtAreaArticle;
      private JTextField txtField1,txtField2,txtField3;
      private JMenuBar jmb;
      private JMenu fileMenu;
      private JMenuItem out;
   
       public PartOfSpeech()
      {
         setTitle("PART OF SPEECH SYSTEM");
         Container con=getContentPane();
         con.setLayout(new BorderLayout());
      
         jmb=new JMenuBar();
         fileMenu=new JMenu("File");
         out=new JMenuItem("Exit");
         fileMenu.add(out);
         out.addActionListener(this);
         jmb.add(fileMenu);
         setJMenuBar(jmb);
      
         JPanel top=new JPanel();
         con.add(top,BorderLayout.NORTH);
			
			txtAreaArticle = new JTextArea(15,40);
			top.add(txtAreaArticle);
			
			JPanel middle=new JPanel();
         con.add(middle,BorderLayout.CENTER);
         middle.setLayout(new GridLayout(3,6,50,50));
     
         JLabel label1=new JLabel("NUMBER OF PARAGRAPH");
         middle.add(label1);
         txtField1=new JTextField(10);
			txtField1.setEditable(false);
         middle.add(txtField1);
     
         JLabel label2=new JLabel("NUMBER OF SENTENCE");
         middle.add(label2);
         txtField2=new JTextField(10);
         txtField2.setEditable(false);
         middle.add(txtField2);
      
         JLabel label3=new JLabel("NUMBER OF WORD");
         middle.add(label3);
         txtField3=new JTextField(10);
         txtField3.setEditable(false);
         middle.add(txtField3);
            
         JPanel bottom=new JPanel();
         con.add(bottom,BorderLayout.SOUTH);
         bottom.setLayout(new FlowLayout());
      
         btn1=new JButton("Generate");
         bottom.add(btn1);
         btn1.addActionListener(this);
      
         btn2=new JButton("Clear");
         bottom.add(btn2);
         btn2.addActionListener(this);
      
      
         setSize(500,600);
         setVisible(true);
      
      }
   
       public void actionPerformed(ActionEvent e)
      {
			String paragraph;
			int nrParagraphs;

         
         if(e.getSource()==btn1)
         {   
				paragraph=txtAreaArticle.getText();
				
				if (paragraph.equals("\r\n\r\n"))
				{
					nrParagraphs = 1;
				
					txtField1.setText(String.valueOf(nrParagraphs));
				}

         }  
			
         if(e.getSource()==btn2)
         { 
        		txtAreaArticle.setText("");
				txtField1.setText(" ");
            txtField2.setText(" ");
            txtField3.setText(" ");	 
         }
      
         if(e.getSource()==out)
         {
            System.exit(0);
         }
      }  
     
     	 
   	 public static void main (String[]args)
      {
        PartOfSpeech sys=new PartOfSpeech();
        sys.setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
		
  }

Thanks for suggestion, if i wan simplify the system can? coz i jz wan the button can auto calculate the number of paragraph from the input text area. then the number can display at textfield1. Can u help me in tis? Sorry make u dizzy, coz i also blur ady.

What do you mean by auto calculate ?

What do you mean by auto calculate ?

cant said is auto calculate, but is when i press the button, then will detect how many paragraph tat in text area and show the number of paragraph in text field.

Then you need to write code in your actionPerformed method that will detect when the button is clicked, get the text from wherever the paragraphs are, and read through it, counting the number of times you see '\n\n' (or however you decide to count paragraphs). After you do all of that, use the setText method on your textField to display the number of paragraphs you saw.

edit: I just checked your code and it should already do all of that. You need to write some error code to make sure that

1. It's already going into that if statement (the right button was clicked & the code that I described above executed)
2. Your method to count paragraphs actually works correctly

etc

I think what he wants is that it should happen not on the click of the button but as the user types into the textarea. Meaning as soon as the user enters a new line in the text, he should see the paragraph count incremented. If this is what you need you will have to catch the keystrokes as the user types and look for the keymap of the enter key. For this you will need to implement the KeyListener interface. Check the APi for the same on the link given for details. But if this is not what you are mentioning, detail out what you need to achieve.

I think what he wants is that it should happen not on the click of the button but as the user types into the textarea. Meaning as soon as the user enters a new line in the text, he should see the paragraph count incremented. If this is what you need you will have to catch the keystrokes as the user types and look for the keymap of the enter key. For this you will need to implement the KeyListener interface. Check the APi for the same on the link given for details. But if this is not what you are mentioning, detail out what you need to achieve.

tat is wat i mean. u understand wat i said. so hope any can help me out in detail.

Read the tutorials you have been pointed towards, make an effort to apply them, and then post questions if you run into difficulty. Don't expect people to post detailed examples of every single thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.