Member Avatar for mehnihma

I have problems with choosing a line and to find a number or times specifif word repeats in that line?
Can you help me with that?

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

public class DefectInspector 
{
	private JFrame frame = new JFrame();
	private JPanel p1,p2,p3,p4;
	private JMenuBar jmb;
	private JMenu jmFile;
	private JMenuItem jmiOpen, jmiExit;
	private JLabel jlRow;
	private JTextField jtfRow;
	private JTextArea area;
	private JButton jbInspect;
	private JScrollPane jsp;
	
	public DefectInspector()
	{
		jmb = new JMenuBar();
		frame.setJMenuBar(jmb);
		
		jmFile = new JMenu("File");
		jmb.add(jmFile);
		
		jmiOpen = new JMenuItem("Open");
		jmiExit = new JMenuItem("Exit");
		jmFile.add(jmiOpen);
		jmFile.add(jmiExit);
		
		p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		
		jlRow = new JLabel("Row: ");
		jtfRow = new JTextField(15);
		jtfRow.setEditable(false);
		p1.add(jlRow);
		p1.add(jtfRow);
		
		p2 = new JPanel();
		p2.setLayout(new BorderLayout());
		
		area = new JTextArea(0,0);
		area.setEditable(false);
		area.setLineWrap(true);
		area.setWrapStyleWord(true);
		jsp = new JScrollPane(area);
		
		p2.add(jsp, "Center");
		
		p3 = new JPanel();
		p3.setLayout(new FlowLayout());
		
		jbInspect = new JButton("Inspect");
		p3.add(jbInspect);
		
		p4 = new JPanel();
		p4.setLayout(new BorderLayout());
		p4.add(p1, "North");
		p4.add(p2, "Center");
		p4.add(p3, "South");
		
		frame.add(p4);
		
		DefectListener listener = new DefectListener(jmiOpen, jmiExit, jtfRow, area, jbInspect);
		
		jmiOpen.addActionListener(listener);
		jmiExit.addActionListener(listener);
		jtfRow.addActionListener(listener);
		jbInspect.addActionListener(listener);
		
		frame.setTitle("Defect Inspector");
		frame.setSize(300,300);
		frame.setLocation(200,200);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
	}
	
	public static void main(String[]args)
	{
		new DefectInspector();
	}
}
import java.awt.event.*;
   import java.awt.*;
   import javax.swing.*;
   import java.io.*;
   import java.util.*;

   public class DefectListener implements ActionListener
   {
      private JMenuItem jmiOpen, jmiExit;
      private JTextField jtfRow;
      private JTextArea area;
      private JButton jbInspect;
      private int numberRows = 0;
      private JFrame frame;
      //private File file = new File("data.txt");;
      private LineNumberReader lnr = null; 
   	
   	//private file = new File("student.csv");
   	
   	// file readers
		
		
		//private FileReader fr = new FileReader("data.txt");
		
	//	private BufferedReader in = new BufferedReader(fr);

	//	private ArrayList <String> lines = new ArrayList<String>();
	
   	
       
   
      public DefectListener(JMenuItem jmiOpen, JMenuItem jmiExit, JTextField jtfRow, JTextArea area, JButton jbInspect)
      {
         this.jmiOpen = jmiOpen;
         this.jmiExit = jmiExit;
         this.jtfRow = jtfRow;
         this.area = area;
         this.jbInspect = jbInspect;
      
      }
   
      public void actionPerformed(ActionEvent e)
      {
         if(e.getSource() == jmiOpen)
         {
            try{
				FileReader fr = new FileReader("data.txt");
				BufferedReader in = new BufferedReader(fr);

                       String line = null;
					
            
               while((line = in.readLine()) != null)
               {
                	 area.append(line +"\n");
						//lines.add(line);
               }
            				
               in.close();
            
               area.setEditable(true);
               jtfRow.setEditable(true);
            
            }
            
            
               catch(FileNotFoundException fnf)
               {
                  JOptionPane.showMessageDialog(null, "File not found");
               }
               catch(IOException ioe)
               {
                  System.out.println("IO error detected");
               }
         }
         else if(e.getSource() == jmiExit)
         {
            System.exit(1);
         }
         else if(e.getSource() == jbInspect)
         {
         
            try{
               String value = jtfRow.getText();
               numberRows = Integer.parseInt(value);
					
					String linN = area.getText();
					
					//String selectedLine = get
            
              //lnr = new LineNumberReader(fr);
            
            //area.setLineNumber(numberRows);
            
            
            
            
            
               JOptionPane.showMessageDialog(null, "The number is: ","about",JOptionPane.INFORMATION_MESSAGE);
            }
            
            
               catch (NumberFormatException n)
               
               {
                  System.out.println("BUUUU");
               }
         	
         	
         }
      }
   }

what is the actual problem, and what error message do you get?

btw:

catch (NumberFormatException n)
 
               {
                  System.out.println("BUUUU");
               }

this will do no good at all, replace it (everywhere you have a structure like this) by

catch (NumberFormatException n)
 
               {
                  n.printStackTrace();
               }
Member Avatar for mehnihma

the problem is that I do not know how to choose that specific line and count a word defect in that line of text

Member Avatar for mehnihma

this part i the problem

else if(e.getSource() == jbInspect)
{
 
try{
String value = jtfRow.getText();
numberRows = Integer.parseInt(value);
 
String linN = area.getText();
 
//String selectedLine = get
 
//lnr = new LineNumberReader(fr);
 
//area.setLineNumber(numberRows);

how are you supposed to choose a line? the #th line in a file? line with a certain index?

Member Avatar for mehnihma

I need to enter a line in a gui

so, you read in an int, and you want to read the line on that number spot?
read your file line by line, and keep a counter, which you add 1 to each time you read a new line. if your counter is the same as your entered value, you've got your line.

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.