So far I got this program to run but I got stock in this part: the program should keep prompting the user for a new word until a word representing a valid binary number is input by the user. I know I’m suppose to use a loop but I dont know where to put it.
package programming_assignment_1;
import java.util.Scanner;
public class Programming_Assignment_1 {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.print("Please imput a binary number : ");
String binary;
binary = sc.next();
boolean isBinary = true;
char[] values = binary.toCharArray();
for(int i=0; i<values.length; i++)
{
if( (values[i] != '0') && (values[i] != '1') )
{
isBinary = false;
break;
}
}
if(!isBinary)
{
System.out.println("this is not a binary number");
}
else
{
for(int i=0; i<1; i++)
{
String consecutive1s = "111";
if (binary.indexOf(consecutive1s) != -1)
{
System.out.println("accepted");
}
else
{
System.out.println("rejected");
}
}
}
}
}
Additionally, I am not sure about your “for” loop – it does not add anything to your program, since it is executed exactly once …