Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算dat文件中的字符数?_Java - Fatal编程技术网

Java 如何计算dat文件中的字符数?

Java 如何计算dat文件中的字符数?,java,Java,这是我的密码: import java.util.Scanner; import java.io.*; public class BaseballStats { public static void main (String[] args) throws IOException { Scanner fileScan, lineScan; String fileName; Scanner scan = new Scanner(S

这是我的密码:

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

public class BaseballStats
{

    public static void main (String[] args) throws IOException
    {
        Scanner fileScan, lineScan;
        String fileName;

        Scanner scan = new Scanner(System.in);

        System.out.print ("Enter the name of the input file: ");
        fileName = scan.nextLine();
        fileScan = new Scanner(new File(fileName));

        while (fileScan.hasNext())
        {
          fileName =fileScan.nextLine();
          System.out.println("Name: " + fileName);

          lineScan = new Scanner (fileName);
          lineScan.useDelimiter(",");
          while (lineScan.hasNext())
            System.out.println(" "+lineScan.next());

          System.out.println();
        }

    }
}
运行stats.dat文件时

Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Sally Slugger,o,h,h,o,o,h,h,w
Missy Lots,o,o,s,o,o,w,o,o,o
Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
Sarah Swift,o,o,o,o,h,h,w,o,o,o
Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o
我得到这个输出:

Name: Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
 Willy Wonk
 o
 o
 h
 o
 o
 o
 o
 h
 w
 o
 o
 o
 o
 s
 h
 o
 h

Name: Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o,o
 Shari Jones
 h
 o
 o
 s
 s
 h
 o
 o
 o
 h
 o
 o
 o
 o

Name: Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
 Barry Bands
 h
 h
 w
 o
 o
 o
 w
 h
 o
 o
 h
 h
 o
 o
 w
 w
 w
 h
 o
 o

Name: Sally Slugger,o,h,h,o,o,h,h,w
 Sally Slugger
 o
 h
 h
 o
 o
 h
 h
 w

Name: Missy Lots,o,o,s,o,o,w,o,o,o
 Missy Lots
 o
 o
 s
 o
 o
 w
 o
 o
 o

Name: Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
 Joe Jones
 o
 h
 o
 o
 o
 o
 h
 h
 o
 o
 o
 o
 w
 o
 o
 o
 h
 o
 h
 h

Name: Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
 Larry Loop
 w
 s
 o
 o
 o
 h
 o
 o
 h
 s
 o
 o
 o
 h
 h

Name: Sarah Swift,o,o,o,o,h,h,w,o,o,o
 Sarah Swift
 o
 o
 o
 o
 h
 h
 w
 o
 o
 o

Name: Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
 Bill Bird
 h
 o
 h
 o
 h
 w
 o
 o
 o
 h
 s
 s
 h
 o
 o
 o
 o
 o
 o

Name: Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
 Don Daring
 o
 o
 h
 h
 o
 o
 h
 o
 h
 o
 o
 o
 o
 o
 o
 h

Name: Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o
 Jill Jet
 o
 s
 s
 h
 o
 o
 h
 h
 o
 o
 o
 h
 o
 h
 w
 o
 o
 h
 h
 o
问题: 现在,我需要修改解析文件中一行的内部循环,这样它就可以(单独)计算命中、出局、行走和牺牲的次数,而不是打印每个部分。应该为每个玩家打印这些汇总统计数据

我遇到的问题是,我不知道如何使其计数特定字符,我也不知道如何使其不计数名称中的字符


如果有人能给我指路,那就太好了。

你只需要数数几类字符。您只需为以下各项使用单独的计数器:

      int wCount = 0;
      int hCount = 0;
      // ...
      while (lineScan.hasNext()) {
        switch (lineScan.next()) {
            case 'w': wCount++; break;
            // ...
        }
      }
      // print the stats

下面是您的
main()
方法的完整实现,使用的是给定输入数据时我自己会使用的方法。首先,我用逗号分割
stats.dat
文件的每一行。这就给我们留下了一个数组,其中第一个元素是玩家姓名,其余元素是单个游戏事件统计数据(例如out、walk)。接下来,我只需统计每个玩家的统计数据,然后将其显示到控制台

public static void main (String[] args) throws IOException {
    Scanner fileScan;
    String fileName;

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter the name of the input file: ");
    fileName = scan.nextLine();
    fileScan = new Scanner(new File(fileName));

    while (fileScan.hasNext()) {
        String currLine = fileScan.nextLine();
        String[] parts = currLine.trim().split(",");

        String playerName = "";
        int hits=0, outs=0, walks=0, sacs=0;

        if (parts.length > 0) {
            playerName = parts[0];
        }

        for (int i=1; i < parts.length; ++i) {
            String stat = parts[i].trim();
            if (stat.equals("h")) {
                ++hits;
            }
            else if (stat.equals("o")) {
                ++outs;
            }
            else if (stat.equals("w")) {
                ++walks;
            }
            else if (stat.equals("s")) {
                ++sacs;
            }
        }

        // display aggregated player stats to the console
        System.out.println(playerName + ":");
        System.out.println("hits : "  + hits);
        System.out.println("outs : "  + outs);
        System.out.println("walks : " + walks);
        System.out.println("sacs : "  + sacs);
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪文件扫描;
字符串文件名;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入输入文件名:”);
fileName=scan.nextLine();
fileScan=新扫描仪(新文件(文件名));
while(fileScan.hasNext()){
字符串currLine=fileScan.nextLine();
String[]parts=currLine.trim().split(“,”);
字符串playerName=“”;
int hits=0,out=0,walks=0,sacs=0;
如果(零件长度>0){
playerName=parts[0];
}
对于(int i=1;i
Scanner是一个重量级软件,因此使用它来简单地读取行和拆分行是一种过火的行为。使用
BufferedReader.readLine()
String.split()
。另外,只要可能,不要预先声明变量,而是声明初始化的位置

调用
split()
将得到一个数组,在该数组中,您知道第一个元素是名称,因此可以跳过它

Scanner sysin = new Scanner(System.in);
System.out.print("Enter the name of the input file: ");
String fileName = sysin.nextLine();

try (BufferedReader fileIn = new BufferedReader(new FileReader(fileName))) {
    for (String line; (line = fileIn.readLine()) != null; ) {
        String[] values = line.split(",");
        int hits = 0, outs = 0, walks = 0, sacrifices = 0;
        for (int i = 1; i < values.length; i++)
            switch (values[i].charAt(0)) {
                case 'h': hits++;        break;
                case 'o': outs++;        break;
                case 'w': walks++;       break;
                case 's': sacrifices++;  break;
            }
        System.out.println(values[0] + ": " + hits + " hits, " +
                                              outs + " outs, " +
                                              walks + " walks, " +
                                              sacrifices + " sacrifices");
    }
}
Scanner sysin=新扫描仪(System.in);
System.out.print(“输入输入文件名:”);
字符串fileName=sysin.nextLine();
try(BufferedReader fileIn=new BufferedReader(new FileReader(fileName))){
for(字符串行;(line=fileIn.readLine())!=null;){
字符串[]值=行。拆分(“,”);
智力命中率=0,出局率=0,步行率=0,牺牲率=0;
对于(int i=1;i
为此,我只需使用一个映射来计算每行上不同类型的数据。我会分割数据,第一项始终是人名,然后在HashMap中统计计数。使用这种方法添加不同类型的数据也是最容易的,因为您只需在项查找中添加一个类型,就完成了。一个例子是,如果您想为Bunt添加一个b,那么您只需要添加一个b

itemLookup.put("b", "Bunt"); 
按照代码,你就完了。下面是实现

我只是创建了一个方法来处理每一行。我假设用户可以从他们想要的任何提要传入队列

import java.util.HashMap;
import java.util.Map;

public class CountCharacters {

    public static void main(String[] args) {
        CountCharacters.processLine("Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h");
    }

    static Map<String, String> itemLookup = new HashMap<>();

    static {
        itemLookup.put("s", "Strike");
        itemLookup.put("w", "Walk");
        itemLookup.put("o", "Out");
        itemLookup.put("h", "Hit");

    }

    public static void processLine(String currentLineReadFromFile) {
        String inputData[] = currentLineReadFromFile.split(",");
        HashMap<String, Integer> characterCount = new HashMap<>();

        for (String key : inputData) {
            Integer value = characterCount.get(key);
            if (value == null) {
                characterCount.put(key, 1);
            } else {
                value++;
                characterCount.put(key, value);
            }
        }

        // show counted characters of all items collected
        boolean firstItem = true;
        for (String character : characterCount.keySet()) {
            Integer value = characterCount.get(character);
            if (firstItem == true) {
                System.out.println(character);
                firstItem = false;
                continue;
            }
            System.out.println(itemLookup.get(character) + ":" + value);
        }

    }
}
import java.util.HashMap;
导入java.util.Map;
公共类计数字符{
公共静态void main(字符串[]args){
进程行(“Willy Wonk,o,o,h,o,o,o,o,o,o,h,w,o,o,o,o,o,s,h,o,o,o,h”);
}
静态映射itemLookup=newHashMap();
静止的{
itemLookup.put(“s”、“Strike”);
itemLookup.put(“w”,“Walk”);
itemLookup.put(“o”、“Out”);
itemLookup.put(“h”,“Hit”);
}
公共静态void processLine(字符串currentLineReadFromFile){
字符串inputData[]=currentLineReadFromFile.split(“,”);
HashMap characterCount=新HashMap();
for(字符串键:inputData){
整数值=characterCount.get(键);
如果(值==null){
characterCount.put(键,1);
}否则{
值++;
characterCount.put(键、值);
}
}
//显示收集的所有项目的计数字符
布尔值firstItem=true;
for(字符串:characterCount.keySet()){
整数值=characterCount.get(字符);
if(firstItem==true){
System.out.println(字符);
第一项=假;
继续;
}
System.out.println(itemLookup.get(character)+“:”+值);
}
}
}

遇到逗号时开始计数。如果linescannext==',开始计数字符,如果它们不是一个命令,请向所有人发送w