Simple Java question

B.def

Member
Jun 13, 2010
68
0
0
My professor gave me this problem due tomorrow.
1. [5 points]
2. For each string entered by user, find if the string is a binary value.
a) Set a boolean variable to true.
b) Use a for loop with counter variable i which runs until String.length( ). Use char charAt(i) method
of String class to compare character at index i with ‘0’ or ‘1’. If a character is not ‘0’ and ‘1’ that
means the string is not binary (set boolean variable to false).
c) If the string is a valid binary value (indicated by Boolean variable), find the corresponding decimal
value. Use the method public static int parseInt(String s, int radix) of Integer class, with radix=2
and your String s. Print the binary string and the decimal value. If it not a binary value, then print a
message as shown below.



I am having a hard time trying to figure it out. I know he wants us to figure out if it is a binary number and then switch it to a decimal value. I just can't figure out what I'm doing wrong.

My code below, I am on step 2b)

Code:
//Part 2
String userString = scan.next();
boolean value = true;
for(int i=1;i<=userString.length();)
{
if(userString.charAt(i)==0 || userString.charAt(i)==1)
{
    value=true;
i++;
}
else {value=false;}

if(value==true)
System.out.print(value);




}

Any help or pointers would be great. Thanks guys.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Think about this way, as soon as you find a character that is NOT a '0' or a '1' then you know the string isn't a valid binary number. You should only ever need to check to see if the character at index i is NOT '0' and is NOT '1'.

Also, when comparing values in your loop 0 is not the same as '0'. One is a character literal and the other is an integer literal.
 

B.def

Member
Jun 13, 2010
68
0
0
Ok, I think I'm getting somewhere.
Code:
String userString = scan.next();
boolean value = true;
for(int i=0;i<=userString.length();)
{
if(userString.charAt(i)!='0' || userString.charAt(i)!='1')
{
value = false;
i++;
}
Still confused on the next step.
Not sure how I am going to declare that the whole string is binary, so I can move onto step c.

I feel like I would do another if statement.
If value = true, continue with code?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
First, if you begin with i = 0 then the exit condition on your for loop should be "< userString.length()" not "<= userString.length()".

Second, if you set value to true, and you run through the whole loop without setting it to false, the entire string must contain '0' or '1', i.e. it's binary.

You also don't need to complete the loop if the character check ever results to true, because if there is a single character that isn't '0' or '1' then the string isn't binary and you can exit the loop with a break.
 

B.def

Member
Jun 13, 2010
68
0
0
Thank you mark, I think I fixed the things you have mentioned.

Code:
String userString = scan.next();

boolean value = true;
for(int i=0;i<userString.length();)
{

    
    if(userString.charAt(i)!='0' || userString.charAt(i)!='1')
{
value = false;
if (value == false)
{break;}

i++;
}
    
    
        if (value)
{



}

Part c tells me to use "public static int parseInt(String s, int radix)"
For some reason every time I try I get a error.
 

dwell

pics?
Oct 9, 1999
5,185
2
0
Code:
boolean isBinary = true;

for(int i = 0; i < s.length(); i++)
{
    char ch = s.charAt(i);

    if(!ch == '0' || ch == '1')
    {
        isBinary = false;
        break;
    }
}

You need to figure out the parseInt part on your own ;)
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Now I want you to look at this part one more time and tell me what you did wrong :).

Code:
if(userString.charAt(i)!='0' || userString.charAt(i)!='1')
{
    value = false;
    if (value == false)
    {break;}

    i++;
}

Hint: the mistake is a matter of redundancy, not incorrect behavior.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Now I want you to look at this part one more time and tell me what you did wrong :).

Code:
if(userString.charAt(i)!='0' || userString.charAt(i)!='1')
{
    value = false;
    if (value == false)
    {break;}

    i++;
}

Hint: the mistake is a matter of redundancy, not incorrect behavior.

The conditional is wrong as well, you want the character to be both not '0' and not '1'. As it's written the strings '0' or '1' will both fail.

I find it easier to read when written like this
Code:
bool test = true;
for(...) {
    if (c == '0' || c == '1') {
        continue;
    }

    test = false;
    break;
}
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
The conditional is wrong as well, you want the character to be both not '0' and not '1'. As it's written the strings '0' or '1' will both fail.

I find it easier to read when written like this
Code:
bool test = true;
for(...) {
    if (c == '0' || c == '1') {
        continue;
    }

    test = false;
    break;
}

Didn't catch that. Damn, programming is a picky business.