tags:

views:

68

answers:

1

I've successfully created an item scanner for my game.

Here is my code:

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!");
        }
    }
}

I'm on the last step of making my life easier when it comes to finding how many of this item the player has.

here's the scenario:

Search Item: 6570

Found [x] time(s) in Account.txt!

This is how the layout of the items are

account-item = 6570 1

It's read like this: 6570 is the item, then [tab] , 1 equals how much of the item that user has.

So if it says

account-item = 6570 24

The user has 24 of that item.


Problem:

I simply do not know how to return the value of the item from 1 tab over.

So if I search for 6570, and if it is found, how would i get the amount of the item being found? Here's my code to return the items

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  A: 

You can access the elements in array by addressing them using the array accessor.

if the String value of load in your example code is 6570<TAB>24, your array elements will have the following values

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

To get at the value '24', use delimiter[1]. For example

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

Will print

The value is 24

Robert Christie
What if the value was unknown and it just reads the value from the character?
Surfer Kyle
When you split the string, the values in the array are set. Java arrays start at index 0 so the first value is at index 0, the second at index 1, etc. Accessing the elements does not rely on knowing the value in the element, just it's position.
Robert Christie