0

I'm currently trying to complete a hw assignment and honestly confused how to even get past this point.

I am supposed to ask the user for a bit string length of 24 that consists of 1's and 0's. I just can't seem to understand how I would go filling up each single array slot with a only 1 value. After I am giving the 24 bit string of 1's and 0's I'm suppose to convert that 24 bit string into 3 separate 8 bit integer components that represent a number from 0-255, which is suppose have a value on the RGB scale.

For example I would take in a bit string of "111111110000000000000000" this would represent "111111111" for red which would be 255, "00000000" for green which would be 0 and "00000000" for blue, so this value would represent pure red.

I'm very stuck on what to do to even start this but I would think its something like this

import java.util.Scanner;
public class Test {
       public static void main(String args[]){
          String[] bitString;
          bitString = new String[24];

          Scanner consuleInput = new Scanner(System.in);
            System.out.println("what is your input bit string?");
            for(int i=0;i < bitstring.length; i++) {
                bitString = consuleInput.nextLine();
                // this was my rough idea on how to go through the array at everyone spot
                // and fill in a 0 or 1 depending on what the giving bitString is
            }

When it comes to splitting the bit string into three components i'm honestly not sure. I would think its something like you take the bit string array of [0] to[7] and assign that to the redValue, [8]-[15] to green and [16]-[23] to blue. with these three split up I would use something like

 int re = Integer.parseInt(redValue, 2);

which would give me the integer value of that string of 1's and 0's from bitString[0] to bitString[7]. I'm just not entirely sure how to implement it.

1 Answer 1

1

In your original code, you have a typo in your for loop condition - bitstring should be bitString. You're also trying to assign a String to the entire array (String[]). You need to access the array at a certain index and then assign a value to it.

for (int i = 0; i < bitString.length; i++) {
    bitString[i] = consuleInput.nextLine();
}

However, I propose a better approach. Instead of getting the input one bit at a time, get the whole string and use subtring() to convert every 8 bits to decimal. If needed, you can still implement validation to print an error if the string is not 24 characters long or isn't a binary value.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("what is your input bit string?");
    String input = scanner.nextLine();

    int[] rgb = {
        Integer.parseInt(input.substring(0, 8), 2),
        Integer.parseInt(input.substring(8, 16), 2),
        Integer.parseInt(input.substring(16, 24), 2)
    };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.