Results 1 to 10 of 10
  1. #1
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default 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 ?

    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;
      }
    }
    Thanks...
    Last edited by Eranga; 12-22-2010 at 04:52 PM. Reason: code tags added

  2. #2
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    You can use Try Catch

  3. #3
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    yes i know
    how can i do that?
    thanks for reply.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eng_hyzoom View Post
    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
    If you detect a situation where you want to throw that exception, simply throw it:

    Java Code:
    throw new NumberFormatException("you goofed!");
    ... and let anything else catch it and do with it what it wants.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    thanks very much Jos
    this method parseBinary

    Java 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';
        }
    it used to convert binary String into binary
    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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eng_hyzoom View Post
    thanks very much Jos
    this method parseBinary

    Java 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';
        }
    it used to convert binary String into binary
    i want to put exception if user enter String not binary String
    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:

    Java Code:
    int v= binaryString.charAt(i) - '0';
    if (v < 0 || v > 1)
       throw new NumberFormatException("you goofed!");
    kind regards,

    Jos
    Last edited by JosAH; 12-22-2010 at 06:38 PM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Throw and catches
    By cdawg_2010 in forum New To Java
    Replies: 3
    Last Post: 11-23-2010, 01:36 PM
  2. About throw message
    By killerf2006 in forum New To Java
    Replies: 4
    Last Post: 08-22-2010, 11:48 AM
  3. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 03:45 PM
  4. throw exception
    By GIRISH PATEL in forum New To Java
    Replies: 4
    Last Post: 04-23-2009, 04:35 AM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •