Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is binary method

    I need to write a method that takes a string from the user and checks to see if it is a binary string
    this is what i have so far:
    Code:
     static boolean isBinary (String s)
    {
      int l = s.length(); 
      int check = 1;
      for (int i = 1; i < l; i++)
      {
        if (s.charAt(i) != 0 && s.charAt(i) != 1)
        {
          check = 0;
        }
       } 
      if (check == 0)
        return false;
    
      else
        return true;
    }
    It compiles but when i enter a string it gives an error message that i dont know what it means. The error message says string index out of range.

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First off, you have a few mistakes:
    * The indexes on the string will start from 0, so you need to change the for loop.
    * You need to compare to the o and 1 characters, and not numbers.
    Code:
    static boolean isBinary (String s)
    {
      int l = s.length(); 
      int check = 1;
      for (int i = 0; i < l; i++) // <- start from 0
      {
        if (s.charAt(i) != '0' && s.charAt(i) != '1') <- compare to characters
        {
          check = 0;
        }
       } 
      if (check == 0)
        return false;
    
      else
        return true;
    }
    Second, the error suggests you're trying to access a character that's outside of the string (although I can't see why that would happen from the code), so adding a little debugging statements should help you narrow the problem down:
    Code:
    static boolean isBinary (String s)
    {
      try {
      System.out.println("Checking string " + s);
      int l = s.length(); 
      int check = 1;
      for (int i = 0; i < l; i++) // <- start from 0
      {
        System.out.println("l=" + l + ",i=" + i + ",char=" + s.charAt(i));
        if (s.charAt(i) != '0' && s.charAt(i) != '1') <- compare to characters
        {
          check = 0;
        }
       } 
      if (check == 0)
        return false;
    
      else
        return true;
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      return false;
    }
    Now when you run the code the console will print out valuable information that will probably help you see what's wrong.

    shmoove

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    int l = s.length();
    Wouldn't this be a waste of memory? Surely you could just ask for the length of s in the for loop like so:

    Code:
    for (int i = 0; i < s.length(); i++)
    *remembers all the lectures on saving memory*

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KeZZeR
    Code:
    int l = s.length();
    Wouldn't this be a waste of memory?
    4 bytes! I thing he can spare that.

    Surely you could just ask for the length of s in the for loop like so:

    Code:
    for (int i = 0; i < s.length(); i++)
    *remembers all the lectures on saving memory*
    And that would degrade the performance because he would be calling a method on every iteration of the loop instead of checking a variable.

    All in all, his method is better in this case.

    shmoove

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You never know, 4 bytes could mean a lot!

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd like to see the systems you're working on

    shmoove

  • #7
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the help i figured it out. I was close i just didnt try this combination.
    Code:
    static boolean isBinary (String s)
    {
      int l = s.length(); 
      int check = 1;
      for (int i = 0; i < l; i++)
      {   
        if (s.charAt(i) == '1' || s.charAt(i) == '0')
        {}
         else
           check = 0;
       } 
      if (check == 0)
        return false;
    
      else
        return true;
    }

  • #8
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Weird because the new code is completely equivalent to the old code (with my corrections), according to DeMorgan's law (I think that's the correct name):

    A || B = !A && !B

    Another suggestion I could make is that once you find one character that isn't 1 or 0 you have no reason to keep on going on with the loop (the result will be false anyway), so breaking out of it (or even returning false immediately) might be a good idea.

    shmoove


  •  

    Posting Permissions

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