12 Replies - 789 Views - Last Post: 04 February 2012 - 02:22 PM Rate Topic: -----

Topic Sponsor:

#1 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Bit Strings in Java

Posted 04 February 2012 - 12:48 PM

Hello.
I was writing a java code for class and I was unable to figure out how strings and sub-strings work. Can anyone provide some insight on the program.
Here is the problem: Given a bit string expression, such as 10110 OR 11101, evaluate it.
Input: Each string represents a bit-string expression with one operator in it. The operators are AND, OR, and NOT. The operands are bit strings, from 1 to 8 bit long. If the expression contains two operands, they will be the same length. Each input must read as a single string. A single blank will separate the operators and operands.
Output: the value of the expression.
My code so far...
import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class BitStringsClient 
{
	public static void main(String[] args) throws IOException
	{
		//communicating with the data
		Scanner inFile = new Scanner(new File("BitStrings.txt"));
		
		//declare variables
		String firststr, secondstr = null;
		int charValue = 0;
		
		//process data
		while(inFile.hasNext())
		{
			firststr = inFile.next();
			if(firststr.equals("NOT"));
			{
				secondstr = inFile.nextLine();
				//for loop to convert bits to their opposite
				for(int index = 0; index < firststr.length(); index++)
				{
					if()
				}
			}
			//else
			if(firststr.equals("AND"));
			{
				secondstr = inFile.nextLine();
				//for loop to multiply strings
				for(int index = 0; index < firststr.length(); index++)
					{

					}
			}
			//else
			if(firststr.equals("OR"));
			{
				secondstr = inFile.nextLine();
				//for loop to add strings
				for(int index = 0; index < firststr.length(); index++)
					{

					}
			}
			//else
				
		}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Bit Strings in Java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Bit Strings in Java

Posted 04 February 2012 - 12:52 PM

I would treat these as bytes, not Strings, and apply the !, and bitwise & and | operators. That'll handle things for you easily.
Was This Post Helpful? 1
  • +
  • -

#3 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Re: Bit Strings in Java

Posted 04 February 2012 - 12:58 PM

Sir, could you give an example because I have only a vague example of what you mean. The problem was we don't have a book for our class and I missed the notes on this whole topic.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Bit Strings in Java

Posted 04 February 2012 - 01:00 PM

You can use


int operand = Integer.parseInt(bitString, 2);

Was This Post Helpful? 1
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Bit Strings in Java

Posted 04 February 2012 - 01:02 PM

Two things to comment in your work:

- First NOT does not have two operands, so for the NOT case no second operand needed..

- Making the assumption that:

Quote

If the expression contains two operands, they will be the same length

then I will just compare the bits with the same indexes. for example in AND case we know AND will result to one if and only of both are one, else gives 0. so check if the character at index are both "1"
EDIT: post before seeing the above replies, go with them ...

This post has been edited by smohd: 04 February 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Re: Bit Strings in Java

Posted 04 February 2012 - 01:02 PM

@g00se
I know you are trying to help but, I cant use that method. We have not learned about "parse"
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Bit Strings in Java

Posted 04 February 2012 - 01:10 PM

Quote

We have not learned about "parse"


Well then the only alternative is to bit shift
Was This Post Helpful? 0
  • +
  • -

#8 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Re: Bit Strings in Java

Posted 04 February 2012 - 01:19 PM

Look, I know your not going to do my homework. But, I simply dont know really how to do that. I missed the notes on it.Can you give a example or explanation.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Bit Strings in Java

Posted 04 February 2012 - 01:29 PM

See the Oracle tutorial on bit shifting.
Was This Post Helpful? 0
  • +
  • -

#10 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Re: Bit Strings in Java

Posted 04 February 2012 - 02:06 PM

Well, I read the tutorial and tried the operation in the "not" part of my code and errors kept coming up whenever I added it in. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Bit Strings in Java

Posted 04 February 2012 - 02:06 PM

Post your code and error messages exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#12 razakhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-February 12

Re: Bit Strings in Java

Posted 04 February 2012 - 02:10 PM

		while(inFile.hasNext())
		{
			firststr = inFile.next();
			if(firststr.equals("NOT"));
			{
				secondstr = inFile.nextLine();
				//for loop to convert bits to their opposite
				for(int index = 0; index < firststr.length(); index++)
				{	
					if(firststr.charAt(index) < 1 && firststr.charAt(index) > -1)
						firststr = ~firststr.substring(index, (index + 1));
					System.out.print(firststr);
				}
			}



Error is at line 11.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Bit Strings in Java

Posted 04 February 2012 - 02:22 PM

I'm pretty sure the ~ operator can't be applied to Strings. You will have to deal with primitives, not Strings. If you use the charAt() method and subtract '0' from the char, that will return the int value of the number. Since '0' as a char != 0 as an int.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1