java - While loop that checks for an 8 digit binary number -


i'm struggling succesfully write without errors. example type 101 , 103, on 3rd number if type 8 digit binary 10101010 still displays isnt binary. while loop should check if number 1) binary , 2) 8 digits.

while (number.length() != 8) {     for(int = 0; < number.length(); i++)//for loop accumulator     {         if (number.charat(i) != 48 || number.charat(i) != 49)         {             binaryfail = true;             while (binaryfail == true)             {                 system.out.println("the number entered not binary number or 8 digits");                 number = keyboard.nextline();//re-entry             }         }     } } 

i wouldn't use loop check every character. use string.matches() test if input 1's , 0's:

while (!number.matches("[01]{8}")) {     // read number again } 

as afterthought, if wanted similar solution not use regex:

while (number.length() != 8 || !number.replace("0", "").replace("1", "").isempty()) {     // read number again } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -