Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created September 20, 2017 11:32
Show Gist options
  • Save javamultiplex/62e59ac4516fc1c715066d4a10ac8303 to your computer and use it in GitHub Desktop.
Save javamultiplex/62e59ac4516fc1c715066d4a10ac8303 to your computer and use it in GitHub Desktop.
Given Number is Octal or not?
package com.javamultiplex.number;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category Number Problems
* @problem Number is octal or not
*
*/
public class OctalNumber {
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter number : ");
String number = input.next();
/*
* Regular expression that matches string containing only
* digits[0-9].
*/
String pattern1 = "^[0-9]+$";
if (number.matches(pattern1)) {
/*
* Regular expression that matches string containing only octal
* digits[0-7].
*/
String pattern2 = "^[0-7]+$";
if (number.matches(pattern2)) {
System.out.println("It is octal.");
} else {
System.out.println("It is not octal.");
}
} else {
System.out.println("Please enter valid number.");
}
} finally {
if (input != null) {
input.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment