...

View Full Version : is binary method



cwl157
11-22-2004, 01:04 AM
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:

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.

shmoove
11-22-2004, 09:40 AM
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.


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:


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

KeZZeR
11-22-2004, 09:50 AM
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:


for (int i = 0; i < s.length(); i++)

*remembers all the lectures on saving memory*

shmoove
11-22-2004, 10:36 AM
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:


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

KeZZeR
11-22-2004, 12:34 PM
You never know, 4 bytes could mean a lot! ;)

shmoove
11-22-2004, 12:43 PM
I'd like to see the systems you're working on :p

shmoove

cwl157
11-22-2004, 04:58 PM
thanks for the help i figured it out. I was close i just didnt try this combination.


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;
}

shmoove
11-22-2004, 05:51 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum