1,105,391 Community Members

Read Lines

Member Avatar
mehnihma
Posting Whiz in Training
239 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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");
               }
         	
         	
         }
      }
   }
Member Avatar
stultuske
Posting Expert
5,308 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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
mehnihma
Posting Whiz in Training
239 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
mehnihma
Posting Whiz in Training
239 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
Member Avatar
stultuske
Posting Expert
5,308 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
mehnihma
Posting Whiz in Training
239 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I need to enter a line in a gui

Member Avatar
stultuske
Posting Expert
5,308 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

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.

Question Answered as of 2 Years Ago by stultuske
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article