Results 1 to 4 of 4
  1. #1
    Ludwoscht is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Exclamation Search in a txt file and find how often a special string exists

    Hello,

    Iam new in this forum and also new in Java

    I want to find out how often a special string exists in a logfile.
    At the moment the Program shows me in which line the search sting exists but i need to know how often it exists.
    Any ideas how that works?
    Here is the Code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    /*
    * this program searches for a string in a text file and
    * says which line it found the string on
    */
    
    public class SearchText implements ActionListener {
    
    private String filename = "Logfile.txt";
    
    private JFrame frame;
    private JTextField searchField;
    private JButton searchButton;
    private JLabel lineLabel;
    private String searchFor;
    private BufferedReader in;
    
    public SearchText() {
    frame = new JFrame("SearchText");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    searchField = new JTextField(10);
    searchButton = new JButton("Search");
    // this is used later in our actionPerformed method
    searchButton.setActionCommand("search");
    // this sets the action listener for searchButton, which is the current class
    // because this class implements ActionListener
    searchButton.addActionListener(this);
    lineLabel = new JLabel("SearchText");
    }
    
    public void createGUI() {
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
    JPanel bottomPanel = new JPanel();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    topPanel.add(searchField);
    topPanel.add(searchButton);
    bottomPanel.add(lineLabel);
    mainPanel.add(topPanel);
    mainPanel.add(bottomPanel);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
    // now we get the action command and if it is search, then it is the button
    if ("search".equals(e.getActionCommand())) {
    searchFor = searchField.getText();
    searchTheText();
    }
    }
    
    private void searchTheText() {
    // I initialize the buffered reader here so that every time the user searches
    // then the reader will start at the beginning, instead of where it left off last time
    try {
    in = new BufferedReader(new FileReader(new File(filename)));
    } catch (IOException e) {
    }
    String lineContent = null;
    int currentLine = 0;
    // this will be set to true if the string was found
    boolean foundString = false;
    while (true) {
    currentLine++;
    // get a line of text from the file
    try {
    lineContent = in.readLine();
    } catch (IOException e) {
    break;
    }
    // checks to see if the file ended (in.readLine() returns null if the end is reached)
    if (lineContent == null) {
    break;
    }
    if (lineContent.indexOf(searchFor) == -1) {
    continue;
    } else {
    lineLabel.setText(String.valueOf(currentLine));
    foundString = true;
    break;
    }
    }
    if (!foundString)
    lineLabel.setText("Could not find string");
    try {
    in.close();
    } catch (IOException ioe) {
    }
    }
    
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new SearchText().createGUI();
    }
    });
    }
    
    }
    Thanks a lot! :)
    Last edited by JosAH; 01-11-2012 at 09:37 AM. Reason: added [code] ... [/code] tags (but it didn't help much)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Search in a txt file and find how often a special string exists

    If you use regex (Pattern and Matcher classes), you can give it the entire file and just ask it how many matches there are! Lookup a java regex tutorial :D

  3. #3
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Search in a txt file and find how often a special string exists

    What is the special String that you are looking for?
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  4. #4
    Ludwoscht is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Search in a txt file and find how often a special string exists

    Thanks for your answers ;)
    I solved the exercise with PHP but in the future i will learn more about java :)
    greetings Ludwoscht

Similar Threads

  1. Replies: 0
    Last Post: 12-21-2011, 09:15 AM
  2. Check if file exists before download
    By dman in forum Networking
    Replies: 0
    Last Post: 04-28-2011, 03:57 PM
  3. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 02:43 AM
  4. Replies: 2
    Last Post: 04-07-2009, 06:25 AM
  5. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:13 AM

Posting Permissions

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