Exception: java.util.InputMismatchException

3

Good morning Community:

I have an employee.dat file with the following content (the correlative numbering in the left margin is not part of the contents of the file):

  • employees: 3
  • Carl Hacker | IT Manager | 75000.0 | 1987 | 10 | 15
  • Harry Cracker | IT Analyst | 50000.0 | 1989 | 8 | 1
  • Tony Tester | Software Developer | 40000.0 | 1990 | 1 | fifteen
  • When executing I get the exception: Exception in thread "main" java.lang.NumberFormatException: For input string: "r"

    The error marks it in another method that makes the reading of the file and constructs the employee objects (Exactly in the line: double salary = Double.parseDouble (tokens [2]);

    public static Employee readEmployee(Scanner in)
        {
                String line = in.nextLine();
                String[] tokens = line.split("\ |");
                String name = tokens[0];
                String role = tokens[1];
                double salary = Double.parseDouble(tokens[2]);
                int year = Integer.parseInt(tokens[3]);
                int month = Integer.parseInt(tokens[4]);
                int day = Integer.parseInt(tokens[5]);
                return new Employee(name, role, salary, year, month, day);
        }
    

    The main method from which I make the call is:

    // recupera todos los registros en un nuevo array
        try(Scanner in = new Scanner(new FileInputStream("employee.dat"), "UTF-8"))
        {
            Employee[] newStaff = Employee.readData(in);
            // print los nuevos registro de empleados
            for(Employee e : newStaff)
            {
                System.out.println(e);
            }
        }
    

    I hope your kind help with this exception.

    Thank you.

        
    asked by David Leandro Heinze Müller 20.10.2016 в 15:15
    source

    2 answers

    0

    The problem is how you are dividing the tokens:

    String[] tokens = line.split("\ |");
    

    If your intention is for the split to occur with the following string: " | " , then the split should be expressed as follows:

    String[] tokens = line.split(" \| "); // no te olvides el espacio al principio y al final
    

    The vertical bar has special meaning in regex, so you must place the \ in front so that it is interpreted literally.

        
    answered by 21.10.2016 / 15:54
    source
    2

    As the Java documentation says, this error occurs due to to which you are trying to recover a data that does not correspond to the type of data that exists in the file.

    I suspect that it is because in your file on your first line you have the following:

    numero de empleados:3
    

    which means that it is not just an integer, but the number is part of a string and you are trying to recover it as an integer.

    I recommend that you read the whole line as a String and then divide the String taking the two points as a reference (:). It will create an array with two positions, the text before the colon and the text after the colon. You will have to take the text after the colon to recover the number of employees.

    Finally, pass said String to integer (parsing). Something similar to this:

    String todaLaLinea = in.nextLine(); //Recuperamos la línea "numero de empleados:3"
    String lineaDividida[] = todaLaLinea.split(":"); //Dividimos la línea y la almacenamos en un array de Strings tomando como referencia los dos puntos
    int numeroEmpleados = Integer.parseInt(lineaDividida[1]); //Parseamos el número de empleados de String a int
    

    EDIT : So that the answer is not outdated with the new edition of your question (since you have removed the code and error of the original question), as commented on @sstan , the New problem you have is when it comes to splitting.

    When you make a split you have to escape the special characters, that is, you have to treat them as a String and not as a special character. To do this, as you have done well, you need to use the double backslash \ just before the special character. The problem is that for your case you have left a space between the double backslash and the special character:

    String[] tokens = line.split("\ |");
    

    so it should be like this:

    String[] tokens = line.split("\|");
    

    In your case, you have a space before and after the special character, so you should also deal with it when splitting:

    String[] tokens = line.split(" \| ");
    

    As a curiosity: If all the numbers were Double , you could use without any problem String[] tokens = line.split("\|"); because it does not seem that the spaces matter to you and you will return the value Double without any problem. However, since in this case you also have integers, the Integer.parseInt() function will give you a java.lang.NumberFormatException if you do not detect the spaces.

        
    answered by 20.10.2016 в 16:05