2014-04-15 42 views
-1

我在Python中编程,但现在我想在Java中执行相同的代码。你能帮我吗?这是我在在文本文件中搜索字符串

import random 
import re 

a = "y" 

while a == "y": 
i = input('Search: ') 
b = i.lower() 
word2 = "" 
for letter in b: 
    lista = [] 
    with open('d:\lista.txt', 'r') as inF: 
    for item in inF: 
    if item.startswith(letter): 
     lista.append(item) 
    word = random.choice(lista) 
    word2 = word2 + word 

print(word2) 

a = input("Again? ") 

工作现在我想这样做Java的,但林真的不知道如何做到这一点的代码。它不是那么容易。我只是一个初学者。到目前为止,我创建了一个代码,使得在文本文件中搜索,但我卡住了。

这是java代码。它找到这个词的位置。我一直在试图修改它而没有我期待的结果。

import java.io.*; 
import java.util.Scanner; 
class test { 

public static void main(String[] args){ 

    Scanner input = new Scanner(System.in); 
    System.out.println("Search: "); 
    String searchText = input.nextLine(); 
    String fileName = "lista.txt"; 
    StringBuilder sb = new StringBuilder(); 

    try { 

     BufferedReader reader = new BufferedReader(new FileReader(fileName)); 


     while (reader.ready()) { 

      sb.append(reader.readLine()); 
     } 

    } 
    catch(IOException ex) { 
     ex.printStackTrace(); 
    } 

    String fileText = sb.toString(); 
    System.out.println("Position in file : " + fileText.indexOf(searchText)); 

} 
} 

我希望的是找到一个文本文件,列表中的内容,但只是想表明,我想搜索的字符串的字母开头的项目。例如,我有字符串“紧急”和文本文件包含: 宝宝 雷德曼 爱 城市 绅士 游戏 大象 晚上 托德 所以显示会“城市” +“雷德曼” +“绅士“+直到达到字符串的末尾。

+2

安置自己的Java代码,到目前为止,以及问题特别是。 – RossC

+1

您的Java代码正在为我工​​作,那么问题是什么?如果你只是告诉我你想要什么,我可以给你一个答案。 – CodeCamper

回答

1

假设您已经对字符串进行了标记,以便您有一个字符串列表,每个字符串都包含一个单词。如果你每行有一个单词,这就是你的Python代码的编写方式。现在

String[] haystack = {"baby", "redman", "love", "urban", "gentleman", "game", 
    "elephant", "night", "todd"}; 

,寻找一根针,你可以简单的草垛的第一个字符比较针的所有字符:

String needle = "urgent"; 

for (String s : haystack) { 
    for (int i = 0; i < needle.length(); ++i) { 
     if (s.charAt(0) == needle.charAt(i)) { 
      System.out.println(s); 
      break; 
     } 
    } 
} 

该解决方案运行在O(|针| * |草垛|)。 为了提高它一下了额外的内存一点点的成本,我们可以预先计算的哈希表可用的启动:

String needle = "urgent"; 
Set<Character> lookup = new HashSet<Character>(); 

for (int i = 0; i < needle.length(); ++i) { 
     lookup.add(needle.charAt(i));   
} 

for (String s : haystack) { 
    if (lookup.contains(s.charAt(0))) { 
     System.out.println(s); 
    } 
} 

第二种解决方案运行在O(|针| + |草堆|) 。

0

如果您的单词列表不太大,则可以使用。如果你的单词列表很大,你可以调整它,这样你就可以多次收集文件并使用文件。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Random; 


public class Test { 

    public static void main(String[] args) { 
     Map<Character, List<String>> map = new HashMap<Character, List<String>>(); 
     File file = new File("./lista.txt"); 
     BufferedReader reader = null; 
     try { 
      reader = new BufferedReader(new FileReader(file)); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       // assumes words are space separated with no 
       // quotes or commas 
       String[] tokens = line.split(" "); 

       for(String word : tokens) { 
        if(word.length() == 0) continue; 

        // might as well avoid case issues 
        word = word.toLowerCase(); 

        Character firstLetter = Character.valueOf(word.charAt(0)); 

        List<String> wordsThatStartWith = map.get(firstLetter); 
        if(wordsThatStartWith == null) { 
         wordsThatStartWith = new ArrayList<String>(); 
         map.put(firstLetter, wordsThatStartWith); 
        } 

        wordsThatStartWith.add(word); 
       } 

      } 

      Random rand = new Random(); 
      String test = "urgent"; 

      List<String> words = new ArrayList<String>(); 
      for (int i = 0; i < test.length(); i++) { 
       Character key = Character.valueOf(test.charAt(i)); 
       List<String> wordsThatStartWith = map.get(key); 
       if(wordsThatStartWith != null){ 
        String randomWord = wordsThatStartWith.get(rand.nextInt(wordsThatStartWith.size())); 
        words.add(randomWord); 
       } else { 
        // text file didn't contain any words that start 
        // with this letter, need to handle 
       } 
      } 

      for(String w : words) { 
       System.out.println(w); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if(reader != null) { 
       try { 
        reader.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

这是假设lista.txt的内容看起来像

baby redman love urban gentleman game elephant night todd 

和输出看起来像

urban 
redman 
gentleman 
elephant 
night 
todd