Printable View
Dude you just help me big time now i can finish like 7 programs!:-bd:-bd:-bd:-bd
If you are unsure of the input, make sure to catch any NumberFormatExceptions that can be thrown when trying to parse something that can't be parsed
Code Java:
Personally I'd check the input before trying to parse it as exceptions are slow.
// Json
Good point. Out of curiosity though...according to this benchmark below going through the try/catch block is 10 times faster than validation (with correct input of course). For incorrect input the validation is 2 times faster than try/catch. I know in the real world things are much different, and there are probably other ways that are potentially faster to do validation, but its just food for thought.
Code Java:public class ExceptionTest{ public static void main( String[] args ){ String input = "126"; long start = System.currentTimeMillis(); for ( int i = 0 ; i < 100000; i++ ){ try{ int st = Integer.parseInt(input); }catch ( NumberFormatException e ){ //System.out.println("Error"); } } System.out.println(System.currentTimeMillis() - start ); start = System.currentTimeMillis(); for ( int i = 0 ; i < 100000; i++ ){ if ( !input.matches("[0-9]+") ){ //System.out.println("Error"); }else{ int st = Integer.parseInt(input); } } System.out.println(System.currentTimeMillis() - start ); } }
I updated your code somewhat to add an input checker of my own.
Code Java:package uk.co.cdl.testing.io; public class ExceptionTest1 { public static void main(String[] args) { String input = "126h"; long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { try { int st = Integer.parseInt(input); } catch (NumberFormatException e) { //System.out.println("Error"); } } System.out.println("Exceptions: " + (System.currentTimeMillis() - start) + "ms"); start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { if (!input.matches("[0-9]+")) { //System.out.println("Error"); } else { int st = Integer.parseInt(input); } } System.out.println("Regexp: " + (System.currentTimeMillis() - start) + "ms"); start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { if (isDigits(input)) { int st = Integer.parseInt(input); } } System.out.println("isDigits: " + (System.currentTimeMillis() - start) + "ms"); } public static boolean isDigits(final String input) { if (input == null) { return false; } else if ("".equals(input)) { return false; } else { for (int i = 0; i < input.length(); ++i) { if (!Character.isDigit(input.charAt(i))) { return false; } } } return true; } }
In the case where the input is not a number I get the following output.
And in the case were the input is a number I get the following output.Quote:
Exceptions: 203ms
Regexp: 141ms
isDigits: 0ms
// JsonQuote:
Exceptions: 0ms
Regexp: 141ms
isDigits: 0ms
Thanks for posting that code Json.
when i tried this code
Code Java:
it compiled showing no errors ... however when I run the program i get an exception in thread "main" java.lang.NumberFormatException: for input String "Ashar" along with other things on the console after running the program
I want to know why that happens ... I thought this statement will give me the ASCII equivalent to the string "Ashar"
No this is designed for converting a String representation of a Numerical value into a numerical value.
I.e "1" would become integer 1 and "8" would become integer 8 rather than string literals.
the following would print each of the ascii values.
Code Java:
Chris
Thank you chris :)
can you tell me why was it used in a program to read a number .. does JOptioPane always reads strings?
like this :
JOptionPane.showInputDialog() does indeed always return a String
Or null?
// Json
Well yes or null i suppose, but its a null String lol ;)
hehe :D
// Json
Hi guys, I don't understand a thing...
Let me give you some code first:
Code Java:public class Airplane { private String plane, firstLetter, lastLetter; private int nRows; private int columns; String[] windowsSeats; String[] aisleSeats; public Airplane(String plane, int nRows, String firstLetter, String lastLetter) { this.plane=plane; this.nRows=nRows; this.firstLetter=firstLetter; this.lastLetter=lastLetter; try{ this.columns=Integer.parseInt(lastLetter)-Integer.parseInt(firstLetter)+1; }catch ( NumberFormatException e ){ System.out.println("Wrong number..."); return; } ... }
I throw it with a kind of main like:
Code Java:public class MainExample { public static void main(String[] args) throws FullFlight, SeatTaken, NoPlane, NoSeat { FlightManager fm = new FlightManager(); fm.addAirplane("B737-1", 32, "A", "F"); fm.addAirplane("B737-2", 42, "A", "F"); ... }
I obtain always the error message.. Can you explain why the conversion (this.columns..) doesn't succeed?
Hello there, its because you are trying to find a numeric value in a string which is the letter "F" or "A", this will always throw a NumberFormatException because neither A or F are numbers.
If you wish to convert this into their ASCII equivalents you can use this.
You also need to make sure that lastLetter and firstLetter is a char instead of a String.
Hope that helps.
// Json
Hi Json and thanks for the answer first!
I understood your thesis but unfortunately I can not change the type of firstLetter and lastLetter. they're passed as String in the function.
Can I find another way to solve it?
Take into account that it is a Flight Manager program, the function "addAirplane" receives these 2 String as parameters, and I must be able to, when I make a reservation, understand how many "columns" of seats there are and also for compute the capacity of an airplane (rows*columns).
I accept any kind of explanation and help.
Is very important to me to understand because it is part of an exam at univerisity.
Thanks
If you know that the 2 strings are always going to be 1 character long you can do this.
Code Java:char character = firstLetter.charAt(0);
That will grab the character at the first position in the string and then you can use that to get the int value.
// Json