import java.util.Scanner;
public class NumberConversionSystems {
public static void main(String[] args) {
//String binary = toBinaryString(number);
Scanner input = new Scanner(System.in);
System.out.println("Number Conversion Systems \n");
// Display the menu
System.out.println("1.\t Decimal to Binary");
System.out.println("2.\t Decimal to Hexadecimal");
System.out.println("3.\t Binary to Decimal");
System.out.println("4.\t Hexadecimal to Decimal \n");
System.out.println("Your choice?");
//Get user's choice
int choice = input.nextInt();
switch (choice) {
case 1: System.out.println("\nEnter Decimal Number");
break;
case 2: System.out.println("\nEnter Decimal Number");
break;
case 3: System.out.println("\nEnter Binary");
break;
case 4: System.out.println("\nEnter Hexadecimal");
break;
default:
System.out.println("\nInvalid choice. Please choose a number between 1 and 4.");
choice = input.nextInt();
break;
}
if (choice == 1) {
int number = input.nextInt();
String binary = toBinaryString(number);
binary = recursive(number);
System.out.printf("Decimal to Binary (%d) = %s", number, binary);
}
else if (choice == 2) {
int number2 = input.nextInt();
String hexadecimal = toHexString(number2);
hexadecimal = recursiveDecHex(number2);
System.out.printf("Decimal to Hexadecimal (%d) = %s ", number2, hexadecimal);
}
else if (choice == 3 ) {
String binary2 = input.next();
int decimal = toDecimalUsingParseInt(binary2);
decimal = recursiveBin(binary2);
System.out.printf("\n2. Binary to decimal - recursive(%s) = %d ", binary2, decimal);
}
else {
String hex = input.next();
int decimal = toHexUsingParseInt(hex);
decimal = recursiveHexDec(hex);
System.out.printf("Hexadecimal to Decimal (%s) = %d ", hex, decimal);
}
input.close();
}
private static String toBinaryString(int number) {
return Integer.toBinaryString(number);
}
private static String toHexString(int number) {
return Integer.toHexString(number);
}
private static int toDecimalUsingParseInt(String binaryNumber) {
return Integer.parseInt(binaryNumber, 2);
}
private static int toHexUsingParseInt(String number) {
return Integer.parseInt(number, 16);
}
private static String recursive(int number) {
StringBuilder builder = new StringBuilder();
if (number > 0) {
String binaryNumber = recursive(number / 2);
int digit = number % 2;
builder.append(binaryNumber + digit);
}
return builder.toString();
}
private static String recursiveDecHex(int number) {
StringBuilder builder = new StringBuilder();
if (number > 0) {
String hexNumber = recursiveDecHex(number / 16);
String hexCode = "0123456789ABCDEF";
int hexDigit = number % 16;
builder.append(hexNumber + hexCode.charAt(hexDigit));
}
return builder.toString();
}
private static int recursiveBin(String binaryNumber) {
int decimal = 0;
int length = binaryNumber.length();
if (length > 0) {
String substring = binaryNumber.substring(1);
int digit = Character.getNumericValue(binaryNumber.charAt(0));
decimal = digit * (int) Math.pow(2, length - 1) + recursiveBin(substring);
}
return decimal;
}
private static int recursiveHexDec(String hexNumber) {
int decimal = 0;
String hexCode = "0123456789ABCDEF";
hexNumber = hexNumber.toUpperCase();
int length = hexNumber.length();
if (length > 0) {
char ch = hexNumber.charAt(0);
int digit = hexCode.indexOf(ch);
String substring = hexNumber.substring(1);
decimal = digit * (int) Math.pow(16, length - 1) + recursiveHexDec(substring);
}
return decimal;
}
}
When I choose an invalid number (Number that is not in between 1 and 4), the programme will then show "Invalid choice. Please choose a number between 1 and 4", and when I enter a valid number after that, the programme just stops running.
It does not ask me to enter a decimal number if, for example, I choose '1'. What am I missing?
You have no loop, where you try it again and again until a vaild number is entered.
Try this one:
int choice = -1;
while (choice < 0 || choice > 4) {
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("\nEnter Decimal Number");
break;
case 2:
System.out.println("\nEnter Decimal Number");
break;
case 3:
System.out.println("\nEnter Binary");
break;
case 4:
System.out.println("\nEnter Hexadecimal");
break;
default:
System.out.println("\nInvalid choice. Please choose a number between 1 and 4.");
break;
}
}
But keep in mind, that there is no escape from the loop beside enterin a correct number.
You are waiting for a new entry in the default of the switch. So it just sits there until you put one in.
You could do that part before the switch more easily:
boolean badEntry = true;
do{
System.out.println("Your choice?");
int choice = input.nextInt();
if (( choice<1 )|| (choice>4)) {
System.out.println("\nInvalid choice. Please choose a number between 1 and 4.");
} else {
badEntry = false;
}
}
while (badEntry);
Your program doesn't stop. It's actually waiting for input after you enter the valid number. If you don't believe me, try entering another number after you enter the valid choice. For me, it worked something like this.
Number Conversion Systems
1. Decimal to Binary
2. Decimal to Hexadecimal
3. Binary to Decimal
4. Hexadecimal to Decimal
Your choice?
5
Invalid choice. Please choose a number between 1 and 4.
1
23
Decimal to Binary (23) = 10111
However, by the time you've reached the point in your program where you enter a valid choice, the part that prints out the message such as "Enter decimal number" has already passed; which is why you're not getting that message.
Related
Below are the code. To fix the error, I simply rewrote the code in getFraction() method to den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/")+1, fracValue.length())) , by adding +1. I have never seen or learn this during my courses and I just encounter this while doing project. I want to understand what the code did in num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/"))) and den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length())), we are converting the numerator to int and the num is all the values before the / and the den is all the values after the /. Am I right ? My second question is that why do we need to add +1 after the indexOf("/") ? Is it so we are taking the values after the /?
import java.util.Scanner;
public class FractionCalculator {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
intro();
while (true) {
String operation = getOperation();
Fraction frac1 = getFraction();
Fraction frac2 = getFraction();
Fraction result = new Fraction(1,1);
String result2 = "";
if (operation.equals("=")) {
System.out.println(frac1+" "+operation+" "+frac2+" is "+frac1.equals(frac2));
} else {
if (operation.equals("+")) {
result=frac1.add(frac2);
} else if (operation.equals("-")) {
result=frac1.subtract(frac2);
} else if (operation.equals("/")) {
if(frac2.getNumerator()==0) {
result2="Undefined";
} else {
result=frac1.divide(frac2);
}
} else if (operation.equals("*")) {
if(frac2.getNumerator()==0) {
result2 = "Undefined";
} else {
result=frac1.multiply(frac2);
}
}
//print results
} if (result2!="") {// division and multiplication by zero is undefined
System.out.println(frac1+" "+operation+" "+"0"+" = "+result2);
} else if (result.getNumerator()%result.getDenominator() == 0) {
System.out.println(frac1+" "+operation+" "+frac2+" = "+(result.getNumerator()/ result.getDenominator()));
} else {
System.out.println(frac1+" "+operation+" "+frac2+" = "+result.toString());
}
}
}
public static void intro() {
System.out.println("\nThis program is a fraction calculator");
System.out.println("It will add, subtract, multiply and divide fractions until you type Q to quit.");
System.out.println("Please enter your fraction in the form a/b, where a and b are integers.");
for (int i=0; i<80; i++) {
System.out.print("-");
}
}
public static String getOperation() {
System.out.println("\nPlease enter an operation (+, -, /, *, = or \"Q\" to quit): ");
Scanner input = new Scanner(System.in);
String operation = input.nextLine();
int x = 0;
while (x == 0) {
if (operation.equals("+") || operation.equals("-") || operation.equals("/") || operation.equals("*") || operation.equals("=")) {
x++;
} else if (operation.equalsIgnoreCase("q")) {
System.exit(0);
} else {
System.out.println("Invalid input, enter valid operation (+, -, /, *, = or \"Q\" to quit)");
operation = input.nextLine();
}
}
return operation;
}
public static boolean validFraction(String input) {
boolean valid;
if (input.startsWith("-")) {
input = input.replaceFirst("-",""); // or use 'input.substring("1", input.length())';
}
if (input.contains("-") || input.charAt(input.indexOf("/")+1)==('0') || input.contains(" ")) {
valid = false;
} else if (input.contains("/")) {
input = input.replace("/", "");
}
if (input.matches("^[0-9]+$") && input.length() > 0) {
valid = true;
} else {
valid = false;
}
return valid;
}
public static Fraction getFraction() {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
String fracValue = input.nextLine();
//validate input
while (!validFraction(fracValue)) {
System.out.println("Please enter a fraction (a/b) or integer (a): ");
fracValue = input.nextLine();
}
//convert to numerator, denominator
int num = 0;
int den = 0;
if (fracValue.contains("/")) {
num = Integer.parseInt(fracValue.substring(0, fracValue.indexOf("/")));
den = Integer.parseInt(fracValue.substring(fracValue.indexOf("/"), fracValue.length()));
} else {
num = Integer.parseInt(fracValue);
den = 1;
}
// return fraction
Fraction fracConv = new Fraction(num, den);
return fracConv;
}
}
substring(int start, int end) includes the character at start and excludes the character at end. Since the integer cannot be parsed with a / in it, you need to do fracValue.indexOf("/")+1 to get just the numerical part of the denominator.
Firstly you have to understand your input
if your input is String a = "6+7", it means your string length is 3, and alloted indexes are 0,1 and 2
where 0 index is '6', 1 index is '+' and 2 index is '7'
So, when you use a.substring(0, a.indexOf("+")) it means you are saying
a.indexOf("+") = 1 index
you should get a string including '0' index but not 1 index, because substring works with inclusive first param and exclusive second param.
In case of this a.substring(a.indexOf("+")+1, a.length())
you don't want to include '+' in your denominator, So you should not include 1 index, that's why you are adding (+1) with indexof.
So, by adding +1 you are saying only pick value from a.indexOf("+") +1, i.e. 2 index,
and length of string is 3, which means you will get string inclusive of 2 index, i.e 7
if your input is "6 + 7" in that case you should use 'trim()' before using Integer.parseInt.
I have written a Java program to convert roman numerals into numbers. My only problem is if somebody enters "IIII" it shows up as 4, but instead it should give an error that it is not a valid roman numeral. I need to include the following rules into my code. Could anybody help me with this?
(1) No digit is repeated in succession more than thrice, i.e., I, X and C cannot be repeated more than 3 times.
(2) The digits V, L and D are not repeated. The repetition of V, L and D is invalid in the formation of numbers.
Conversion of Roman number to Decimal number:
public class RomanNumberUtils {
static String romanNumeral;
static int decimalNum;
public static void main(String args[]) {
RomanNumberUtils roman = new RomanNumberUtils();
roman .convertRomanToDecimal();
roman .printRoman(romanNumeral);
}
public void convertRomanToDecimal () {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Roman number: ");
romanNumeral = scan.nextLine();
romanNumeral = romanNumeral.toUpperCase();
int l= romanNumeral.length();
int num=0;
int previousnum = 0;
for (int i=l-1;i>=0;i--)
{
char x = romanNumeral.charAt(i);
x = Character.toUpperCase(x);
switch(x)
{
case 'I':
previousnum = num;
num = 1;
break;
case 'V':
previousnum = num;
num = 5;
break;
case 'X':
previousnum = num;
num = 10;
break;
case 'L':
previousnum = num;
num = 50;
break;
case 'C':
previousnum = num;
num = 100;
break;
case 'D':
previousnum = num;
num = 500;
break;
case 'M':
previousnum = num;
num = 1000;
break;
}
if (num<previousnum)
{decimalNum= decimalNum-num;}
else
decimalNum= decimalNum+num;
}
}
public static void printRoman (String romanNumeral){
System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is "+decimalNum);
}
}
Here's my version of a Roman numeral to decimal number converter.
Here are the test results from one of my many tests.
Enter a Roman numeral: ccccllllxxxxvvvviiii
The input Roman numeral CCCCLLLLXXXXVVVVIIII is invalid.
The correct Roman numeral is DCLXIV.
The decimal value is 664.
Enter a Roman numeral: mcmlxxii
The input Roman numeral MCMLXXII is valid.
The decimal value is 1972.
Enter a Roman numeral: mmmccclll
The input Roman numeral MMMCCCLLL is invalid.
The correct Roman numeral is MMMCDL.
The decimal value is 3450.
Enter a Roman numeral: mcmlxxio
The input Roman numeral MCMLXXIO contains invalid characters.
Enter a Roman numeral: lcl
The input Roman numeral LCL is invalid.
The correct Roman numeral is CC.
The decimal value is 200.
Enter a Roman numeral: quit
Basically, I converted the input Roman numeral into a decimal number. Then I converted the decimal number back into a Roman numeral. I compared the Roman numerals, and output the correct Roman numeral along with the decimal value.
Here's the complete runnable code.
import java.util.Scanner;
public class RomanNumeralConversion {
public static void main(String[] args) {
RomanNumeralConversion rnc = new RomanNumeralConversion();
rnc.processRomanNumerals();
}
private Object[][] conversion = { { 1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1 },
{ "M", "CM", "D", "CD", "C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I" } };
public void processRomanNumerals() {
Scanner scanner = new Scanner(System.in);
String inputRomanNumeral = readRomanNumeral(scanner);
while (!inputRomanNumeral.equals("QUIT")) {
int value = convertToDecimal(inputRomanNumeral);
if (value < 0) {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " contains invalid characters.");
} else {
String calculatedRomanNumeral = convertToRoman(value);
if (inputRomanNumeral.equals(calculatedRomanNumeral)) {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " is valid.");
} else {
System.out.println("The input Roman numeral " +
inputRomanNumeral + " is invalid.");
System.out.println("The correct Roman numeral is " +
calculatedRomanNumeral + ".");
}
System.out.println("The decimal value is " + value + ".");
}
inputRomanNumeral = readRomanNumeral(scanner);
}
scanner.close();
}
private String readRomanNumeral(Scanner scanner) {
System.out.print("Enter a Roman numeral: ");
return scanner.nextLine().trim().toUpperCase();
}
private int convertToDecimal(String input) {
int output = 0;
int index = 0;
while (index < input.length()) {
boolean isInvalid = true;
for (int i = 0; i < conversion[1].length; i++) {
String test = (String) conversion[1][i];
int j = index + test.length();
if ((j <= input.length()) &&
(input.substring(index, j).equals(test))) {
output += (Integer) conversion[0][i];
index = j;
isInvalid = false;
break;
}
}
if (isInvalid) {
return -1;
}
}
return output;
}
private String convertToRoman(int input) {
String output = "";
for (int i = 0; i < conversion[0].length; i++) {
int value = (Integer) conversion[0][i];
if (input >= value) {
output += (String) conversion[1][i];
input -= value;
i--;
}
}
return output;
}
}
I am new at coding and now I am learning Java. I tryed to write something like calculator. I wrote it with switch case but then I realized I must take all inputs in single line. For example in this code I took 3 inputs but in 3 different lines. But I must take 2 input and 1 char in single line. First first number second char and then third number. Can you help me ?
Public static void main(String[] args) {
int opr1,opr2,answer;
char opr;
Scanner sc =new Scanner(System.in);
System.out.println("Enter first number");
opr1=sc.nextInt();
System.out.println("Enter operation for");
opr=sc.next().charAt(0);
System.out.println("Enter second number");
opr2=sc.nextInt();
switch (opr){
case '+':
answer=opr1+opr2;
System.out.println("The answer is: " +answer);
break;
case '-':
answer=opr1-opr2;
System.out.println("The answer is: " +answer);
break;
case '*':
answer=opr1*opr2;
System.out.println("The answer is: " +answer);
break;
case '/':
if(opr2>0) {
answer = opr1 / opr2;
System.out.println("The answer is: " + answer);
}
else {
System.out.println("You can't divide to zero");
}
break;
default:
System.out.println("Unknown command");
break;
}
Try following way
System.out.print("Enter a number then operator then another number : ");
String input = scanner.nextLine(); // get the entire line after the prompt
String[] sum = input.split(" ");
Here numbers and operator separated by "space". Now, you can call them by sum array.
int num1 = Integer.parseInt(sum[0]);
String operator = sum[1]; //They are already string value
int num2 = Integer.parseInt(sum[2]);
Then, you can do as you did than.
You can try something like this:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter number, operation and number. For example: 2+2");
String value = scanner.next();
Character operation = null;
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
Character c = value.charAt(i);
// If operation is null, the digits belongs to the first number.
if (operation == null && Character.isDigit(c)) {
a.append(c);
}
// If operation is not null, the digits belongs to the second number.
else if (operation != null && Character.isDigit(c)) {
b.append(c);
}
// It's not a digit, therefore it's the operation itself.
else {
operation = c;
}
}
Integer aNumber = Integer.valueOf(a.toString());
Integer bNumber = Integer.valueOf(b.toString());
// Switch goes here...
}
Note: didn't validate input here.
How do I make my output for Binary to Decimal this: Choice: 1 Binary Number : 2222 INVALID Binary Number
If a user types the binary above 1.
Also, how should I make the default of the switch case not show unless the user inputs the wrong option. Example: They input "HOOPdoop" which is not on the menu screen, the default will output saying that they must try again with the correct input. However, I don't want the default to output when the user inputs "Octal to Decimal" / after the calculation.
here is my code:
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//VARIABLES BEFOR LOOP / FOR LOOP
boolean Loop = true;
String Choice;
//OBJECTS
Scanner input = new Scanner(System.in);
while (Loop == true) {
//MENU
System.out.println("This program will convert...\n" + " \n" + "Binary to Decimal\n" + "Octal to Decimal\n" + "Decimal to Binary\n" + "Decimal to Octal\n" + "or you may Exit");
//ASK
System.out.println("--------------------------------------------");
System.out.println("Which converter would you like to use? or would you like to exit? : ");
Choice = input.nextLine();
//VARIABLES INSIDE LOOP
String binary = "";
int decimal;
String octal;
switch (Choice) {
case "Binary to Decimal":
System.out.print("Enter a binary number: ");
binary = input.nextLine();
if (binary.equals(2)) {
System.out.println("INVALID.");
} else {
System.out.println("Decimal: " + Integer.parseInt(binary, 2));
System.out.println("-----------------------------------------");
}
break;
case "Decimal to Binary":
System.out.print("Enter decimal number: ");
decimal = input.nextInt();
int answer = decimal;
while (answer > 0) {
binary = (answer % 2) + binary;
answer = answer / 2;
}
System.out.println("The binary number = " + binary);
System.out.println("-----------------------------------------");
break;
case "Decimal to Octal":
System.out.print("Enter decimal number: ");
decimal = input.nextInt();
octal = Integer.toOctalString(decimal);
System.out.println("Octal number = " + octal);
System.out.println("-----------------------------------------");
break;
case "Octal to Decimal":
System.out.print("Enter a Octal number: ");
octal = input.nextLine();
System.out.println("Decimal: " + Integer.parseInt(octal, 8));
System.out.println("-----------------------------------------");
break;
case "Exit":
System.out.println("Goodbye!!");
Loop = false;
break;
}
}
}
}```
So my goal this week was to find the hexadecimal octal and binary for decimal. I was able to get the hexdecimal, binary, and octal but were individual loops on different public class. So i was wondering how could i make this code one and read the hexadecimal, octal, and binary all in one loop.
decimal to hexadecimal
import java.util.Scanner;
public class uncode {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = "";
while (decimal != 0 ) {
int hexValue = decimal % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ?
(char) (hexValue + '0') : (char)(hexValue - 10 + 'A');
hex = hexDigit + hex;
decimal = decimal / 16;
}
System.out.println("The hex number is " + hex);
}
}
decimal to octal
import java.util.Scanner;
public class octal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String octal = "";
while ( decimal > 0 ) {
int remainder = decimal % 8;
octal = remainder + octal;
decimal = decimal / 8;
}
System.out.println("Octal number: " + octal);
}
}
decimal to binary
import java.util.Scanner;
public class GuessNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
System.out.println("Binary number: " + binary);
}
}
Easy way would be to use already present converstions, for example
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = Integer.toHexString(decimal);
String oct = Integer.toOctalString(decimal);
String bin = Integer.toBinaryString(decimal);
If you need an integer value, not the string, you can use
int h = Integer.parseInt(hex, 16);
int o = Integer.parseInt(oct, 8);
int b = Integer.parseInt(bin, 2);
Assuming you don't want to use these methods (let's say you have your reasons).
First, you need to put your code in a method, not inside main.
Then you can do something like this:
public class Class {
public static void uncode() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = "";
while (decimal != 0) {
int hexValue = decimal % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
: (char) (hexValue - 10 + 'A');
hex = hexDigit + hex;
decimal = decimal / 16;
}
System.out.println("The hex number is " + hex);
}
public static void octal() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String octal = "";
while (decimal > 0) {
int remainder = decimal % 8;
octal = remainder + octal;
decimal = decimal / 8;
}
System.out.println("Octal number: " + octal);
}
public static void GuessNumbers() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
System.out.println("Binary number: " + binary);
}
public static void allInOne() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
int hex = decimal;
int oct = decimal;
int bin = decimal;
String hexal = "";
String octal = "";
String binary = "";
while (hex > 0 || oct > 0 || bin > 0) {
if (hex > 0) {
// Get Hexal
int hexValue = hex % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
: (char) (hexValue - 10 + 'A');
hexal = hexDigit + hexal;
hex = hex / 16;
}
if (oct > 0) {
// Get Octal
int remainder = oct % 8;
octal = remainder + octal;
oct = oct / 8;
}
if (bin > 0) {
// Get Binary
int remainder = bin % 2;
binary = remainder + binary;
bin = bin / 2;
}
}
System.out.println("The hex number is " + hexal);
System.out.println("Octal number: " + octal);
System.out.println("Binary number: " + binary);
}
public static void main(String[] args) {
uncode();
octal();
GuessNumbers();
allInOne();
}
}
I tried to make as little changes to your code as possible.
Here i convert from decimal to octal,bineary,hexadecimal by calling method getEveryFromDeci(param1,param2) where param1 - any decimal number and param2- its base value like 8,2,16.
And also i convert octal,bineary,hexadecimal to decimal by calling method allToDeci(param1,param2) where param1 - value of hexadecimal,bineary,octal in string form and param2- base value of hexadecimal
private String getEveryFromDeci(Integer x,Integer y){
List<String> al = deciBin(x,y,new ArrayList<String>());
StringBuffer buffer = new StringBuffer();
for(String s : al)
buffer.append(s);
return buffer.toString();
}
private List<String> deciBin(Integer a,Integer b,List<String> list){
if(a>=b){
deciBin(a/b,b,list);
list.add(a%b > 9 ? getHexaDecimal(a%b):Integer.toString(a%b));
}else
list.add(Integer.toString(a));
return list;
}
private String getHexaDecimal(int d){
String s= null;
switch(d){
case 10:
s="A";
break;
case 11:
s="B";
break;
case 12:
s="C";
break;
case 13:
s="D";
break;
case 14:
s="E";
break;
case 15:
s="F";
break;
}
return s;
}
private int allToDeci(String applyNum,int type){
int sum =0;
char[] ch = applyNum.toCharArray();
for(int pum=0;pum<ch.length;pum++)
sum += Character.isDigit(ch[pum]) ? getAct(ch.length-(pum+1),type) * Character.getNumericValue(ch[pum]) :getAct(ch.length-(pum+1),type) * getNum(ch[pum]);
return sum;
}
private int getNum(char ch){
int num = 0;
switch(ch){
case 'A':
num =10;
break;
case 'B':
num = 11;
break;
case 'C':
num =12;
break;
case 'D':
num =13;
break;
case 'E':
num =14;
break;
case 'F':
num=15;
break;
default:
num =Character.getNumericValue(ch);
break;
}
return num;
}
private int getAct(int k,int p){
int s=1;
if(k >0){
for(int i=0;i<k;i++)
s *=p;
return s;
}else
return 1;
}