4

I'm working on a pokemon battle simulator, (basically pokemonshowdown gen1), trying to automate making the pokemon array, but running into a Scanner problem. File is formatted as: Name.Type1.Type2.hp.attack.defense.special.speed.list of learnable moves. So: Aerodactyl.Flying.Rock.80.105.65.60.130.Agility,Bide,Bite,Double-Edge,Double Team,Dragon Rage,Fire Blast,Fly,Hyper Beam,Mimic,Rage,Razor Wind,Reflect,Rest,Sky Attack,Substitute,Supersonic,Swift,Take Down,Toxic,Wing Attack.

I've gotten a method working for both my typeArray and moveArray but for some reason using basically the same loop the scanner is returning empty tokens instead of what's in the file.

Exception:

0   Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Controller.initPokemonArray(Controller.java:169)
at Controller.<init>(Controller.java:29)
at Driver.main(Driver.java:15)

Here's the whole method, it's throwing the error at the parseInt call for hp.

    private Pokemon[] initPokemonArray() {
    Pokemon[] pokemonArray = new Pokemon[83];
    try {
        Scanner inputScan = new Scanner(new File("src/pokemon")).useDelimiter(".");
        String name = "";
        Type type1 = typeArray[0];
        String inputType1 = "";
        Type type2 = typeArray[0];
        String inputType2 = "";
        int hp = 0;
        int atk = 0;
        int def = 0;
        int spc = 0;
        int spe = 0;
        String[] lm = {};
        Move[] learnableMoves;
        int counter = 0;
        while (counter < 83) {
        System.out.print(counter);
            if (inputScan.hasNextLine()) {
                name = inputScan.next();
                System.out.println(name+" ");
                //System.out.print("name");
                inputType1 = inputScan.next();
                for (int i = 0;i < 16;i++)
                    if (inputType1.equals(typeArray[i].toString()))
                        type1 = typeArray[i];
                System.out.println(type1.toString()+" ");
                inputType2 = inputScan.next();
                for (int i = 0;i < 16;i++)
                    if (inputType2.equals(typeArray[i].toString()))
                        type2 = typeArray[i];
                System.out.println(type2.toString()+" ");
                hp = Integer.parseInt(inputScan.next());
                System.out.println(hp+" ");
                atk = Integer.parseInt(inputScan.next());
                System.out.println(atk+" ");
                def = Integer.parseInt(inputScan.next());
                System.out.println(def+" ");
                spc = Integer.parseInt(inputScan.next());
                System.out.println(spc+" ");
                spe = Integer.parseInt(inputScan.next());
                System.out.println(spe+" ");
                lm = inputScan.next().split(",");
                System.out.println();
            }
            //TODO move this to private helper method
            learnableMoves = new Move[lm.length];
            for (int i = 0;i < 160;i++) {
                for (int j = 0;j < lm.length;j++) {
                    if (lm[j] == moveArray[i].getName())
                        learnableMoves[j] = moveArray[i];
                }
            }
            pokemonArray[counter] = new Pokemon(name,type1,type2,hp,atk,def,spc,spe,learnableMoves);
            counter++;
        }
        inputScan.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return pokemonArray;
}

DISCLAIMER: this is a project for my java 2 course and also my first post on here so I don't know exactly what I'm supposed to do so just letting it be known down here.

6
  • What error is it throwing? Commented Nov 14, 2016 at 21:20
  • 1
    "error at the parseInt call for hp" -- what kind of error? Does it happen for every line or some in particular? It would be helpful if you included a portion of src/pokemon that causes the issue. Commented Nov 14, 2016 at 21:21
  • As long as you have a specific question and aren't just looking for someone else to write if for you you are fine. Try saving inputScan.next() into a String variable first and the printing it. You may be picking up a space or something. Also look into inputScan.nextInt(). Commented Nov 14, 2016 at 21:21
  • Show the actual entire input (from the start of your program until the error occurs). Commented Nov 14, 2016 at 21:22
  • 1
    Decent question. Please provide the text of the error itself. In an unrelated note, you should really break your code into methods instead of having one huge block of text like this. It will be much easier to read, among other things. Commented Nov 14, 2016 at 21:22

1 Answer 1

2

The useDelimiter() method takes the passed String as regex value.https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#useDelimiter-java.lang.String-. A period has a specific meaning in regex. To get the specific character "." use this.

Scanner inputScan = new Scanner(new File("src/pokemon")).useDelimiter("\\.");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That seemed to be the problem. Didn't know that about regex and periods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.