将文件解析为数组,然后搜索文件中的单词,计算文件中的单词

时间:2014-01-11 13:48:55

标签: java

我可以解析文件,我可以读出文件的内容,但我无法在文件中搜索特定的单词或计算文件中的单词数:

以下代码:

public class Manager{



private String[] textData; 
private String path;


public String loadFile (String file_path){
    return path= file_path;

}


public String [] openFile() throws IOException{
    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader (fr);

    int numberOfLines = readLines();
    textData = new String[numberOfLines];

    int i;

    for (i=0; i < numberOfLines; i++) {
    textData[i] = textReader.readLine();
    }
    textReader.close( );
    return textData;
}
    int readLines() throws IOException{
        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader (file_to_read);

        String aLine;
        int numberOfLines = 0;

        while ((aLine =bf.readLine()) !=null){
            numberOfLines++;

        }
        bf.close();

        return numberOfLines;
    }
}
private int findText(String s){ 
        for (int i = 0; i < textData.length; i++){ 
        if (textData[i] != null && textData[i].equals(s)){ 
            return i; 
        }
    }
    return -1;
}

public boolean contains(String s){
    for(int i=0; i<textData.length; i++){
        if(textData[i] !=null && textData[i].equals(s)){
        return true;    
        }
    }
    return false;
}




public int count(){  
    int counter = 0;    
    for (int i = 0; i < textData.length; i++){   
        if (textData[i] != null) counter++;   
    }
    return counter;   

}

}

我的其他班级:

ublic class Runner {

private String fileInput;
private Scanner scanner = new Scanner(System.in); 
private boolean keepRunning= true;
private Manager m = new Manager();

public static void main(String[] args) throws Exception { 
    new Runner(); 
}

public Runner() throws Exception{
    do { 
        System.out.println("--------------------------------------------------");
        System.out.println("\t\t\t\tText Analyser");
        System.out.println("--------------------------------------------------");
        System.out.println("1)Parse a File");
        System.out.println("2)Parse a URL");
        System.out.println("3)Exit");
        System.out.println("Select option [1-3]>");

        String option = scanner.next(); 

        if (option.equals("1")){ 
            parseFile(); 
        }else if(option.equals("2")){ 
            parseUrl();
        }else if(option.equals("3")){ 
            keepRunning = false;    
        }else{
            System.out.println("Please enter option 1-3!");
        }
    } while (keepRunning);

    System.out.println("Bye!");
    scanner.close(); 
    }


private void parseFile()throws Exception{

    String file_name;
    System.out.print("What is the full file path name of the file you would like to parse?\n>>"); ////The user might enter in a path name like: "C:/Users/Freddy/Desktop/catDog.txt";
    file_name = scanner.next();


    try {
        Manager file = new Manager();
        file.loadFile(file_name);
        String[] aryLines = file.openFile( );
        int i;
        for ( i=0; i < aryLines.length; i++ ) {
        System.out.println( aryLines[ i ] ) ;
        }
    }

    catch ( IOException e ) {
        System.out.println( e.getMessage() );
        }

    do { 
        System.out.println("*** Parse a file or URL ***");

        System.out.println("1)Search File");
        System.out.println("2)Print Stats about File");
        System.out.println("3)Exit");
        System.out.println("Select option [1-3]>");

        String option = scanner.next(); 

        if (option.equals("1")){
        }else if(option.equals("2")){ 
        }else if(option.equals("3")){ 
            keepRunning = false;    
        }else{
            System.out.println("Please enter option 1-3!");
        }
    } while (keepRunning);

    System.out.println("Bye!");
    scanner.close(); 
    }


private void parseUrl()throws Exception{

}


private void search() throws Exception{
    do { 
        System.out.println("*** Search ***");

        System.out.println("1)Does the file/URL contain a certain word");
        System.out.println("2)Count all words in the file/url");

        System.out.println("9)Exit");
        System.out.println("Select option [1-9]>");

        String choice = scanner.next(); //Get the selected item

        if (choice.equals("1")){
            contains(); 
        }else if(choice.equals("2")){ 
            count();
        }else if(choice.equals("3")){ 
            keepRunning = false;    
        }else{
            System.out.println("Please enter option 1-3!");
        }
    } while (keepRunning);

    System.out.println("Bye!");
    scanner.close(); 
    }

private void contains(){
    System.out.println("*** Check to see if a certain Word/Letter appears in the file/URL ***");
    System.out.println("Enter what you would like to search for:");
    String s = scanner.next();
    boolean sc = m.contains(s);
    System.out.println("If its true its in the file, if its false its not in the file/URL");
    System.out.println("The answer = " + sc);
}   

private void count(){
   int totalNumberOfElement = m.count();
       System.out.println("Total number of elements in the file/Url is" +  totalNumberOfElement );
}

3 个答案:

答案 0 :(得分:0)

请考虑以下几点来更改您的代码: 1.使用List(例如:List contents = new List)从文件中读取行 2.使用contents.size()获取行数。那很简单。 3.您正在使用equals()方法搜索一行中的文本。使用contains()或indexOf()方法。更好,如果你意识到它,请使用正则表达式。

答案 1 :(得分:0)

我认为最简单的方法如下:

public boolean contains(String s){
    return textData.toLowerCase().contains(s.toLowerCase())

}

但只有goint才能为字符串工作!

答案 2 :(得分:0)

我在下面写了一些代码。看看它是否对你有所帮助。请不要复制粘贴,尝试学习逻辑。

import java.io.*;

public class Manager 
{
String[] textData;
String path;
int numberOfWords;

public void loadFile(String path) throws Exception
{
    this.path = path;
    StringBuffer buffer = new StringBuffer();
    BufferedReader reader = new BufferedReader(new FileReader(path));
    String line;
    String[] words;
    numberOfWords = 0;
    while((line=reader.readLine()) != null)
    {
        buffer.append(line + "\n");
        words = line.split(" ");
        numberOfWords = numberOfWords + words.length;
    }
    //deleting the last extra newline character
    buffer.deleteCharAt(buffer.lastIndexOf("\n"));
    textData = buffer.toString().split("\n");
}

public void printFile()
{
    for(int i=0;i<textData.length;i++)
        System.out.println(textData[i]);
}

public int findText(String text)
{
    for(int i=0;i<textData.length;i++)
        if(textData[i].contains(text))
            return i;
    return -1;
}

public boolean contains(String text)
{
    for(int i=0;i<textData.length;i++)
        if(textData[i].contains(text))
            return true;
    return false;
}

public int getNumberOfWords()
{
    return numberOfWords;
}

public int getCount()
{
    return textData.length;
}   
}