While loops (binary strings)

need help with this program

Page 1 of 1

4 Replies - 2240 Views - Last Post: 06 November 2008 - 11:37 PM Rate Topic: -----

#1 djsat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-November 08

While loops (binary strings)

Posted 05 November 2008 - 11:08 PM

I need help with this program. I have to write a program that inputs a word representing a binary number ( that is 1's and 0's only), if the word word does not represent a valid binary number, then the program should keep prompting the user for a new word until a word respenting a valid number is input by the user and end it should tell you how many one's are there in the string ( i know how to do this part). I will just post the pseudocode


boolean valid;
String binstr;
int i = 0;
char T;

input binstr
find length of binstr
valid = true;

while (valid)
{ 
   while (valid && not beyond the string)
   {
		T = binstr.CharAt( i );
		if (! (T == '0' || T == '1'))
				 valid = false;
		i++;
   }

   if (!valid)
	input binstr (input is not a binary string and please enter again)

}
  
	  


The problem is when i enter 1 the program just hangs and when i enter 2 it will say it's not a valid binary number and ask for again and if i enter 2 again it will just exit afterwards without checking values again. I am so lost what to do with this now, can anybody help me.

This post has been edited by born2c0de: 07 November 2008 - 12:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: While loops (binary strings)

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: While loops (binary strings)

Posted 05 November 2008 - 11:24 PM

post your program then, that would make it easy for people to help you.
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: While loops (binary strings)

Posted 06 November 2008 - 03:15 PM

This kinda intersted me so i did something up to have a look at it etc..

//
// Class for checking validity of binary 
// String entered by a user.
// Coder : bbq - dreamincode.com
// 
import java.util.*;

class BinaryTest
{
	Scanner input = new Scanner(System.in);
	
	// Function to Check Validity of user entered string
	// Checks and sets a global bool to show if it is a valid
	// binary string
	boolean testInput(char aChar)
	{		
		if (aChar == '1')
			return true;
			else if (aChar == '0')
				return true;
				else 
					return false;
	}
		
	void run(){
		
		boolean valid = true;
		String binaryStr = "";
		
		// Get user input
		System.out.println("Please Enter a binary String (contains 1's and zero's only!) : ");
		binaryStr = input.next();
		
		for (int i = 0; i < binaryStr.length(); i++)
		{
			if(!testInput( binaryStr.charAt(i) ))
				valid = false;
		}
		
		// Based on the above test tell user if it is valid.
		if ( valid == true)
			System.out.println("You have entered a Valid Binary String");
			else 
				System.out.println("You have entered an INVALID Binary String");
		
		System.out.println("\n\nThankyou for using bintest");
		
	}
	
	public static void main(String[] args)
	{
		BinaryTest myTest = new BinaryTest();
		myTest.run();
	}
}




I have not tested it, however it should be ok. :)

hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: While loops (binary strings)

Posted 06 November 2008 - 07:18 PM

Ouf a lot of code for nothing let Java works for you

input the number as a string then use the valueOf with a try/catch

String str = scan.next();
int nb = 0;
boolean itWorked = true;
try {
  nb = Integer.valueOf(str, 2);
}
catch (NumberFormatException e) {
   itWorked = false;
}

if(itWorked) {
   .........


This post has been edited by pbl: 07 November 2008 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: While loops (binary strings)

Posted 06 November 2008 - 11:37 PM

Cool, pbl. Was not sure how to do it as i have never been asked that question :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1