-
-
Save mariiaKolokolova/c18a6db2320a592770762fd3077bad0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maricka.kolokolova; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Please input binnary number:"); | |
String str = sc.nextLine(); | |
int a; | |
if (isBinnary(str) == 1) { | |
a = Integer.parseInt(str, 2); | |
System.out.println("Result: \"" + str + "\" -> " + a); | |
} else { | |
System.out.println("Your number is not binary"); | |
} | |
sc.close(); | |
} | |
static int isBinnary(String str) { | |
int res = 0; | |
char[] st = str.toCharArray(); | |
for (int i = 0; i < st.length; i++) { | |
if (st[i] == '1' || st[i] == '0') { | |
res = 1; | |
} else { | |
res = -1; | |
break; | |
} | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment