ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの検索プログラム

  • mixiチェック
  • このエントリーをはてなブックマークに追加
以下のプログラムは全文一致検索プログラムなのですが、
このプログラムを部分一致検索にするにはどうすれば
いいのでしょうか?

// TextFileHandle2.java
import java.io.*;
import java.util.*;;
public class TextFileHandle2{//<1
//****************************************
static String keyExtract(String line){//<2
int idx = line.lastIndexOf('/');
if(idx == -1){//<3
return null;
}else{//3><3
return line.substring(idx + 1);
}//3>
}//2>
//***********************************
static int putLinesToHashtable
(String inFileName, Hashtable<String, LinkedList<String>> ht){//<2
int putCount = 0;
try{//<3
BufferedReader br1 =
new BufferedReader(new FileReader(inFileName));
while(true){//<4
String line = br1.readLine();
if(line == null){//<5
break;
}else{//5><5
String key = keyExtract(line);
if(key != null){//<6
LinkedList<String> orgVal = ht.get(key);
if(orgVal != null){//<7
orgVal.add(line);
ht.put(key, orgVal);
putCount++;
}else{//7><7
LinkedList<String> newVal = new LinkedList<String>();
newVal.add(line);
ht.put(key, newVal);
putCount++;
}//7>
}//6>
}//5>
}//4>
br1.close();
}catch(IOException e){//3><3
System.out.println("Catching " + e);
}//3>
return putCount;
}//2>
//**********************
public static void main(String args[]){//<2
if(!(args.length >= 1)){//<3
System.out.println("At least one command-line arg is required.");
System.exit(1);
}//3>
Hashtable<String, LinkedList<String>> ht1
= new Hashtable<String, LinkedList<String>>();
int count = putLinesToHashtable(args[0], ht1);
System.out.println("count = " + count);
BufferedReader rd =
new BufferedReader(new InputStreamReader(System.in));
while(true){//<3
try{//<4. Input the class name to be searched.
System.out.print( "What class? (for quit: just type `return'):");
String className = rd.readLine();
if(className.length() == 0){//<5. For just return.
break;
}//5>
String key = className + ".class";// Search the entry.
LinkedList<String> entries = ht1.get(key);


if(entries != null){//<5. When found, print it.
System.out.println("** Number of entries = " + entries.size());
System.out.println(entries);
}else{//5><5. Otherwise, just say so.
System.out.println( className +" is not found.");
}//5>
}catch(IOException e){//4><4
System.out.println( "Catching " + e);
return;
}//4>
}//3>
}//2>
}//1>

分かる方いましたらよろしくお願いします。

コメント(2)

コード読んでみたところ、コマンドライン引数でもらったファイルを開いて、その中の情報を格納してるみたいですが、どんなファイルを想定してるんだろう…

部分一致にしても、正確にどんな動作にしたいのかわからないんだよなぁ……
といいつつ、適当に書いた。 動かしてみてないから動くかどうかわからないし、動いたとしても、要求通りの動作であることは保証しません。
Hashtableなんて、久々につかったわ。


import java.io.*;
import java.util.*;

public class TextFileHandle2 {// <1
// ****************************************
static String keyExtract(String line) {// <2
int idx = line.lastIndexOf('/');
if (idx == -1) {// <3
return null;
} else {// 3><3
return line.substring(idx + 1);
}// 3>
}// 2>
// ***********************************

static int putLinesToHashtable(String inFileName, Hashtable<String, LinkedList<String>> ht) {// <2
int putCount = 0;
try {// <3
BufferedReader br1 = new BufferedReader(new FileReader(inFileName));
while (true) {// <4
String line = br1.readLine();
if (line == null) {// <5
break;
} else {// 5><5
String key = keyExtract(line);
if (key != null) {// <6
LinkedList<String> orgVal = ht.get(key);
if (orgVal != null) {// <7
orgVal.add(line);
ht.put(key, orgVal);
putCount++;
} else {// 7><7
LinkedList<String> newVal = new LinkedList<String>();
newVal.add(line);
ht.put(key, newVal);
putCount++;
}// 7>
}// 6>
}// 5>
}// 4>
br1.close();
} catch (IOException e) {// 3><3
System.out.println("Catching " + e);
}// 3>
return putCount;
}// 2>
// **********************

public static LinkedList<String> findEntry(Hashtable<String, LinkedList<String>> aTable, String aSaerchStr){
Enumeration<String> tKeys = aTable.keys();

while(tKeys.hasMoreElements()){
String tKey = tKeys.nextElement();
if(tKey.indexOf(aSaerchStr) != -1)
return aTable.get(tKey);
}

return null;
}

public static void main(String args[]) {// <2
if (!(args.length >= 1)) {// <3
System.out.println("At least one command-line arg is required.");
System.exit(1);
}// 3>
Hashtable<String, LinkedList<String>> ht1 = new Hashtable<String, LinkedList<String>>();
int count = putLinesToHashtable(args[0], ht1);
System.out.println("count = " + count);
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
while (true) {// <3
try {// <4. Input the class name to be searched.
System.out.print("What class? (for quit: just type `return'):");
String className = rd.readLine();
if (className.length() == 0) {// <5. For just return.
break;
}// 5>
String key = className + ".class";// Search the entry.
LinkedList<String> entries = findEntry(ht1, key);

if (entries != null) {// <5. When found, print it.
System.out.println("** Number of entries = " + entries.size());
System.out.println(entries);
} else {// 5><5. Otherwise, just say so.
System.out.println(className + " is not found.");
}// 5>
} catch (IOException e) {// 4><4
System.out.println("Catching " + e);
return;
}// 4>
}// 3>
}// 2>
}// 1>
> seraph さん
ありがとうございます。
部分一致の動作は

rt.jar.contents.txt

META-INF/
META-INF/MANIFEST.MF
META-INF/services/javax.print.PrintServiceLookup
META-INF/services/javax.print.StreamPrintServiceFactory
META-INF/services/javax.sound.midi.spi.MidiDeviceProvider
META-INF/services/javax.sound.midi.spi.MidiFileReader
META-INF/services/javax.sound.midi.spi.MidiFileWriter
META-INF/services/javax.sound.midi.spi.SoundbankReader
META-INF/services/javax.sound.sampled.spi.AudioFileReader
META-INF/services/javax.sound.sampled.spi.AudioFileWriter
META-INF/services/javax.sound.sampled.spi.FormatConversionProvider
META-INF/services/javax.sound.sampled.spi.MixerProvider
com/sun/accessibility/internal/resources/accessibility.class
com/sun/accessibility/internal/resources/accessibility_de.class
com/sun/accessibility/internal/resources/accessibility_en.class
com/sun/accessibility/internal/resources/accessibility_es.class
以下省略

というテキストファイルからデータを読み取り
文字列を入力したときそれを含む全ての行を標準出力に
書き出すというものです。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング