Results 1 to 10 of 10
Thread: Throw Exception in this code
- 12-21-2010, 08:46 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Throw Exception in this code
in the following code converts Binary String into Decimal number
i want to throw NumberFormatException if the string isnt binary
how can i do that ?
Thanks...Java Code:public class Exercise8_8 { public static void main(String[] args) { // Prompt the user to enter a string java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter a binary number string: "); String s = input.nextLine(); System.out.println("The decimal value is " + parseBinary(s)); } public static int parseBinary(String binaryString) { int value = binaryString.charAt(0) - '0'; for (int i = 1; i < binaryString.length(); i++) { value = value * 2 + binaryString.charAt(i) - '0'; } return value; } }Last edited by Eranga; 12-22-2010 at 04:52 PM. Reason: code tags added
- 12-21-2010, 08:52 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 19
- Rep Power
- 0
You can use Try Catch
- 12-21-2010, 08:56 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
yes i know
how can i do that?
thanks for reply.
-
- 12-22-2010, 03:45 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
thanks all for reply
i under stand the concept but i steal cant use it
and this code is home work
so i want to do it before tomorrow
thanks again
- 12-22-2010, 04:39 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-22-2010, 05:13 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
thanks very much Jos
this method parseBinary
it used to convert binary String into binaryJava Code:public static int parseBinary(String binaryString) { int value = binaryString.charAt(0) - '0'; for (int i = 1; i < binaryString.length(); i++) { value = value * 2 + binaryString.charAt(i) - '0'; }
i want to put exception if user enter String not binary String
i dont know where i put this exception i tried put it was error
this is my trial there are 5 errors
Java Code:public static int parseBinary(String binaryString) { try{ int value = binaryString.charAt(0) - '0'; for (int i = 1; i < binaryString.length(); i++) { value = value * 2 + binaryString.charAt(i) - '0'; } catch(Exception ex){ System.out.println("Exception"); } } return value; }Last edited by Eranga; 12-22-2010 at 05:26 PM. Reason: code tags added
- 12-22-2010, 05:26 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you please use code tags nest time.
- 12-22-2010, 05:28 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean by there are 5 errors?
What you have to do is, if you comes with an error return a value that you can handle in the invoked method, main in your case.
- 12-22-2010, 06:18 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Your value 'binaryString.charAt(i) - '0'' needs to be a value 0 or 1; if it isn't simply throw that exception just as I showed you in my previous reply:
kind regards,Java Code:int v= binaryString.charAt(i) - '0'; if (v < 0 || v > 1) throw new NumberFormatException("you goofed!");
JosLast edited by JosAH; 12-22-2010 at 06:38 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Throw and catches
By cdawg_2010 in forum New To JavaReplies: 3Last Post: 11-23-2010, 01:36 PM -
About throw message
By killerf2006 in forum New To JavaReplies: 4Last Post: 08-22-2010, 11:48 AM -
what exception to throw
By DoolinDalton in forum New To JavaReplies: 5Last Post: 02-10-2010, 03:45 PM -
throw exception
By GIRISH PATEL in forum New To JavaReplies: 4Last Post: 04-23-2009, 04:35 AM -
throw an exception
By sfe23 in forum New To JavaReplies: 3Last Post: 02-14-2009, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks