move_slow_break_things
move_slow_break_things

Reputation: 847

java - Reading specific words from a file

So I have a zoo database where visitors come and see different kinds of animals. Each visitor has his own log of what animals he saw and when. I'm trying to create a list of all the animals the visitor saw and also separate out the visitor's unique id. If a visitor saw the same kind of animal on different days then map the number of times the animal was seen to the animal name. I just need the names of the animals for the list and the client's id for other processing purposes. The dates aren't important. This is what a sample log would look like.

ClientId: 1001
Zebra 10/1
Cheetah 10/2
Tiger 10/2
Lion 10/3
Zebra 10/4

This is my log class:

public class Log {

    private int clientId;
    private int animalCount = 1;

    private Map<String, Integer> animalList = new HashMap<String, Integer>();

    public void addFromFile(String fileName) {
        File file = new File(fileName);
        Scanner sc = null;
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            System.out.println("File Not Found.");
        }
        while (sc.hasNextLine()) {
            sc = new Scanner(sc.nextLine());
                String s = sc.next();

                 //Don't add the dates. Only the Strings

                if (!isNumeric(s)) {
                    if (animalList.containsKey(s)) {
                        animalList.put(s, animalList.get(s) + 1);
                    } else {
                        animalList.put(s, animalCount);
                    }
                }
            }

        }

    public Map<String, Integer> getList() {
        return animalList;
    }

    public int getClientId() {
        return this.clientId;
    }

    public int itemCount() {
        return this.itemCount;
    }

    public boolean isNumeric(String str) {
        return Character.isDigit(str.charAt(0));
    }
}

The log would always appear in that order. I'm having trouble just processing the client Id line first and then doing the animal name processing.

The output for the following using the example log above is:

Log log = new Log();
log.addFromFile("DataSetOne1.txt");
System.out.println(log.getList());
    //Output: {ClientId:=1}

My code just keeps getting stuck on the first line. I'd like to be able to process the first line and get it out of the way first because it contains the only integer value that I care for. I'm just confused about how to approach this.

Upvotes: 0

Views: 114

Answers (2)

move_slow_break_things
move_slow_break_things

Reputation: 847

Ended up figuring something out. It's not the most elegant solution, but it does the trick.

public void addFromFile(String fileName) {
        File file = new File(fileName);
        Scanner sc = null;
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (sc.hasNextLine()) {
            Scanner sc2 = new Scanner(sc.nextLine());
            while (sc2.hasNext()) {
                String s = sc2.next();
                if (!isNumeric(s)) {
                    // Retrieve the client ID and assign it to the instance
                    // variable
                    if (s.equals("ClientId:")) {
                        clientId = Integer.parseInt(sc2.next());
                    }
                    if (animalList.containsKey(s)) {
                        animalList.put(s, animalList.get(s) + 1);
                    } else {
                        animalList.put(s, animalCount);
                    }
                }
            }
        }
        // Delete the ClientId entry
        animalList.remove("ClientId:");
    }

Upvotes: 0

CosmicGiant
CosmicGiant

Reputation: 6441

If each entry is on a separate line, loop through the chars until either digits (numbers) or : chars are found, on each line, storing the chars in a 2D array, or an array within a list.

If the char found on a line is :, discard the previously acquired chars for that line, and record the chars until the end of the line, this, with a String.trim(), should get you your visitor's ID.

Else, if a digit was found, stop recording the chars, and break out of that line's loop, and, again, after a .trim(), you should have the name of the animal.


PS: RegEx, or a Pattern-Matcher, are harder to understand (as you pointed out in OP's comments), but would make this much easier. You should really, really, really, REALLY learn this stuff if you want to work with string or file parsing.

Upvotes: 1

Related Questions