Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Binary Calculator -Why does variable execute?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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!

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


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Binary Calculator -Why does variable execute?

    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.

    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.
    Last edited by pbrockway2; June 28th, 2012 at 12:42 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Calculator -Why does variable execute?

    Awesome! Thanks! I will give that a try.

Similar Threads

  1. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  2. Binary File. Extract float data from binary file and put into array
    By cardinal152 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 13th, 2012, 07:31 PM
  3. how to execute multiple different queries in one execute?
    By Sakina in forum JDBC & Databases
    Replies: 1
    Last Post: June 9th, 2012, 09:40 AM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM

Tags for this Thread