Write a java prgram that inputs a word representing a binary number (0s and 1s).
ID: 3634050 • Letter: W
Question
Write a java prgram that inputs a word representing a binary number (0s and 1s). First, your program should check that is is indeed a binary number, that is, that it contains only 0s and 1s. If that is not the case, your program should output that the number is not a valid binary number. If that word contains at least 3 consecutive 1s, your program should output that that word is "accepted," otherwise that it is "rejected." If the word does not represent a valid binary number, the program should keep prompting the user for a new word until a word representing a valid binary number is input by the user.Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String[] args)
{String number;
int i,count;
char digit;
boolean good=false,valid=false;
Scanner in=new Scanner(System.in);
while(!good)
{System.out.print("Enter a binary number: ");
number=in.next();
for(i=0;i<number.length();i++)
{digit=number.charAt(i);
if(digit<'0'||digit>'1')
{i=number.length()+5;
System.out.println(number +" is not a valid binary number");
}
}
if(i==number.length())
{good=true;
count=0;
for(i=0;i<number.length()-2;i++)
{if(number.charAt(i)=='1')
if(number.charAt(i+1)==number.charAt(i+2)&&
number.charAt(i+1)=='1')
{valid=true;
i=number.length();
}
}
if(valid)
System.out.println(number+" is accepted");
else
System.out.println(number+" is rejected");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.