Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Dude you just help me big time now i can finish like 7 programs!
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
Last edited by copeg; October 25th, 2009 at 10:24 PM.
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.
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 ); } }
Last edited by copeg; October 26th, 2009 at 01:45 PM.
I updated your code somewhat to add an input checker of my own.
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.Exceptions: 203ms
Regexp: 141ms
isDigits: 0ms
// JsonExceptions: 0ms
Regexp: 141ms
isDigits: 0ms
copeg (October 27th, 2009)
Thanks for posting that code Json.
when i tried this code
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.
Chris
Last edited by Freaky Chris; December 4th, 2009 at 09:27 AM.
Ashar (December 4th, 2009)
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 :
int x = Integer parseInt(JOprionPane.showInputDialog("Enter number") ;
JOptionPane.showInputDialog() does indeed always return a String
Or null?
// Json
Well yes or null i suppose, but its a null String lol
hehe
// Json
Hi guys, I don't understand a thing...
Let me give you some code first:
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:
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.
Character.getNumericValue(lastLetter);
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.
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
Daddo (January 23rd, 2010)