If the string does not contain a valid integer then it will throw a NumberFormatException. The parseInt() method is a method of Integer class under java.lang package. Use Integer.parseInt () to Convert a String to an Integer This method returns the string as a primitive type int. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle. In Java, we can use Integer.valueOf () and Integer.parseInt () to convert a string to an integer. So it ignores one space after 46 and 10 and then looks for the String. Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. It should be like this String a s.split (' +') What happened in your case is : When it split '46 10 2' string with ' ', it counts only one space for spliting the String. If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong. In general, it's a good idea to raise an exception as soon as there is a problem. Si te refieres a code Integer.parseInt('Cadena')/code es un método de clase code Integer/code, la cual como su nombre indica, parsea (convierte, transforma) una cadena pasada como parámetro y te devuelve un número de tipo int (no confundir con code Integer.valueOf/code). The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN. unholysampler at 18:19 2 first check with instanceof keyword. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. Sheehan Alam 60k 124 354 556 6 What do you really want to do If the Object isn't an Integer, I'm not sure what your are expecting from your cast. El resultado lo mandará de vuelta al cliente. The folks at Sun who implemented Integer (a long time ago :) ) probably were not thinking of databases when they wrote that method. En este ejercicio tengo que introducir una cadena con cuatro números separados por un espacio, mandarlo al servidor, luego el servidor sumará el primero más el segundo, luego multiplicarlo por el tercero y finalmente restarle el cuarto.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |