在.txt文件中搜索字符串并获取Java中的行和列编号
我现在遇到了一个问题。我应该写一个程序,能够搜索作为参数给出的.txt文件中的字符串。程序必须返回找到的字符串的行和列。 我正在努力寻找一种方法来实现这一点,但我不知道如何继续下去。我很高兴收到你的来信 以下是我在处理我的任务时的尝试: -我曾考虑过通过字符串数组中的缓冲读取器保存文件内容,但这似乎不起作用,因为我无法从一开始就定义数组的长度 -我还考虑通过缓冲读取器将文件内容保存为字符串,然后将该字符串拆分为字符。但是,我不确定如何才能检索原始文件中的行 这是我目前拥有的非功能性代码:在.txt文件中搜索字符串并获取Java中的行和列编号,java,arrays,string,search,java-io,Java,Arrays,String,Search,Java Io,我现在遇到了一个问题。我应该写一个程序,能够搜索作为参数给出的.txt文件中的字符串。程序必须返回找到的字符串的行和列。 我正在努力寻找一种方法来实现这一点,但我不知道如何继续下去。我很高兴收到你的来信 以下是我在处理我的任务时的尝试: -我曾考虑过通过字符串数组中的缓冲读取器保存文件内容,但这似乎不起作用,因为我无法从一开始就定义数组的长度 -我还考虑通过缓冲读取器将文件内容保存为字符串,然后将该字符串拆分为字符。但是,我不确定如何才能检索原始文件中的行 这是我目前拥有的非功能性代码: pub
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();
}
}
}
}
}
}
公共类字符串搜索{
公共静态void main(字符串[]args){
如果(args.length>0){
BufferedReader br=null;
字符串文本=空;
试一试{
br=新的BufferedReader(新的文件读取器(args[0]);
//尝试将“参数”文件的内容保存在字符串数组中,然后保存在字符串中
String[]lines=new String[];//我知道它不是这样工作的
对于(int i=0;i
这里有一种方法-
让我们考虑一个计数器,它为所有的计数器保存一个计数器。
readLine()
方法调用-表示.txt中的“行”
文件因此,在
while循环
您可以按如下方式进行操作:
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]);
}
}
}
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.
input.txt
的内容如下:
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]);
}
}
}
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.
代码中的逻辑是直截了当的,我相信,您应该能够在第一次阅读时理解它。如果有任何疑问,请随时发表评论。非常感谢您的回答。我还不清楚我应该先对课文做些什么。我应该读取行并将它们保存到字符串或字符串数组中吗?什么是可取的?您可以动态解析文本。并且仅在找到匹配项时存储“行”和“列”值。如果有多个匹配项,您可以使用相关的数据结构来存储这些数据。非常感谢您的输入和全面的帮助!选择HashMap来存储行和列的编号是很巧妙的,但我不明白它是如何工作的。HashMap是为字符串声明的,而rowCount和index是整数,对吗?你能解释一下吗?提前多谢!我只在地图中存储了字符串,例如,第一个条目是
行:1=列:51
,其中行:1
是键,列:51
是值;它们都是字符串。如果只想存储数字(行号和列号),可以将映射声明为map lineColMap=newhashmap()然后可以使用lineColMap.put(行数,索引)
将值存储到地图中。如果有任何进一步的疑问,请随时发表评论。这是有道理的。再次感谢!最后一个关于int rowCount的问题。您将其初始化为int rowCount=1,index;这是否意味着它有两个价值观?非常欢迎<代码>整数行计数=1,索引
是在一条语句中声明两个或多个变量的快捷方式。您还可以将其编写为int rowCount=1;整数指数代码>作为两个单独的语句。在这两种方式中的任何一种方式中,rowCount
将用1
初始化,index
将用0
初始化(这是int
变量的默认值)。如有任何疑问,请随时发表评论。