我已经为我的游戏成功创建了一个物品扫描仪。

这是我的代码:

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

public class ItemScanner {

    public static void main(String args[]) {

        System.out.print("Enter item to find: ");
        Scanner sc = new Scanner(System.in);
        find(sc.nextLine());

    }

    public static void find(String delim) {
        File dir = new File("accounts");
        if (dir.exists()) {
            String read;
            try {
                File files[] = dir.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File loaded = files[i];
                    if (loaded.getName().endsWith(".txt")) {
                        BufferedReader in = new BufferedReader(new FileReader(loaded));
                        StringBuffer load = new StringBuffer();
                        while ((read = in.readLine()) != null) {
                            load.append(read + "\n");
                        }
                        String delimiter[] = new String(load).split(delim);
                        if(delimiter.length > 1) {
                                System.out.println("Found " + (I don't know how to read 1 tab over - 1) + " time(s) in " + loaded.getName() + "!");
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("error: dir wasn't found!");
        }
    }
}


在寻找玩家拥有多少物品的时候,我正处于使我的生活更轻松的最后一步。

这是场景:

搜寻项目:6570

在Account.txt中找到[x]时间!

这就是项目的布局

帐户项目= 6570 1

内容如下:6570是项目,然后[tab]等于1的用户拥有多少项目。

所以如果说

帐户项目= 6570 24

用户有24个项目。



问题:

我只是不知道如何从1个选项卡返回项目的值。

因此,如果我搜索6570,并且找到了它,我将如何获取被发现的物品数量?这是我的退货代码

String delimiter[] = new String(load).split(delim);
                        if(delimiter.length > 1) {
                                System.out.println("Found " + (I don't know - 1) + " time(s) in " + loaded.getName() + "!");
                        }

最佳答案

您可以通过使用数组访问器寻址数组中的元素来访问它们。

如果示例代码中load的String值为6570<TAB>24,则数组元素将具有以下值

delimiter[0] = '6570'
delimiter[1] = '24'


要获得值'24',请使用delimiter [1]。例如

System.out.println("The value is " + delimiter[1]);


将打印


值是24

关于java - 如果找到,则在[java]上返回一个标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1969381/

10-09 01:08