1,105,331 Community Members

Using charAt(i) to compare three consecutive characters

Member Avatar
nova4005
Light Poster
30 posts since Sep 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everyone, I am working on a program that takes an input from a user and then tests whether it is a binary number or not. If it does not contain only 0's and 1's it prints out not a valid binary number. The part I am confused about is the second part where if it contains at least three consecutive 1's the program prints "accepted" and if it does not it prints "rejected".

I don't think I can use charAt(i) to compare the three consecutive 1's, but I don't know what I can use to test if there are three consecutive 1's in the string.

Thank you for any help you can provide.

 public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String input = sc.next();
        boolean binary = true;
        boolean validBinary = true;

        for (int i = 0; i < input.length(); i++)
            if (input.charAt(i) != '0' && input.charAt(i) != '1')
            {
                binary = false;
                System.out.println("Not a valid binary number.");
                break;
            }
        if (binary == true)
        {
            for (int i = 0; i < input.length(); i++) {
                if (input.charAt(i) == '111')
                    validBinary = true;
        if (validBinary == true)
        {
            System.out.println("Accepted");
            break;
        }
        else
        {
            System.out.println("Rejected");
        }
            }
        }
  }
}
Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
1
 

I don't think I can use charAt(i) to compare the three consecutive 1's, but I don't know what I can use to test if there are three consecutive 1's in the string.

You could use charAt() but it is tedious to do so compared to using contains().
Note that the test input.charAt(i) == '111' is not valid since '111' is not a valid character literal.
To test if your bit String contains at least three consecutive ones somewhere in the String, you can use input.contains("111").

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 
  1. You can use substring to pick each 3 letter sub string and test it for equals "111"
  2. You can use three charAt calls like
    if (input.charAt(i) == '1' && input.charAt(i+1} == ...

ps There's no such thing in Java as '111'. ' delimits a (single) char, " delimits Strings

edit: Just saw mvmalderen's post. Yes, contains(...) is the best solution.

Member Avatar
nova4005
Light Poster
30 posts since Sep 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you mvmalderen, and JamesCherrill for your fast responses. I have made several changes and I think I have it figured out now. I have run the new program several times and I seem to be getting the right responses. I have also added in for it to keep prompting the user for a binary number if other numbers were entered besides 0 or 1. Can you see any other flaws that might not be showing up from the runs I have done?

Thank you again for all your help.

public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String input = sc.next();
        boolean binary = true;

        for (int i = 0; i < input.length(); i++)
            if (input.charAt(i) != '0' && input.charAt(i) != '1')
            {
                binary = false;
                System.out.println("Not a valid binary number.");
                System.out.print("Please enter a binary number(0's and "
                                      + "1's only: ");
                input = sc.next();
            }
        if (input.contains("111"))
        {
            System.out.println("Accepted");
        }
        else
        {
            System.out.println("Rejected");
        }
            }
        }
Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
2
 

Changing the value of input partway throught the loop can only lead to confusion. What if the original value of input is "0101A000" and then the new value is "ABCDE111"? The start of the new String won't be tested again and to the user it will look like it was perfectly accepted.

What if the original value of input is "0A" and the new value is "0111"? From the output the user would have the right to think that the number "0111" has been perfectly accepted, but the value of binary would be false.

Member Avatar
nova4005
Light Poster
30 posts since Sep 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I see that now, thank you for the help bguild. I am still not sure how to fix it though.

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
1
 

Create a method like private static boolean isBinary(String value) that returns true only if the string is a binary number, then create a loop that asks for input and checks it with isBinary until isBinary returns true.

Member Avatar
jalpesh_007
Posting Whiz
385 posts since Sep 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 6 [?]
 
-1
 

i think there is mistake inside for loop:

if (input.charAt(i) != '0' && input.charAt(i) != '1')

Above statement executed if both conditions are true.How it should be possible that at the same location '0' and '1' both are there?

So, it is the problem. and your binary variable never becomes false.first check out at that.

Member Avatar
JamesCherrill
... trying to help
10,364 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

jalpesh ... those are != tests, so the code is OK.

Member Avatar
nova4005
Light Poster
30 posts since Sep 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks again for all the feedback. I am going to do some reading and research on the "private static boolean isBinary(String value)" and make changes to my program accordingly.

Member Avatar
nova4005
Light Poster
30 posts since Sep 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello again, I have been reading over the private static boolean(String value) and how I could use that for my program, but I am still confused on how to make this program work. Can someone explain this method and how I can use it in this program?

Member Avatar
bguild
Posting Whiz
339 posts since Oct 2012
Reputation Points: 163 [?]
Q&As Helped to Solve: 84 [?]
Skill Endorsements: 8 [?]
 
0
 

A method has 5 parts, three of which are optional.

[flags] ReturnType name([ParameterList]) { [code] }

The [flags] is an optional part of a method that indicates what kind of method it is. Many very important indications of what a method can do and how it can be used go here. When present [flags] is a list of words such as public, private, protected, static, final, and synchronized. When a method is private that means it can only be used from inside the same class. When a method is static, that means that it can be used without an instance of the class. Your main method is static so it doesn't have an instance of its class, so unless you find an instance somewhere you will only be able to call static methods of that class.

So a private static method is good for you because main is in the same class and you can use a static method without creating an object first.

The ReturnType says what sort of value the method produces as a result. In this case you want a true or false to indicate whether the string is a binary number containing only 1s and 0s. Methods that return true or false have boolean as the return type. ReturnType is not optional; even methods that produce no result have void in the ReturnType place.

name is what you want to call your method and how you will use your method. It can be almost anything except for a few reserved words, but by convention it starts with a lower-case letter, like isBinary. Putting isBinary as the name means that you would use your method like this: isBinary(input). Using a method is called calling the method, and when a method is called it is replaced by the result that it produces. So when your application is running if(isBinary(input)) would become if(true) or if(false) depending on whether input is a binary number or not, so using this method makes it very easy to write your application.

The [ParameterList] part is optional and surrounded by (). It represents the values that your method needs to work with to decide on the result that it will produce. In this case, you want something like String value which means that isBinary needs a string and it will be named value in the code of the method. Even if you have no [ParameterList] you would still need the ().

The [code] part is where you say what you want your method to do. The [code] part is optional; some methods do nothing, but only when ReturnType is void, because otherwise your method needs to at least produce a result. In your case you want [code] to check input for being a binary number and then use a return statement to produce a result that is either true or false.

So now I hope the meaning of private static boolean isBinary(String value) is clear.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article