有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如果找到数组,则返回值[java]上的一个制表符

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

这是我的代码:

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

在帐户中找到[x]个时间。txt

这就是项目的布局

科目=6570 1

它是这样读的:6570是项目,然后[tab],1等于用户拥有的项目数量

所以如果它说

科目=657024

用户拥有该项目的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() + "!");
                        }

共 (1) 个答案

  1. # 1 楼答案

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

    如果示例代码中load的字符串值为6570<TAB>24,那么数组元素将具有以下值

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

    要获取值“24”,请使用分隔符[1]。比如

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

    将打印

    The value is 24