Java 将txt文件拆分为数组并查找数组中的元素

Java 将txt文件拆分为数组并查找数组中的元素,java,Java,您好,基本上我的目标是读取txt文件,将其存储在数组中,并在方法参数之后打印数组元素。文本文件的格式如图所示(一行中每个字符串之间的空格) e、 g.如果我的参数是lee,则该方法应打印出20。如果rodney那么28 public class practice { public void dataReader(String fName, String pname) { try { FileReader fr=new FileReader(fName);

您好,基本上我的目标是读取
txt
文件,将其存储在数组中,并在方法参数之后打印数组元素。文本文件的格式如图所示(一行中每个字符串之间的空格)

e、 g.如果我的参数是
lee
,则该方法应打印出
20
。如果
rodney
那么
28

public class practice
{
public void dataReader(String fName, String pname)
{
    try
    {
      FileReader fr=new FileReader(fName);
      BufferedReader br=new BufferedReader(fr);

      String[] a={};
      String line= br.readLine();


      while(line !=null)
      {
           a= line.split(" "); // deliminator white space
      }

      for(int i=0; i <a.length; i++)
      {
          if(a[i].equals(pname))
          {
              System.out.println(a[i]+1);
          }
      }
    }

    catch(IOException e)
    {
    }
}
公共课堂实践
{
公共void数据读取器(字符串fName,字符串pname)
{
尝试
{
FileReader fr=新的FileReader(fName);
BufferedReader br=新的BufferedReader(fr);
字符串[]a={};
String line=br.readLine();
while(行!=null)
{
a=行。拆分(“”;//清除器空白
}

对于(inti=0;i您应该使用Dictionary对象

Dictionary<String, Integer> wordPairs = new Dictionary<String, Integer>();
while(br.ReadLine())
{
    wordPairs.put(a[0], Integer.parseInt(a[1]));
}

应该使用Dictionary对象

Dictionary<String, Integer> wordPairs = new Dictionary<String, Integer>();
while(br.ReadLine())
{
    wordPairs.put(a[0], Integer.parseInt(a[1]));
}

您发布的代码不起作用,因为您只读取了第一行,然后一直循环该行

您的代码,经过修剪和注释:

String line= br.readLine(); // returns the first line of the file

while(line !=null) { // checks if the line is null - it's not at the first line
    a= line.split(" "); // deliminator white space
}
// we never get here, because nowhere in the loop do we set line to null
您需要在一个循环中调用
br.readLine()
,直到它返回null,如下所示:

BufferedReader br=new BufferedReader(new FileReader(fName));

String line= br.readLine(); // reads the first line, or nothing

while(line != null) {
    a= line.split(" "); // deliminator white space
    String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
    if(arr.length >= 2 && arr[0].equals(lookup)) {
        System.out.println(arr[1]);
    }

    line = br.readLine(); // this will eventually set line to null, terminating the loop
}
原始代码中的for循环将不起作用,如果您点击它,您的输出将分别为
lee1
rodney1
。如果您将其更改为
arr[i+1]
相反,如果数组中的最后一个项与
pname
匹配,则它将因IndexOutOfBoundsException崩溃,我假设您正在尝试这样做


原始答案

这是一个理想的用例。它“扫描”字符串或文件以查找您要查找的内容,大大简化了许多用例的文件解析,尤其是以空格分隔的文件

public void searchFile(String fName, String lookup){
  Scanner in = new Scanner(new File(fName));
  // Assumes the file has two "words" per line
  while(in.hasNext()){
    String name = in.next();
    String number = in.next():
    if(name.equals(lookup){
      System.out.println(number);
    }
  }
}
如果不能使用scanner解析每一行,您仍然可以使用scanner简化每一行的读取,然后执行需要对该行执行的任何更复杂的解析,如:

public void searchFile2(String fName, String lookup){
  Scanner in = new Scanner(new File(fName));
  while(in.hasNextLine()){
    String line = in.nextLine();
    String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
    if(arr[0].equals(lookup)){
      System.out.println(arr[1]);
    }
  }
}

另一方面,如果您知道名称是唯一的,那么可以使用a(特别是a)为了高效地存储和查找像名称到数字这样的映射。因此,您可以使用一个方法来解析文件并返回所有名称到数字的映射,而不是使用一个方法来查找文件名和名称,然后您可以简单地调用
map.get(“name”)
可以在返回的地图上高效地获取给定的个人号码,而无需每次都重新读取文件。

您发布的代码无效,因为您只读取了第一行,然后一直循环该行

您的代码,经过修剪和注释:

String line= br.readLine(); // returns the first line of the file

while(line !=null) { // checks if the line is null - it's not at the first line
    a= line.split(" "); // deliminator white space
}
// we never get here, because nowhere in the loop do we set line to null
您需要在一个循环中调用
br.readLine()
,直到它返回null,如下所示:

BufferedReader br=new BufferedReader(new FileReader(fName));

String line= br.readLine(); // reads the first line, or nothing

while(line != null) {
    a= line.split(" "); // deliminator white space
    String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
    if(arr.length >= 2 && arr[0].equals(lookup)) {
        System.out.println(arr[1]);
    }

    line = br.readLine(); // this will eventually set line to null, terminating the loop
}
原始代码中的for循环将不起作用,如果您点击它,您的输出将分别为
lee1
rodney1
。如果您将其更改为
arr[i+1]
相反,如果数组中的最后一个项与
pname
匹配,则它将因IndexOutOfBoundsException崩溃,我假设您正在尝试这样做


原始答案

这是一个理想的用例。它“扫描”字符串或文件以查找您要查找的内容,大大简化了许多用例的文件解析,尤其是以空格分隔的文件

public void searchFile(String fName, String lookup){
  Scanner in = new Scanner(new File(fName));
  // Assumes the file has two "words" per line
  while(in.hasNext()){
    String name = in.next();
    String number = in.next():
    if(name.equals(lookup){
      System.out.println(number);
    }
  }
}
如果不能使用scanner解析每一行,您仍然可以使用scanner简化每一行的读取,然后执行需要对该行执行的任何更复杂的解析,如:

public void searchFile2(String fName, String lookup){
  Scanner in = new Scanner(new File(fName));
  while(in.hasNextLine()){
    String line = in.nextLine();
    String[] arr = line.split("\\s+"); // splits a string by any amount of whitespace
    if(arr[0].equals(lookup)){
      System.out.println(arr[1]);
    }
  }
}

另一方面,如果您知道名称是唯一的,那么可以使用a(特别是a)为了高效地存储和查找像名称到数字这样的映射。因此,您可以使用一个方法来解析文件并返回所有名称到数字的映射,而不是使用一个方法来查找文件名和名称,然后您可以简单地调用
map.get(“name”)
在返回的地图上有效地获取给定的人号,而无需每次重新读取文件。

使用格式,Java类以大写字母开头(约定),如果这是作业,则应将其标记为大写字母。使用格式,Java类以大写字母开头(约定)如果这是作业,则应将其标记为此类。任何一个映射类型集合都可以用于此操作。请注意,这是过时的,不应使用。应使用实现对象。此外,应避免以大写字母开头方法和变量名,因为它们很容易与类名混淆。Finally,你不能在泛型中使用
int
,你必须使用包装类
Integer
。感谢大家的帮助,但是任务需要使用line.split。我同意扫描仪是最好的选择,但是在这种情况下我不能使用it@mathew如果你能澄清你正在努力解决的问题,这将是非常有帮助的使用
String.split()的替代方法
在我的回答中。再次感谢,尽管任务规定完全不允许使用扫描仪。我只是想知道为什么我编写的代码不起作用,据我所知,它读取数组中的每一行并拆分每个字符串,并尝试搜索数组。任何一个地图类型集合都可以用于此。请注意,这已过时,不应使用。应改用对象实现。此外,应避免以大写字母开头方法和变量名,因为它们很容易与类名混淆。最后,您不能在泛型中使用
int
,必须使用包装类
Integer
。谢谢s的帮助家伙,但任务需要使用line.split。我同意扫描仪将是最好的选择,但在这种情况下,我不能使用it@mathew如果您能准确地说明您正在努力解决的问题,这将非常有帮助。我发布了一个使用
String.split()的替代方案
在我的回答中。再次感谢,尽管任务规定完全不允许使用扫描仪。我只是想知道为什么