Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created May 22, 2020 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/c18a6db2320a592770762fd3077bad0c to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/c18a6db2320a592770762fd3077bad0c to your computer and use it in GitHub Desktop.
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