# Binary Calculator -Why does variable execute?

• June 27th, 2012, 08:48 PM
Turk222
Binary Calculator -Why does variable execute?
I am making a program that takes a five digit binary code entered by the user and displays the value of that code. I have no problem determining if the user enters five digits or not or how to calculate the answer so those aspects aren't included in the code here. The problem I'm having is
determining if the user's input is indeed binary. Below is a test I was running to help me conceptualize how to do this. I wanted to use the counter "nobin" to help me determine if the numbers entered are binary or not. The idea is that if the user enters a value that is not 0 or 1 then 1 is added to nobin. Later I would have code that would check to see if nobin is greater than 0. If it is then ,obviously, something other than a 1 or a 0 was entered and thus direct the user to enter a valid binary code. When I run this program , however, nobin always executes even when only ones and zeros are entered. I hope some of you brilliant people can enlighten me as to why this is. Thanks!

Code java:

```  import java.util.Scanner; public class Binary { public static void main (String [] args){   Scanner input= new Scanner(System.in);   System.out.println("Enter a five digit binary number");//prompts user to enter #   String user= input.nextLine();// sets user input to string "user"   String delims=""; //sets the delimiter for string split   String str[]=user.split(delims); //splits the number input into an array   int count=0;//counter for while loop   int nobin=0; //counter to determine if character //other than a one or zero is entered   while (count<user.length()){//loops through each character of user input to //determine if input contains characters other than a // one or a zero. count++; if (str[count]!="0") { if (str[count]!="1")   nobin++;   } }   System.out.println(nobin); } }```
• June 28th, 2012, 12:38 AM
pbrockway2
Re: Binary Calculator -Why does variable execute?
Code :

`if (str[count]!="0")`

Don't compare strings (or any other objects) with == and != as it is unlikely these do what you expect. Instead use the string method equals() which will do exactly what it is documented to do. In the case of strings foo.equals(bar) is true if and only if foo and bar have the same characters in the same order: which is what you want.

All classes have an equals() method and it is intended that it is to be used to determine equality in a way that makes sense for the particular type of object that is being compared.

Quote:

if the user enters a value that is not 0 or 1 then 1 is added to nobin. Later I would have code that would check to see if nobin is greater than 0
And if the user enters an empty string...? This will have no "bad" characters and yet will not be a good binary numeral. Likewise you may, or may not, want to accept numerals with leading zeros or with a leading '-' character.

-----

Integer has a parseInt() that does what you are trying to do. This method is possibly useless for a "homework" exercise, but worth knowing about in any case.
• June 28th, 2012, 05:19 AM
Turk222
Re: Binary Calculator -Why does variable execute?
Awesome! Thanks! I will give that a try.