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 19 of 19

Thread: Bit Strings in Java

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bit Strings in Java

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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    Can you explain your problem? Does the code compile? If not post the errors.
    Does the code execute?
    Post the output and explain what is wrong with it and show what it should be.

    To make testing easier, I suggest that you replace the file with a String. For example:
    Scanner inFile = new Scanner("1101 AND 1000");

    Then we'll all have the same data for testing when there is a problem.


    One way to get to each character in a String is to use the charAt() method. Get one char from each String and do the operation on them and place the output value in a third String.

    For debugging I'd print out the values of every String that is read to be sure it is what you expected to read.
    Last edited by Norm; February 4th, 2012 at 08:44 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    First, my teacher wants us to use a text file for the assignment.
    My data:
    Input:
    101 OR 011
    101 AND 011
    NOT 1100111
    The problem is I don't know what to put exactly in the final "if' statements.
    I know what you are saying is what I have to do in terms of your suggestion. I just dont know how because I missed the notes on it in class. Please futher explain.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    For testing you can put the data in the program. When you have the logic of the program working,
    then you can add the code to read from a file.

    You need to write a small test program that takes a String and prints out the characters in the String one by one in a loop. There are a couple of ways to get a single character. One is the charAt() method. Another is substring.
    Write a small test program that uses one or both of these methods in a loop for the length of the String to get the String's characters one by one and print them out.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java


  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    Well, I did do your suggestions. Now what? how do i perform the operation itself?
    Last edited by razakhan; February 5th, 2012 at 02:58 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    Apply the bitwise operator to the "bits" from each String.
    Do you know the rules for each operator? Given two bits (one from each String) and an operator, what is the result? It will be either a '0' or a '1'.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    					newstr = secondstr.substring(index, (index + 1));
    					System.out.print(newstr);
    Where would the operator go? Have I done this correctly so far?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    Please explain what you are trying to do in the code you posted.
    What are the steps the program must take to do the task? Write a list of short,simple steps.
    For example, the first step could be:
    read a String with the expression to evaluate.
    Then what???

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    I'm just trying to get the individual numbers out.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    trying to get the individual numbers out.
    Post an example of the String you are working on and what you are trying to get.

    Did you try what I suggested in post#4?
    Post the program you wrote and its output when you execute it.

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    input: 1100111
    current output : 1100111
    desired output: 0011000
    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("NOT 1100111");
     
    		//declare variables
    		String firststr, secondstr;
    		String newstr;
     
    		//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 < secondstr.length(); index++)
    				{	
    					newstr = secondstr.substring(index, (index + 1));
    					System.out.print(newstr);
    				}
     
    			}
    			//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++)
    					{
     
    					}
    			}*/
     
    		}
     
    	}
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    You did not post the full contents of the expression you are trying to evaluate. (the NOT is missing)

    Where do you do the NOT logic on the value the code reads into the newstr variable? The code just echos back what was read in.

  14. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    that is exactly what i dont know how to do.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    First you need to know what the NOT operator does. And the AND and the OR
    You should find a tutorial that describes how those boolean operators work.

  16. #16
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    i know what they do, i just dont know how to apply them here

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    how to apply them here
    Your input consists of binary digits: 0 or 1
    Given a binary digit what is its NOT value?
    Show the two examples.

  18. #18
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bit Strings in Java

    While the site was down, i figured out the not part.
    					charValue = secondstr.charAt(index);
    			http://www.javaprogrammingforums.com/newreply.php?do=postreply&t=13796		if(charValue != '0')
    						notValue = 0;
    					if(charValue != '1')
    						notValue = 1;
    					System.out.print(notValue);
    input: NOT 1100111
    Output: 0011000
    SO, that part is fine.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Bit Strings in Java

    On to the next one.

Similar Threads

  1. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  2. Question regarding Strings
    By yeeesh in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2010, 12:09 PM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM