シャクナゲ:
私は、現在の問題で立ち往生しています。私は、引数として与えられた.txtファイル内の文字列を検索することができますプログラムの開発を書くことになっています。プログラムの開発は、行、見つかった文字列の列を返さなければなりません。私はそれを達成し、上に行くためにどのよう見当がつかないする方法を見つけるのに苦労しています。私はあなたから聞いて非常に幸せになります。
ここに私の仕事に取り組んで私の試みです: - 私は、文字列配列にバッファリーダーを経由してファイルの内容を保存することについて考え、私は最初から、配列の長さを定義することはできませんので、それが動作するようには思えない - 私は、また、文字で、この文字列を分割、文字列にバッファリングリーダー経由でファイルの内容を保存して考えました。しかし、私はその後、元のファイル内の行をretreieveすることができるようになりますかわかりません。
これは私が現時点で持っていることを非機能コードです。
public class StringSearch{
public static void main(String[] args){
if(args.length > 0){
BufferedReader br = null;
String text = null;
try{
br = new BufferedReader(new FileReader(args[0]));
// attempt of saving the content of the "argument" file in a string array and then in a string
String[] lines = new String[]; // I know this does not work like this
for( int i = 0; i < lines.length; i++){
lines[i] = br.readLine;
text = text + lines[i];
i++;
}
text.split("\r\n");
} catch (IOException ioe){
ioe.printStackTrace();
} finally{
if (br != null) {
try{
br.close();
}catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}
}
}
アービンド・クマールのAvinash:
次のようにあなたはそれを行うことができます。
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("The correct syntax to use this program is: java Main <filename.txt> <text-to-search>");
return;
}
Scanner scanner;
File file = new File(args[0]);
int rowCount = 1, index;
String line;
// Map to collect row and col info of the search string
Map<String, String> lineColMap = new HashMap<String, String>();
if (!file.exists()) {
System.out.println("The file, " + args[0] + " does not exist");
return;
}
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {// Loop until the last line in the file
line = scanner.nextLine();// Read a line from the file
index = line.indexOf(args[1]);// Find if the string exists in the line
if (index != -1) {// If the string exists
// Put the row and col info of the search string into the map
lineColMap.put("Row: " + rowCount, "Column: " + index);
}
rowCount++;// Increase the row count
}
} catch (Exception e) {
System.out.println("Error occured while processing the file");
e.printStackTrace();
}
if (lineColMap.entrySet().size() > 0) {// If there is at least one entry collected into the map
System.out.println("'" + args[1] + "' exists in " + args[0] + " as follows:");
for (Map.Entry<String, String> entry : lineColMap.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
} else {
System.out.println("'" + args[1] + "' does not exist in " + args[0]);
}
}
}
サンプルを実行します。 java Main input.txt of
'of' exists in input.txt as follows:
Row: 1, Column: 51
Row: 2, Column: 50
Row: 3, Column: 50
Row: 5, Column: 71
内容はinput.txt
次のとおりです。
Stack Overflow is a question and answer site for professional and enthusiast programmers.
It is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky.
It features questions and answers on a wide range of topics in computer programming.
It was created to be a more open alternative to earlier question and answer sites such as Experts-Exchange.
The name for the website was chosen by voting in April 2008 by readers of Coding Horror, Atwood's popular programming blog.
コードのロジックは単純であり、私は、あなたが最初の読み取りにそれを理解することができるはずと信じています。任意の疑いの場合にはコメントをお気軽に。