Converting some hexadecimal string to a decimal integer - java

I wrote some code to convert my hexadecimal display string to decimal integer. However, when input is something like 100a or 625b (something with a letter) I got an error like this:
java.lang.NumberFormatException: For input string: " 100a" at
java.lang.NumberFormatException.forInputString(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source)
How can I convert my string with letters to a decimal integer?
if(display.getText() != null)
{
if(display.getText().contains("a") || display.getText().contains("b") ||
display.getText().contains("c") || display.getText().contains("d") ||
display.getText().contains("e") || display.getText().contains("f"))
{
temp1 = Integer.parseInt(display.getText(), 16);
temp1 = (double) temp1;
}
else
{
temp1 = Double.parseDouble(String.valueOf(display.getText()));
}
}

It looks like there's an extra (leading) space character in your string (" 100a"). You can use trim() to remove leading and trailing whitespaces:
temp1 = Integer.parseInt(display.getText().trim(), 16);
Or if you think the presence of a space means there's something else wrong, you'll have to look into it yourself, since we don't have the rest of your code.

public static int hex2decimal(String s) {
String digits = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int d = digits.indexOf(c);
val = 16*val + d;
}
return val;
}
That's the most efficient and elegant solution I have found on the Internet. Some of the other solutions provided here didn't always work for me.

//package com.javatutorialhq.tutorial;
import java.util.Scanner;
/* Java code to convert hexadecimal to decimal */
public class HexToDecimal {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Hexadecimal Input: ");
// Read the hexadecimal input from the console
Scanner s = new Scanner(System.in);
String inputHex = s.nextLine();
try {
// Actual conversion of hexadecimal to decimal
Integer outputDecimal = Integer.parseInt(inputHex, 16);
System.out.println("Decimal Equivalent: " + outputDecimal);
}
catch(NumberFormatException ne) {
// Printing a warning message if the input
// is not a valid hexadecimal number
System.out.println("Invalid Input");
}
finally {
s.close();
}
}
}

My way:
private static int hexToDec(String hex) {
return Integer.parseInt(hex, 16);
}

This is my solution:
public static int hex2decimal(String s) {
int val = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int num = (int) c;
val = 256*val + num;
}
return val;
}
For example to convert 3E8 to 1000:
StringBuffer sb = new StringBuffer();
sb.append((char) 0x03);
sb.append((char) 0xE8);
int n = hex2decimal(sb.toString());
System.out.println(n); //will print 1000.

You can use this method to get the digit:
public int digitToValue(char c) {
(c >= '&' && c <= '9') return c - '0';
else if (c >= 'A' && c <= 'F') return 10 + c - 'A';
else if (c >= 'a' && c <= 'f') return 10 + c - 'a';
return -1;
}

void htod(String hexadecimal)
{
int h = hexadecimal.length() - 1;
int d = 0;
int n = 0;
for(int i = 0; i<hexadecimal.length(); i++)
{
char ch = hexadecimal.charAt(i);
boolean flag = false;
switch(ch)
{
case '1': n = 1; break;
case '2': n = 2; break;
case '3': n = 3; break;
case '4': n = 4; break;
case '5': n = 5; break;
case '6': n = 6; break;
case '7': n = 7; break;
case '8': n = 8; break;
case '9': n = 9; break;
case 'A': n = 10; break;
case 'B': n = 11; break;
case 'C': n = 12; break;
case 'D': n = 13; break;
case 'E': n = 14; break;
case 'F': n = 15; break;
default : flag = true;
}
if(flag)
{
System.out.println("Wrong Entry");
break;
}
d = (int)(n*(Math.pow(16,h))) + (int)d;
h--;
}
System.out.println("The decimal form of hexadecimal number "+hexadecimal+" is " + d);
}

Since there is no brute-force approach which (done with it manualy). To know what exactly happened.
Given a hexadecimal number
KₙKₙ₋₁Kₙ₋₂....K₂K₁K₀
The equivalent decimal value is:
Kₙ * 16ₙ + Kₙ₋₁ * 16ₙ₋₁ + Kₙ₋₂ * 16ₙ₋₂ + .... + K₂ * 16₂ + K₁ * 16₁ + K₀ * 16₀
For example, the hex number AB8C is:
10 * 16₃ + 11 * 16₂ + 8 * 16₁ + 12 * 16₀ = 43916
Implementation:
//convert hex to decimal number
private static int hexToDecimal(String hex) {
int decimalValue = 0;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
}
return decimalValue;
}
private static int hexCharToDecimal(char character) {
if (character >= 'A' && character <= 'F')
return 10 + character - 'A';
else //character is '0', '1',....,'9'
return character - '0';
}

You could take advantage of ASCII value for each letter and take off 55, easy and fast:
int asciiOffset = 55;
char hex = Character.toUpperCase('A'); // Only A-F uppercase
int val = hex - asciiOffset;
System.out.println("hexadecimal:" + hex);
System.out.println("decimal:" + val);
Output:
hexadecimal:A
decimal:10

A much simpler way is to use BigInteger, like so:
BigInteger("625b", 16)

Scanner sc = new Scanner(System.in);
System.out.println("Enter the value");
String s = sc.next();
//String s = "AD";
String s1 = s.toUpperCase();
int power = 0;
double result = 0;
char[] c = s1.toCharArray();
for (int i = c.length-1; i >=0 ; i--) {
boolean a = true;
switch(c[i]){
case 'A': c[i] = 10; a = false; break;
case 'B': c[i] = 11; a = false; break;
case 'C': c[i] = 12; a = false; break;
case 'D': c[i] = 13; a = false; break;
case 'E': c[i] = 14; a = false; break;
case 'F': c[i] = 15; a = false; break;
}
if(a==true){
result = result + (c[i]-48) * Math.pow(16, power++);
}else {
result = result + (c[i]) * Math.pow(16, power++);
}
}
System.out.println(result);

This is a little library that should help you with hexadecimals in Java: https://github.com/PatrykSitko/HEX4J
It can convert from and to hexadecimals. It supports:
byte
boolean
char
char[]
String
short
int
long
float
double (signed and unsigned)
With it, you can convert your String to hexadecimal and the hexadecimal to a float/double.
Example:
String hexValue = HEX4J.Hexadecimal.from.String("Hello World");
double doubleValue = HEX4J.Hexadecimal.to.Double(hexValue);

Use:
public class Hex2Decimal {
public static void hexDec(String num)
{
int sum = 0;
int newnum = 0;
String digit = num.toUpperCase();
for(int i=0; i<digit.length(); i++)
{
char c = digit.charAt(digit.length()-i-1);
if(c == 'A')
{
newnum = 10;
}
else if(c == 'B')
{
newnum = 11;
}
if(c == 'C')
{
newnum = 12;
}
if(c == 'D')
{
newnum = 13;
}
if(c == 'E')
{
newnum = 14;
}
if(c == 'F')
{
newnum = 15;
}
else
{
newnum = Character.getNumericValue(c);
}
sum = (int) (sum + newnum*Math.pow(16, i));
}
System.out.println(" HexaDecimal to Decimal conversion is" + sum);
}
public static void main(String[] args) {
hexDec("9F");
}
}

Related

How to convert from base 10 to base x in java?

What i've done so far:
public static String convert(int base, int target , String number) {
ArrayList<Integer> numbers= new ArrayList<Integer>();
for(int i=0;i<number.length();i++) {
char check=number.charAt(i);
if(check>='A') {
numbers.add(Character.getNumericValue(check-'A'+1));
}
else {
numbers.add(Character.getNumericValue(check));
}
}
int answer_10 = 0;
for(int i=0;i<number.length();i++) {
answer_10 += Math.pow(numbers.get(number.length()-i-1), base);
}
String answer_target ="";
while(answer_10>0) { // I need help on this part
for(int i=0;i>0;i++) {
if(9*Math.pow(target, i)-answer_10<0) {
i++;
}
else {
for(int j=9;j<=0;j--) {
if(j*Math.pow(target, i)-answer_10<0) {
j-=1;
if(j<=10) {
answer_target += j*Math.pow(target, i);
}
else {
answer_target += j-'A'+1 ;
}
answer_10 -= j*Math.pow(target, i);
break;
}
}
}
}
}
return answer_target;
}
I need help on the part that converts the number from base 10 to base x as a string.
The question limits 2<=x<=20.
I couldn't use the built-in converting function by java, as the question asked not to.
Math.pow isn't a good choice for this problem. It is better to use simple multiply /divide cycle.
public static String convert(int base, int target, String number) {
if (base < 2 || base > 20)
throw new IllegalArgumentException("Invalid base "+base);
if (target < 2 || target > 20)
throw new IllegalArgumentException("Invalid base "+target);
int numberStrLen = number.length();
int numberValue = 0;
for (int i = 0; i < numberStrLen; i++) {
char ch = Character.toLowerCase(number.charAt(i));
int digit;
if (ch >= '0' && ch <= '9')
digit = ch - '0';
else if (ch >= 'a')
digit = ch - 'a' + 10;
else
throw new IllegalArgumentException("Invalid character "+ch+" for base "+base);
if (digit >= base)
throw new IllegalArgumentException("Invalid character "+ch+" for base "+base);
numberValue = numberValue * base + digit; // <= multiply cycle
}
if (numberValue < 0)
throw new IllegalArgumentException("Signed value: "+numberValue+" for "+number);
StringBuilder sb = new StringBuilder();
while (numberValue != 0) {
int digit = numberValue % target;
numberValue = numberValue / target; // <= divide cycle
char ch;
if (digit < 10)
ch = (char)(digit + '0');
else
ch = (char)(digit - 10 + 'a');
sb.insert(0, ch);
}
if (sb.length() == 0)
return "0";
else
return sb.toString();
}
And don't forget about int overflow (wraparound ). Maybe use long or even BigInteger?

Algorithm to check consecutive letters in a QWERTY keyboard

I need to write an algorithm in Java to validate a password field.
Between all the basic controls (minimum length, lunghezza massima, alphanumeric...) I gotta check that the password is not composed of characters in sequence from the keyboard (eg QWERTY, YTREWQ, ASDFGH, etc.).
The minimum password length is 8 characters. Password is disqualified after 4 digits
I cannot create a "dictionary-table" in my DB with the "forbidden" strings, any tip about an existing algorithm or if there's a library already doing a check like this?
Hmm... One thing comes to my mind.
You can try to construct a matrix of letters:
-----------------------
| q w e r t y ...
| a s d f g h ... etc.
| z x c v b n ...
-----------------------
Give each letter an index. For instance, 'q' has [1,1], 'w' has [2, 1], 'v' has [4,3] and so on.
Now, given a String, find the distance between each letter and sum them.
Example:
Given a string "sdgh":
s-d, distance is 1
d-g, distance is 2
g-h, distance is 1
Total distance is: 4
Now you have a ratio of 4/4. (Four letters, the total distance is four).
Is that ok with your security requirements? Answer this question and you're done.
Ok here is my solution, it seems to work quite well for horizontal check. If distance is > 4 it means the password is not valid. Any tip to improve it? I know it's a bit hard-coded but at the moment i can pass on it.
#Test
public void runCheckDigit() {
checkdigit("cnqwerty13");
}
public void checkdigit(String password) {
int x = 4;
int y = 10;
String[][] keyboard = new String[x][y];
// first row
keyboard[0][0] = "1";
keyboard[0][1] = "2";
keyboard[0][2] = "3";
keyboard[0][3] = "4";
keyboard[0][4] = "5";
keyboard[0][5] = "6";
keyboard[0][6] = "7";
keyboard[0][7] = "8";
keyboard[0][8] = "9";
keyboard[0][9] = "0";
// second row
keyboard[1][0] = "q";
keyboard[1][1] = "w";
keyboard[1][2] = "e";
keyboard[1][3] = "r";
keyboard[1][4] = "t";
keyboard[1][5] = "y";
keyboard[1][6] = "u";
keyboard[1][7] = "i";
keyboard[1][8] = "o";
keyboard[1][9] = "p";
// TERZA RIGA
keyboard[2][0] = "a";
keyboard[2][1] = "s";
keyboard[2][2] = "d";
keyboard[2][3] = "f";
keyboard[2][4] = "g";
keyboard[2][5] = "h";
keyboard[2][6] = "j";
keyboard[2][7] = "k";
keyboard[2][8] = "l";
keyboard[2][9] = "ò";
// third row
keyboard[3][0] = "z";
keyboard[3][1] = "x";
keyboard[3][2] = "c";
keyboard[3][3] = "v";
keyboard[3][4] = "b";
keyboard[3][5] = "n";
keyboard[3][6] = "m";
keyboard[3][7] = ",";
keyboard[3][8] = ".";
keyboard[3][9] = "-";
printMatrix(keyboard, x, y);
List<Coordinata> cList = new ArrayList<Coordinata>();
for (int i = 0, n = password.length(); i < n; i++) {
char s = password.toLowerCase().charAt(i);
cList.add(getCoordinate(s, keyboard));
}
int distanza = 0;
for (int i = 0; i < cList.size()-1; i++) {
distanza = distanza + distanceElement(cList.get(i), cList.get(i + 1));
}
System.out.println("distanza : " + distanza);
}
private static Coordinata getCoordinate(char s, String[][] keyboard) {
Coordinata c = null;
for (int i = 0; i < keyboard.length; ++i) {
for (int j = 0; j < keyboard[0].length; ++j) {
if (keyboard[i][j].equals(Character.toString(s))) {
// Found the correct i,j
return c = new Coordinata(i, j, Character.toString(s));
}
}
}
return c;
}
private static int distanceElement(Coordinata c1, Coordinata c2) {
int distance = 0;
distance = Math.abs(c2.getX() - c1.getX()) + Math.abs(c2.getY() - c1.getY());
System.out.println("Distance: " + c1.getLettera() + "->" + c2.getLettera() + " = " + distance);
return distance;
}
private static void printMatrix(String[][] matrix, int matrixRow, int matrixCol) {
System.out.println("Matrix is : ");
for (int i = 0; i < matrixRow; i++) {
for (int j = 0; j < matrixCol; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}

How to get 2's complement of a binary number in Java programmatically

How to calculate the 2's Complement of a Hex number in Android/Java.
For Example :
String x = 10011010;
1's complement of x = 01100101;
2's complement is 01100110;
How I can pro-grammatically achieve in Java?
I had tried the following code to convert the binary to its 1's compliment:
public String complementFunction(String bin) {
String ones = "";
for (int i = 0; i < bin.length(); i++) {
ones += flip(bin.charAt(i));
}
return ones;
}
// Returns '0' for '1' and '1' for '0'
public char flip(char c) {
return (c == '0') ? '1' : '0';
}
But I'm not able to get its two's complement.
Thanks for your help everyone.
I got the solution and it is as follows :
public String twosCompliment(String bin) {
String twos = "", ones = "";
for (int i = 0; i < bin.length(); i++) {
ones += flip(bin.charAt(i));
}
int number0 = Integer.parseInt(ones, 2);
StringBuilder builder = new StringBuilder(ones);
boolean b = false;
for (int i = ones.length() - 1; i > 0; i--) {
if (ones.charAt(i) == '1') {
builder.setCharAt(i, '0');
} else {
builder.setCharAt(i, '1');
b = true;
break;
}
}
if (!b)
builder.append("1", 0, 7);
twos = builder.toString();
return twos;
}
// Returns '0' for '1' and '1' for '0'
public char flip(char c) {
return (c == '0') ? '1' : '0';
}
Thanks to all for helping.
This wikipedia section explains an easy way to get the 2's complement: Get the 1's complement, then add 1 (in binary logic). So you can use the complementFunction you already have, then go through the String backwards. If you find a 1, flip it and continue. If you find a 0, flip it and stop.
String twos = "";
for (int i = bin.length() - 1; i >= 0; i--) {
if (bin.charAt(i) == '1') {
twos = "0" + twos;
} else {
twos = bin.substring(0, i) + "1" + two;
break;
}
twos = flip(bin.charAt(i));
}
return twos;
#BackSlash's comment above is intriguing. This answer is the full program written based on this idea:
import java.time.temporal.ValueRange;
import java.util.Scanner;
//This program generates convert decimal to binary
public class ConvertDecimalToBinary {
public static int getNumberOfBytes(int n) {
int bytes = 0;
ValueRange byteRange = ValueRange.of(Byte.MIN_VALUE, Byte.MAX_VALUE);
ValueRange shortRange = ValueRange.of(Short.MIN_VALUE, Short.MAX_VALUE);
ValueRange intRange = ValueRange.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
if (byteRange.isValidValue(n)) {
bytes = 1;
} else if (shortRange.isValidValue(n)) {
bytes = 2;
} else if (intRange.isValidValue(n)) {
bytes = 4;
}
return bytes;
}
//Convert a positive decimal number to binary
public static String convertPositiveNumberToBinary(int n, int bytes,boolean reverse) {
int bits = 8 * bytes;
StringBuilder sb = new StringBuilder(bits); //in-bits
if (n == 0) {
sb.append("0");
} else {
while (n > 0) {
sb.append(n % 2);
n >>= 1; //aka n/2
}
}
if (sb.length() < bits) {
for (int i = sb.length(); i < bits; i++) {
sb.append("0");
}
}
if (reverse) {
return sb.toString();
} else {
return sb.reverse().toString();
}
}
//Convert negative decimal number to binary
public static String convertNegativeNumberToBinary(int n, int bytes) {
int m = -n; //conver to positve
String binary = convertPositiveNumberToBinary(m,bytes,true);
int len = binary.length();
StringBuilder sb = new StringBuilder(len); //in-bits
boolean foundFirstOne = false;
for(int i=0; i < len;i++) {
if(foundFirstOne) {
if(binary.charAt(i) == '1') {
sb.append('0');
}
else {
sb.append('1');
}
}
else {
if(binary.charAt(i) == '1') {
foundFirstOne = true;
}
sb.append(binary.charAt(i));
}
}
return sb.reverse().toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()) {
int n = scanner.nextInt();
int bytes = getNumberOfBytes(n);
String binary;
if(n >= 0) {
binary = convertPositiveNumberToBinary(n,bytes,false);
}
else {
binary = convertNegativeNumberToBinary(n,bytes);
}
System.out.println(String.format("Binary representation of {%s} is {%s}",n,binary));
}
scanner.close();
}
}

Converting Roman Numerals To Decimal

I have managed to get my code to convert most Roman numerals to its appropriate decimal value. But it doesn't work for some exceptional cases. Example : XCIX = 99 but my code prints 109.
Here is my code.
public static int romanConvert(String roman)
{
int decimal = 0;
String romanNumeral = roman.toUpperCase();
for(int x = 0;x<romanNumeral.length();x++)
{
char convertToDecimal = roman.charAt(x);
switch (convertToDecimal)
{
case 'M':
decimal += 1000;
break;
case 'D':
decimal += 500;
break;
case 'C':
decimal += 100;
break;
case 'L':
decimal += 50;
break;
case 'X':
decimal += 10;
break;
case 'V':
decimal += 5;
break;
case 'I':
decimal += 1;
break;
}
}
if (romanNumeral.contains("IV"))
{
decimal-=2;
}
if (romanNumeral.contains("IX"))
{
decimal-=2;
}
if (romanNumeral.contains("XL"))
{
decimal-=10;
}
if (romanNumeral.contains("XC"))
{
decimal-=10;
}
if (romanNumeral.contains("CD"))
{
decimal-=100;
}
if (romanNumeral.contains("CM"))
{
decimal-=100;
}
return decimal;
}
It will be good if you traverse in reverse.
public class RomanToDecimal {
public static void romanToDecimal(java.lang.String romanNumber) {
int decimal = 0;
int lastNumber = 0;
String romanNumeral = romanNumber.toUpperCase();
/* operation to be performed on upper cases even if user
enters roman values in lower case chars */
for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal = processDecimal(1000, lastNumber, decimal);
lastNumber = 1000;
break;
case 'D':
decimal = processDecimal(500, lastNumber, decimal);
lastNumber = 500;
break;
case 'C':
decimal = processDecimal(100, lastNumber, decimal);
lastNumber = 100;
break;
case 'L':
decimal = processDecimal(50, lastNumber, decimal);
lastNumber = 50;
break;
case 'X':
decimal = processDecimal(10, lastNumber, decimal);
lastNumber = 10;
break;
case 'V':
decimal = processDecimal(5, lastNumber, decimal);
lastNumber = 5;
break;
case 'I':
decimal = processDecimal(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
System.out.println(decimal);
}
public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if (lastNumber > decimal) {
return lastDecimal - decimal;
} else {
return lastDecimal + decimal;
}
}
public static void main(java.lang.String args[]) {
romanToDecimal("XIV");
}
}
Try this - It is simple and compact and works quite smoothly:
public static int ToArabic(string number) {
if (number == string.Empty) return 0;
if (number.StartsWith("M")) return 1000 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("CM")) return 900 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("D")) return 500 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("CD")) return 400 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("C")) return 100 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("XC")) return 90 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("L")) return 50 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("XL")) return 40 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("X")) return 10 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("IX")) return 9 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("V")) return 5 + ToArabic(number.Remove(0, 1));
if (number.StartsWith("IV")) return 4 + ToArabic(number.Remove(0, 2));
if (number.StartsWith("I")) return 1 + ToArabic(number.Remove(0, 1));
throw new ArgumentOutOfRangeException("something bad happened");
}
assuming the hash looks something like this
Hashtable<Character, Integer> ht = new Hashtable<Character, Integer>();
ht.put('i',1);
ht.put('x',10);
ht.put('c',100);
ht.put('m',1000);
ht.put('v',5);
ht.put('l',50);
ht.put('d',500);
then the logic gets pretty simple going by digit right to left
public static int rtoi(String num)
{
int intNum=0;
int prev = 0;
for(int i = num.length()-1; i>=0 ; i--)
{
int temp = ht.get(num.charAt(i));
if(temp < prev)
intNum-=temp;
else
intNum+=temp;
prev = temp;
}
return intNum;
}
Following your logic of reducing 2 on IX you should reduce 20 on XC 200 on CM and so on.
Less code, more efficient. Not so clearer, sorry!
public int evaluateRomanNumerals(String roman) {
return (int) evaluateNextRomanNumeral(roman, roman.length() - 1, 0);
}
private double evaluateNextRomanNumeral(String roman, int pos, double rightNumeral) {
if (pos < 0) return 0;
char ch = roman.charAt(pos);
double value = Math.floor(Math.pow(10, "IXCM".indexOf(ch))) + 5 * Math.floor(Math.pow(10, "VLD".indexOf(ch)));
return value * Math.signum(value + 0.5 - rightNumeral) + evaluateNextRomanNumeral(roman, pos - 1, value);
}
Imperative + recursive solutions with validation step and online testing
To avoid useless calculations and to make sure the roman numerals format is correct, we need to check the input with a regular expression.
String regex = "^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$";
Explanation of the regular expression in details
Explanation of the regular expression symbols used
Differences between regex functions matches() and find()
Why is MMMCMXCIX (= 3999) the maximum value with standard roman numerals notation
Imperative solution
public static int romanToDecimal(String s) {
if (s == null || s.isEmpty() || !s.matches("^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"))
return -1;
final Matcher matcher = Pattern.compile("M|CM|D|CD|C|XC|L|XL|X|IX|V|IV|I").matcher(s);
final int[] decimalValues = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
final String[] romanNumerals = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int result = 0;
while (matcher.find())
for (int i = 0; i < romanNumerals.length; i++)
if (romanNumerals[i].equals(matcher.group(0)))
result += decimalValues[i];
return result;
}
try online | try optimized version with comments/explanation online
UPDATE: here are two clever recursive proposals from this thread I solved adding validation step
Recursive solution 1 (original answer)
public class RomanToDecimalConverter {
private static double evaluateNextRomanNumeral(String roman, int pos, double rightNumeral) {
if (pos < 0) return 0;
char ch = roman.charAt(pos);
double value = Math.floor(Math.pow(10, "IXCM".indexOf(ch))) + 5 * Math.floor(Math.pow(10, "VLD".indexOf(ch)));
return value * Math.signum(value + 0.5 - rightNumeral) + evaluateNextRomanNumeral(roman, pos - 1, value);
}
public static int evaluateRomanNumerals(String s) {
if (s == null || s.isEmpty() || !s.matches("^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"))
return -1;
return (int) evaluateNextRomanNumeral(s, s.length() - 1, 0);
}
}
try online
Recursive solution 2 (original answer)
public class RomanToDecimalConverter {
private static int convertRec(String s) {
if (s.isEmpty()) return 0;
if (s.startsWith("M")) return 1000 + convertRec(s.substring(1));
else if (s.startsWith("CM")) return 900 + convertRec(s.substring(2));
else if (s.startsWith("D")) return 500 + convertRec(s.substring(1));
else if (s.startsWith("CD")) return 400 + convertRec(s.substring(2));
else if (s.startsWith("C")) return 100 + convertRec(s.substring(1));
else if (s.startsWith("XC")) return 90 + convertRec(s.substring(2));
else if (s.startsWith("L")) return 50 + convertRec(s.substring(1));
else if (s.startsWith("XL")) return 40 + convertRec(s.substring(2));
else if (s.startsWith("X")) return 10 + convertRec(s.substring(1));
else if (s.startsWith("IX")) return 9 + convertRec(s.substring(2));
else if (s.startsWith("V")) return 5 + convertRec(s.substring(1));
else if (s.startsWith("IV")) return 4 + convertRec(s.substring(2));
else if (s.startsWith("I")) return 1 + convertRec(s.substring(1));
throw new IllegalArgumentException("Unexpected roman numerals");
}
public static int convert(String s) {
if (s == null || s.isEmpty() || !s.matches("^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"))
return -1;
return convertRec(s);
}
}
try online
// Author: Francisco Edmundo
private int translateNumber(String texto) {
int n = 0;
int numeralDaDireita = 0;
for (int i = texto.length() - 1; i >= 0; i--) {
int valor = (int) translateNumber(texto.charAt(i));
n += valor * Math.signum(valor + 0.5 - numeralDaDireita);
numeralDaDireita = valor;
}
return n;
}
private double translateNumber(char caractere) {
return Math.floor(Math.pow(10, "IXCM".indexOf(caractere))) + 5 * Math.floor(Math.pow(10, "VLD".indexOf(caractere)));
}
You can check following code. This code should work on all cases. Also it checks null or empty input and faulty input (Let's say you tried with ABXI)
import java.util.HashMap;
import org.apache.commons.lang3.StringUtils;
public class RomanToDecimal {
private HashMap<Character, Integer> map;
public RomanToDecimal() {
map = new HashMap<>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
}
private int getRomanNumeralValue(char ch) {
if (map.containsKey(ch)) {
return map.get(ch);
}
else {
throw new RuntimeException("Roman numeral string contains invalid characters " + ch);
}
}
public int convertRomanToDecimal(final String pRomanNumeral) {
if (StringUtils.isBlank(pRomanNumeral)) {
throw new RuntimeException("Roman numeral string is either null or empty");
}
else {
int index = pRomanNumeral.length() - 1;
int result = getRomanNumeralValue(pRomanNumeral.charAt(index));
for (int i = index - 1; i >= 0; i--) {
if (getRomanNumeralValue(pRomanNumeral.charAt(i)) >= getRomanNumeralValue(pRomanNumeral.charAt(i + 1))) {
result = result + getRomanNumeralValue(pRomanNumeral.charAt(i));
}
else {
result = result - getRomanNumeralValue(pRomanNumeral.charAt(i));
}
}
return result;
}
}
public static void main(String... args){
System.out.println(new RomanToDecimal().convertRomanToDecimal("XCIX"));
}
}
Full version with error checking and test all valid values in both directions (and some invalid cases).
RomanNumeral.java
import java.util.ArrayList;
import java.util.TreeMap;
/**
* Convert to and from a roman numeral string
*/
public class RomanNumeral {
// used for converting from arabic number
final static TreeMap<Integer, String> mapArabic = new TreeMap<Integer, String>();
// used for converting from roman numeral
final static ArrayList<RomanDigit> mapRoman = new ArrayList<RomanDigit>();
final static int MAX_ARABIC = 3999;
static {
mapArabic.put(1000, "M");
mapArabic.put(900, "CM");
mapArabic.put(500, "D");
mapArabic.put(400, "CD");
mapArabic.put(100, "C");
mapArabic.put(90, "XC");
mapArabic.put(50, "L");
mapArabic.put(40, "XL");
mapArabic.put(10, "X");
mapArabic.put(9, "IX");
mapArabic.put(5, "V");
mapArabic.put(4, "IV");
mapArabic.put(1, "I");
mapRoman.add(new RomanDigit("M", 1000, 3, 1000));
mapRoman.add(new RomanDigit("CM", 900, 1, 90));
mapRoman.add(new RomanDigit("D", 500, 1, 100));
mapRoman.add(new RomanDigit("CD", 400, 1, 90));
mapRoman.add(new RomanDigit("C", 100, 3, 100));
mapRoman.add(new RomanDigit("XC", 90, 1, 9));
mapRoman.add(new RomanDigit("L", 50, 1, 10));
mapRoman.add(new RomanDigit("XL", 40, 1, 9));
mapRoman.add(new RomanDigit("X", 10, 3, 10));
mapRoman.add(new RomanDigit("IX", 9, 1, 0));
mapRoman.add(new RomanDigit("V", 5, 1, 1));
mapRoman.add(new RomanDigit("IV", 4, 1, 0));
mapRoman.add(new RomanDigit("I", 1, 3, 1));
}
static final class RomanDigit {
public final String numeral;
public final int value;
public final int maxConsecutive;
public final int maxNextValue;
public RomanDigit(String numeral, int value, int maxConsecutive, int maxNextValue) {
this.numeral = numeral;
this.value = value;
this.maxConsecutive = maxConsecutive;
this.maxNextValue = maxNextValue;
}
}
/**
* Convert an arabic integer value into a roman numeral string
*
* #param n The arabic integer value
* #return The roman numeral string
*/
public final static String toRoman(int n) {
if (n < 1 || n > MAX_ARABIC) {
throw new NumberFormatException(String.format("Invalid arabic value: %d, must be > 0 and < %d", n, MAX_ARABIC));
}
int leDigit = mapArabic.floorKey(n);
//System.out.println("\t*** floor of " + n + " is " + leDigit);
if (n == leDigit) {
return mapArabic.get(leDigit);
}
return mapArabic.get(leDigit) + toRoman(n - leDigit);
}
/**
* Convert a roman numeral string into an arabic integer value
* #param s The roman numeral string
* #return The arabic integer value
*/
public final static int toInt(String s) throws NumberFormatException {
if (s == null || s.length() == 0) {
throw new NumberFormatException("Invalid roman numeral: a non-empty and non-null value must be given");
}
int i = 0;
int iconsecutive = 0; // number of consecutive same digits
int pos = 0;
int sum = 0;
RomanDigit prevDigit = null;
while (pos < s.length()) {
RomanDigit d = mapRoman.get(i);
if (!s.startsWith(mapRoman.get(i).numeral, pos)) {
i++;
// this is the only place we advance which digit we are checking,
// so if it exhausts the digits, then there is clearly a digit sequencing error or invalid digit
if (i == mapRoman.size()) {
throw new NumberFormatException(
String.format("Invalid roman numeral at pos %d: invalid sequence '%s' following digit '%s'",
pos, s.substring(pos), prevDigit != null ? prevDigit.numeral : ""));
}
iconsecutive = 0;
continue;
}
// we now have the match for the next roman numeral digit to check
iconsecutive++;
if (iconsecutive > d.maxConsecutive) {
throw new NumberFormatException(
String.format("Invalid roman numeral at pos %d: more than %d consecutive occurences of digit '%s'",
pos, d.maxConsecutive, d.numeral));
}
// invalid to encounter a higher digit sequence than the previous digit expects to have follow it
// (any digit is valid to start a roman numeral - i.e. when prevDigit == null)
if (prevDigit != null && prevDigit.maxNextValue < d.value) {
throw new NumberFormatException(
String.format("Invalid roman numeral at pos %d: '%s' cannot follow '%s'",
pos, d.numeral, prevDigit.numeral));
}
// good to sum
sum += d.value;
if (sum > MAX_ARABIC) {
throw new NumberFormatException(
String.format("Invalid roman numeral at pos %d: adding '%s' exceeds the max value of %d",
pos, d.numeral, MAX_ARABIC));
}
pos += d.numeral.length();
prevDigit = d;
}
return sum;
}
}
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("TEST arabic integer => roman numeral string");
for (int i = 0; i<= 4000; i++) {
String s;
try {
s = RomanNumeral.toRoman(i);
}
catch(NumberFormatException ex) {
s = ex.getMessage();
}
System.out.println(i + "\t =\t " + s);
}
System.out.println("TEST roman numeral string => arabic integer");
for (int i = 0; i<= 4000; i++) {
String s;
String msg;
try {
s = RomanNumeral.toRoman(i);
int n = testToInt(s);
assert(i == n); // ensure it is reflexively converting
}
catch (NumberFormatException ex) {
System.out.println(ex.getMessage() + "\t =\t toInt() skipped");
}
}
testToInt("MMMM");
testToInt("XCX");
testToInt("CDC");
testToInt("IVI");
testToInt("XXC");
testToInt("CCD");
testToInt("MDD");
testToInt("DD");
testToInt("CLL");
testToInt("LL");
testToInt("IIX");
testToInt("IVX");
testToInt("IIXX");
testToInt("XCIX");
testToInt("XIWE");
// Check validity via a regexp for laughs
String s = "IX";
System.out.println(s + " validity is " + s.matches("M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})"));
}
private final static int testToInt(String s) {
String msg;
int n=0;
try {
n = RomanNumeral.toInt(s);
msg = Integer.toString(n);
}
catch(NullPointerException | NumberFormatException ex) {
msg = ex.getMessage();
}
System.out.println(s + "\t =\t " + msg);
return n;
}
}
Output
TEST arabic integer => roman numeral string
0 = Invalid arabic value: 0, must be > 0 and < 3999
1 = I
2 = II
3 = III
4 = IV
5 = V
6 = VI
7 = VII
8 = VIII
9 = IX
10 = X
... [snip] ...
3988 = MMMCMLXXXVIII
3989 = MMMCMLXXXIX
3990 = MMMCMXC
3991 = MMMCMXCI
3992 = MMMCMXCII
3993 = MMMCMXCIII
3994 = MMMCMXCIV
3995 = MMMCMXCV
3996 = MMMCMXCVI
3997 = MMMCMXCVII
3998 = MMMCMXCVIII
3999 = MMMCMXCIX
4000 = Invalid arabic value: 4000, must be > 0 and < 3999
TEST roman numeral string => arabic integer
Invalid arabic value: 0, must be > 0 and < 3999 = toInt() skipped
I = 1
II = 2
III = 3
IV = 4
V = 5
VI = 6
VII = 7
VIII = 8
IX = 9
X = 10
... [snip] ...
MMMCMLXXXVIII = 3988
MMMCMLXXXIX = 3989
MMMCMXC = 3990
MMMCMXCI = 3991
MMMCMXCII = 3992
MMMCMXCIII = 3993
MMMCMXCIV = 3994
MMMCMXCV = 3995
MMMCMXCVI = 3996
MMMCMXCVII = 3997
MMMCMXCVIII = 3998
MMMCMXCIX = 3999
Invalid arabic value: 4000, must be > 0 and < 3999 = toInt() skipped
MMMM = Invalid roman numeral at pos 3: more than 3 consecutive occurences of digit 'M'
XCX = Invalid roman numeral at pos 2: 'X' cannot follow 'XC'
CDC = Invalid roman numeral at pos 2: 'C' cannot follow 'CD'
IVI = Invalid roman numeral at pos 2: 'I' cannot follow 'IV'
XXC = Invalid roman numeral at pos 2: invalid sequence 'C' following digit 'X'
CCD = Invalid roman numeral at pos 2: invalid sequence 'D' following digit 'C'
MDD = Invalid roman numeral at pos 2: more than 1 consecutive occurences of digit 'D'
DD = Invalid roman numeral at pos 1: more than 1 consecutive occurences of digit 'D'
CLL = Invalid roman numeral at pos 2: more than 1 consecutive occurences of digit 'L'
LL = Invalid roman numeral at pos 1: more than 1 consecutive occurences of digit 'L'
IIX = Invalid roman numeral at pos 2: invalid sequence 'X' following digit 'I'
IVX = Invalid roman numeral at pos 2: invalid sequence 'X' following digit 'IV'
IIXX = Invalid roman numeral at pos 2: invalid sequence 'XX' following digit 'I'
XCIX = 99
XIWE = Invalid roman numeral at pos 2: invalid sequence 'WE' following digit 'I'
IX validity is true
Lets look at this problem with 3 different scenarios
Scenario 1:
When we see a pattern like the following
'IIIIII' or 'XXXXX' or 'CCCC'
where all characters as the same: We add the value of each characters in the pattern
'IIIIII' gives us '6'
'XXXXX' gives us '50'
'CCCC' gives us '400'
Scenario 2:
When we see any 2 consecutive characters different where first is smaller in value then the 2nd
'IX' or 'XC'
We subtract the value of first from second e.g.
second:'X' gives us '10'
first: 'I' gives us '1'
second - first : 10 - 1 = 9
Scenario 3:
When we see a any 2 consecutive characters different where first is greater in value then the second
'XI' or 'CX'
We add first and second e.g.
second:'I' gives us '10'
first: 'X' gives us '1'
first + second : 10 + 1 = 11
Now we can find the result if we do this recursively.
Here is the java implementation :
//An array to be used for faster comparisons and reading the values
private int[] letters26 = new int[26];
private void init () {
letters26['I' - 'A'] = 1;
letters26['V' - 'A'] = 5;
letters26['X' - 'A'] = 10;
letters26['L' - 'A'] = 50;
letters26['C' - 'A'] = 100;
letters26['D' - 'A'] = 500;
letters26['M' - 'A'] = 1000;
}
public int convertRomanToInteger(String s) {
//Initialize the array
init();
return _convertRomanToInteger(s.toCharArray(), 0);
}
//Recursively calls itself as long as 2 consecutive chars are different
private int _convertRomanToInteger(char[] s, int index) {
int ret = 0;
char pre = s[index];//Char from the given index
ret = _getValue(pre);
//Iterate through the rest of the string
for (int i = index + 1; i < s.length; i++) {
if (compare(s[i], s[i - 1]) == 0) {
//Scenario 1:
//If 2 consecutive chars are similar, just add them
ret += _getValue(s[i]);
} else if (compare(s[i], s[i - 1]) > 0) {
//Scenario 2:
//If current char is greater than the previous e.g IX ('I' s[i - 1] and 'X' s[i - 1])
//We need to calculate starting from 'i' and subtract the calculation ('ret')
//done so far in current call
return _convertRomanToInteger(s, i) - ret;
} else {
//Scenario 3:
//If current char is smaller than the previous e.g XI ('X' s[i - 1] and 'I' s[i - 1])
//We need to calculate starting from 'i' and subtract the result
//from the calculation done so far in current call
return ret + _convertRomanToInteger(s, i);
}
}
return ret;
}
//Helper function for comparison
private int compare(char a, char b) {
return letters26[Character.toUpperCase(a) - 'A']
- letters26[Character.toUpperCase(b) - 'A'];
}
private int _getValue(char c) {
return letters26[Character.toUpperCase(c) - 'A'];
}
Despite the fact that a lot of solutions have been already proposed.
I guess that the following one will be short and clear:
public class RomanToInteger {
public static int romanToInt(String roman) {
final Map<Character, Integer> map = Map.of('I', 1,
'V', 5,
'X', 10,
'L', 50,
'C', 100,
'D', 500,
'M', 1000);
int result = 0;
for (int index = 0; index < roman.length(); index++) {
final int curNumber = map.get(roman.charAt(index));
if (index > 0 && curNumber > map.get(roman.charAt(index - 1))) {
// add current number & remove previous one twice:
// first: we add it before (when it was current number) and removing it for this current number
// second: for correct conversion to roman numbers
result += curNumber - 2 * map.get(roman.charAt(index - 1));
} else {
result += curNumber;
}
}
System.out.printf("%8s -> %4d\n", roman, result);
return result;
}
public static void main(String[] args) {
String[] romans = {"I", "II", "III", "V", "X", "XIV", "XVII", "XX", "XXV",
"XXX", "XXXVIII", "XLIX", "LXIII", "LXXXI", "XCVII", "XCVIII", "XCIX",
"C", "CI", "CCXLVIII", "CCLIII", "DCCXCIX", "MCCCXXV", "MCM", "MM",
"MMCDLVI", "MDCCXV"};
final Instant startTimeIter = Instant.now();
for (String roman : romans) {
romanToInt(roman);
}
final Instant endTimeIter = Instant.now();
System.out.printf("Elapsed time: %d ms\n", Duration.between(startTimeIter, endTimeIter).toMillis());
}
}
Output:
I -> 1
II -> 2
III -> 3
...
MMCDLVI -> 2456
MDCCXV -> 1715
Elapsed time: 101 ms
Logic is quite simple we just go from left to right through roman literal:
if a literal isn't first and the previous one is lower -> we have to add current number to total & extract the previous number twice. First time what we added this at the previous step (when it was current number). Second because it is a rule of roman literals conversion (like IX literal).
otherwise, just add it to the total result.
How about this?
int getdec(const string& input)
{
int sum=0; char prev='%';
for(int i=(input.length()-1); i>=0; i--)
{
if(value(input[i])<sum && (input[i]!=prev))
{ sum -= value(input[i]);
prev = input[i];
}
else
{
sum += value(input[i]);
prev = input[i];
}
}
return sum;
}
Supposing Well-formed Roman numbers:
private static int totalValue(String val)
{
String aux=val.toUpperCase();
int sum=0, max=aux.length(), i=0;
while(i<max)
{
if ((i+1)<max && valueOf(aux.charAt(i+1))>valueOf(aux.charAt(i)))
{
sum+=valueOf(aux.charAt(i+1)) - valueOf(aux.charAt(i));
i+=2;
}
else
{
sum+=valueOf(aux.charAt(i));
i+=1;
}
}
return sum;
}
private static int valueOf(Character c)
{
char aux = Character.toUpperCase(c);
switch(aux)
{
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}
what about this conversion. no switch, no case at all...
P.S. : I use this script from a bash shell
import sys
def RomanToNum(r):
return {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}[r]
#
#
#
EOF = "<"
Roman = sys.argv[1].upper().strip()+EOF
num = 0
i = 0
while True:
this = Roman[i]
if this == EOF:
break
n1 = RomanToNum(this)
next = Roman[i+1]
if next == EOF:
n2 = 0
else:
n2 = RomanToNum(next)
if n1 < n2:
n1 = -1 * n1
num = num + n1
i = i + 1
print num
public class RomInt {
String roman;
int val;
void assign(String k)
{
roman=k;
}
private class Literal
{
public char literal;
public int value;
public Literal(char literal, int value)
{
this.literal = literal;
this.value = value;
}
}
private final Literal[] ROMAN_LITERALS = new Literal[]
{
new Literal('I', 1),
new Literal('V', 5),
new Literal('X', 10),
new Literal('L', 50),
new Literal('C', 100),
new Literal('D', 500),
new Literal('M', 1000)
};
public int getVal(String s) {
int holdValue=0;
for (int j = 0; j < ROMAN_LITERALS.length; j++)
{
if (s.charAt(0)==ROMAN_LITERALS[j].literal)
{
holdValue=ROMAN_LITERALS[j].value;
break;
} //if()
}//for()
return holdValue;
} //getVal()
public int count()
{
int count=0;
int countA=0;
int countB=0;
int lastPosition = 0;
for(int i = 0 ; i < roman.length(); i++)
{
String s1 = roman.substring(i,i+1);
int a=getVal(s1);
countA+=a;
}
for(int j=1;j<roman.length();j++)
{
String s2= roman.substring(j,j+1);
String s3= roman.substring(j-1,j);
int b=getVal(s2);
int c=getVal(s3);
if(b>c)
{
countB+=c;
}
}
count=countA-(2*countB);
return count;
}
void disp()
{
int result=count();
System.out.println("Integer equivalent of "+roman+" = " +result);
}
} //RomInt---BLC
I find the following approach a very intuitive one:
public void makeArray(String romanNumeral){
int[] numberArray = new int[romanNumeral.length()];
for(int i=0; i<romanNumeral.length();i++){
char symbol = romanNumeral.charAt(i);
switch(symbol){
case 'I':
numberArray[i] = 1;
break;
case 'V':
numberArray[i] = 5;
break;
case 'X':
numberArray[i] = 10;
break;
case 'L':
numberArray[i] = 50;
break;
case 'C':
numberArray[i] = 100;
break;
case 'D':
numberArray[i] = 500;
break;
case 'M':
numberArray[i] = 1000;
break;
}
}
calculate(numberArray);
}
public static void calculate(int[] numberArray){
int theNumber = 0;
for(int n=0;n<numberArray.length;n++){
if(n !=numberArray.length-1 && numberArray[n] < numberArray[n+1]){
numberArray[n+1] = numberArray[n+1] - numberArray[n];
numberArray[n] = 0;
}
}
for(int num:numberArray){
theNumber += num;
}
System.out.println("Converted number: " + theNumber);
}
//Bet no one has a smaller and easier logic than this........Open CHALLENGE!!!!!!!
import java.io.*;
class Convertion_practical_q2
{
void Decimal()throws IOException //Smaller code for convertion from roman to decimal
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the number");
String num=in.readLine();
char pos[]={'0','I','V','X','L','C','D','M'};
int l1=7; //l1 is size of pos array
String v[]={"","1","5","10","50","100","500","1000"};
int l=num.length();
int p=0,p1=0,sum=0;
for(int i=l-1;i>=0;i--)
{
char ch=num.charAt(i);
for(int j=1;j<=l1;j++)
{
if(ch==pos[j])
p=j;
}
if(p>=p1)
sum+=Integer.parseInt(v[p]);
else
sum-=Integer.parseInt(v[p]);
//System.out.println("sum ="+sum+"\np="+p+"\np1="+p1);
p1=p;
}
System.out.println(sum);
}
}
Just got it working in Java, nice job guys.
public int getDecimal (String roman) {
int decimal = 0;
int romanNumber = 0;
int prev = 0;
for (int i = roman.length()-1; i >= 0; i--){
romanNumber = hashRomans.get(roman.charAt(i));
if(romanNumber < decimal && romanNumber != prev ){
decimal -= romanNumber;
prev = romanNumber;
} else {
decimal += romanNumber;
prev = romanNumber;
}
}
return decimal;
}
This should work:
import java.io.*;
import java.util.Scanner;
enum RomanToNumber {
i(1), v(5), x(10), l(50), c(100); int value;
RomanToNumber (int p){value = p;}
int getValue(){return value;}
}
public class RomanToInteger {
public static void main(String[] args){
RomanToNumber n;
System.out.println( "Type a valid roman number in lower case" );
String Str = new String(new Scanner(System.in).nextLine());
int n1 = 0, theNo = 0, len = Str.length();
int[] str2No = new int [len];
for(int i=0; i < len; i++){
n = RomanToNumber.valueOf(Str.substring(n1, ++n1));
str2No[i] = n.getValue();
}
for(int j = 0; j < (len-1); j++){
if( str2No[j] >= str2No[j+1] ){ theNo += str2No[j]; }
else{ theNo -= str2No[j]; }
}
System.out.println( theNo += str2No[len-1] );
}
}
Since most of the answers here are in Java, I'm posting the answer in C++ (since I am bored right now and nothing more productive to do :) But please, no downvotes except if code is wrong.
Known-issues = will not handle overflow
Code:
#include <unordered_map>
int convert_roman_2_int(string& str)
{
int ans = 0;
if( str.length() == 0 )
{
return ans;
}
std::unordered_map<char, int> table;
table['I'] = 1;
table['V'] = 5;
table['X'] = 10;
table['L'] = 50;
table['C'] = 100;
table['D'] = 500;
table['M'] = 1000;
ans = table[ str[ str.length() - 1 ] ];
for( int i = str.length() - 2; i >= 0; i--)
{
if(table[ str[i] ] < table[ str[i+1] ] )
{
ans -= table[ str[i] ];
}
else
{
ans += table[ str[i] ];
}
}
return ans;
}
// test code
void run_test_cases_convert_roman_to_int()
{
string roman = "VIII";
int r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
roman = "XX";
r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
roman = "CDX"; //410
r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
roman = "MCMXC"; //1990
r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
roman = "MMVIII"; //2008
r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
roman = "MDCLXVI"; //1666
r = convert_roman_2_int(roman);
cout << roman << " in int is " << r << endl << flush;
}
this is very basic implementation of what you asked and working for all the test cases. If you find anything wrong in it than do mention it,i will try to make it correct also the code is in C++ .
int RomanToInt(string s){
int len = s.length();
map<char,int>mp;
int decimal = 0;
mp['I'] = 1;mp['V'] = 5;mp['X'] = 10;
mp['L'] = 50;mp['C'] = 100;mp['D'] = 500;mp['M'] = 1000;
for(int i = 0; i < len ;i++){
char cur = s[i],fast = s[i+1];
int cur_val = mp[cur],fast_val = mp[fast];
if(cur_val < fast_val){
decimal = decimal - cur_val;
}else{
decimal += cur_val;
}
}
return decimal;
}
public static int convertFromRoman(String romanNumeral)
{
Character[] rnChars = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int[] rnVals = { 1000, 500, 100, 50, 10, 5, 1 };
HashMap<Character, Integer> valueLookup = new HashMap<Character, Integer>();
for (int i = 0; i < rnChars.length;i++)
valueLookup.put(rnChars[i], rnVals[i]);
int retVal = 0;
for (int i = 0; i < romanNumeral.length();i++)
{
int addVal = valueLookup.get(romanNumeral.charAt(i));
retVal += i < romanNumeral.length()-1 &&
addVal < valueLookup.get(romanNumeral.charAt(i+1))?
-addVal:addVal;
}
return retVal;
}
This is a modest variation of the recursive algorithm suggested by Sahtiel:
public static int toArabic(String number) throws Exception {
String[] letras = {"M","CM","D","CD","C","XC","L","XL","X", "IX","V","IV","I"};
int[] valores = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
// here we can do even more business validations like avoiding sequences like XXXXM
if (number==null || number.isEmpty()) {
return 0;
}
for(int i=0; i<letras.length; i++) {
if (number.startsWith(letras[i])) {
return valores[i] + toArabic(number.substring(letras[i].length()));
}
}
throw new Exception("something bad happened");
}
It uses less than 10 effective lines of code.
This scala solution might be useful:
class RomanNumberConverter {
private val toArabic = Map('I' -> 1, 'V' -> 5, 'X' -> 10, 'L' -> 50, 'C' -> 100, 'D' -> 500, 'M' -> 1000)
// assume that correct roman number is provided
def convert(romanNumber: String): Int = {
def convert(rn: StringBuilder, lastDecimal: Int, acc: Int): Int = {
if (rn.isEmpty) acc
else {
val thisDecimal = toArabic(rn.head)
if (thisDecimal > lastDecimal) convert(rn.tail, thisDecimal, acc + thisDecimal - lastDecimal - lastDecimal)
else convert(rn.tail, thisDecimal, acc + thisDecimal)
}
}
val sb = new StringBuilder(romanNumber)
convert(sb.tail, toArabic(sb.head), toArabic(sb.head))
}
}
Solution using tail recursion:
import java.util.LinkedHashMap;
public class RomanNumber {
private final static LinkedHashMap<String, Integer> roman2number = new LinkedHashMap<>(); // preserve key order
static {
roman2number.put("M", 1000);
roman2number.put("CM", 900);
roman2number.put("D", 500);
roman2number.put("CD", 400);
roman2number.put("C", 100);
roman2number.put("XC", 90);
roman2number.put("L", 50);
roman2number.put("XL", 40);
roman2number.put("X", 10);
roman2number.put("IX", 9);
roman2number.put("V", 5);
roman2number.put("IV", 4);
roman2number.put("I", 1);
}
public final static Integer toDecimal(String roman) {
for (String key : roman2number.keySet()) {
if (roman.startsWith(key)) {
if (roman.equals(key)) {
return roman2number.get(key);
}
return roman2number.get(key) + toDecimal(roman.substring(key.length()));
}
}
return 0;
}
}
Testing:
import junitparams.JUnitParamsRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import junitparams.Parameters;
import static org.junit.Assert.assertTrue;
#RunWith(JUnitParamsRunner.class)
public class RomanNumberTest {
#Test
#Parameters({ "1|I", "2|II", "3|III", "4|IV", "5|V", "6|VI", "7|VII", "8|VIII", "9|IX", "10|X",
"11|XI", "12|XII", "13|XIII", "14|XIV", "15|XV", "16|XVI", "17|XVII", "18|XVIII", "19|XIX",
"20|XX", "50|L", "53|LIII", "57|LVII", "40|XL", "49|XLIX", "59|LIX", "79|LXXIX", "100|C", "90|XC", "99|XCIX",
"200|CC", "500|D", "499|CDXCIX", "999|CMXCIX", "2999|MMCMXCIX", "3999|MMMCMXCIX"
})
public void forRomanReturnsNumber(int number, String roman) {
assertTrue(roman + "->" + number, RomanNumber.toDecimal(roman) == (number));
}
}
With only two if-statements:
Check the match of first character on priority and if not matching, match the single first character.
public class Solution {
private static TreeMap<String, Integer> numeralsAndVal;
static{
numeralsAndVal = new TreeMap<>();
numeralsAndVal.put("M", 1000);
numeralsAndVal.put("CM", 900);
numeralsAndVal.put("D", 500);
numeralsAndVal.put("CD", 400);
numeralsAndVal.put("C", 100);
numeralsAndVal.put("XC", 90);
numeralsAndVal.put("L", 50);
numeralsAndVal.put("XL", 40);
numeralsAndVal.put("X", 10);
numeralsAndVal.put("IX", 9);
numeralsAndVal.put("V", 5);
numeralsAndVal.put("IV", 4);
numeralsAndVal.put("I", 1);
}
public int romanToInt(String A) {
if (A.length() == 0) return 0;
if (A.length() >= 2 && numeralsAndVal.containsKey(A.substring(0, 2))){
return numeralsAndVal.get(A.substring(0, 2)) + romanToInt(A.substring(2));
}
return numeralsAndVal.get(A.substring(0, 1)) + romanToInt(A.substring(1));
}
}
For converting TO arabic, it works as follows:
grab the rightmost unchecked character and convert it to its numeric value using the map. If this value is less than the "top" value we've seen so far, then it needs to be subtracting (e.g. IV needs to be 4, not 6), otherwise we add the value to the total and make it the new top value we've seen so far. So, for instance, XIX works like follows: 'X' is checked and comes out to be value of 10. That is higher than the "top" value seen so far, so it becomes "top" and we add 10 to the value. Then go to the next character and "I" is < 10, so subtract
public class RomanNumeral {
private final Map<Integer, String> arabicToRoman = new LinkedHashMap<Integer, String>();
private final Map<String, Integer> romanToArabic = new LinkedHashMap<String, Integer>();
public RomanNumeral() {
arabicToRoman.put(10, "X");
arabicToRoman.put(9, "IX");
arabicToRoman.put(5, "V");
arabicToRoman.put(4, "IV");
arabicToRoman.put(1, "I");
romanToArabic.put("X", 10);
romanToArabic.put("V", 5);
romanToArabic.put("I", 1);
}
public String convertToRomanNumeral(int number) {
String result = "";
for (Integer i : arabicToRoman.keySet()) {
while (number >= i) {
result += arabicToRoman.get(i);
number -= i;
}
}
return result;
}
public String convertToArabicNumber(String romanNumeral) {
int result = 0;
int top = 0;
for (int i = romanNumeral.length() - 1; i >= 0; i--) {
char current = romanNumeral.charAt(i);
int value = romanToArabic.get("" + current);
if (value < top) {
result -= value;
} else {
result += value;
top = value;
}
}
return "" + result;
}
}
An easy solution for input in the range from 1 to 3999.
private static final Map<String, Integer> MAPPING = new HashMap<>() {{
put("I", 1);
put("V", 5);
put("X", 10);
put("L", 50);
put("C", 100);
put("D", 500);
put("M", 1000);
put("IV", 4);
put("IX", 9);
put("XL", 40);
put("XC", 90);
put("CD", 400);
put("CM", 900);
}};
public static int romanToInt(String s) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
if (i < s.length() - 1 && MAPPING.containsKey(String.valueOf(s.charAt(i)) + s.charAt(i + 1))) {
sum += MAPPING.get(String.valueOf(s.charAt(i)) + s.charAt(i + 1));
i++;
} else {
sum += MAPPING.get(String.valueOf(s.charAt(i)));
}
}
return sum;
}
The problem here is that numbers like 9 are presented in the following way
IX = 9
if we take the literal conversion by getting the sum for each number than our program would give us the following sum
I = 1
X = 10
X + I = 11
which is not correct.
We can overcome this if we simply manipulate the Roman String.
In example:
IX = 9 would be VIIII which is 5 + 1 + 1 + 1 + 1
Therefore if we replace all the special cases in the given String, we can calculate the sum for each character.
Hence;
class Solution {
public int romanToInt(String s) {
// Key Value pairs
Map<Character, Integer> map = new HashMap<>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
// convert these to single characters
s = s.replace("IV","IIII");
s = s.replace("IX","VIIII");
s = s.replace("XL","XXXX");
s = s.replace("XC","LXXXX");
s = s.replace("CD","CCCC");
s = s.replace("CM","DCCCC");
// total sum to be returned
int sum = 0;
// convert s to c character array
char[] c = s.toCharArray();
// iterate through c array and sum the numbers
for(Character ch : c){
sum = sum + map.get(ch);
}
return sum;
}
}
Here is my one...
import java.util.Scanner;
class Solution {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String num;
// System.out.println("enter the number you want to convert from roman to integer.");
num = "D";
System.out.println("length of string is " + num.length());
System.out.println("the integer number is :" + romanToInt(num) );
}
public static int romanToInt(String s) {
char I,V, X, L,C, D, M;
char c1,c3, c2 = s.charAt(0);
// System.out.println("the c2 is : " + (int)c2);
int num = 0, num1 = 0;
int j =0, k = 0, temp = 0;
if (c2 == 'I') {
k = (int)c2 - 72;
System.out.println("k is I" + k);
} else if(c2 == 'V') {
k = (int)c2 - 81;
System.out.println("K is V" + k);
// return 86 - 81;
} else if (c2 == 'X') {
k = (int)c2 - 78;
System.out.println("K is X" + k);
} else if (c2 == 'L') {
k = (int)c2 - 26;
System.out.println("K is L" + k);
} else if (c2 == 'C') {
k = (int)c2 + 33;
System.out.println("K is C" + k);
} else if (c2 == 'D') {
k = (int)c2 + 432;
System.out.println("K is D" + k);
} else if ( c2 == 'M') {
k = (int)c2 + 923;
System.out.println("K is M" + k);
}
if (s.length() == 1){
num = k;
} else {
for(int i = 1; i<= s.length()-1 ; i++) {
System.out.println("i is : " + i);
c1 = s.charAt(i);
if (i == s.length() - 1) {
temp = 0;
} else {
c3 = s.charAt(i+1);
if (c3 == 'I') {
temp = (int)c3 - 72;
System.out.println("temp is I " + temp);
} else if(c3 == 'V') {
temp = (int)c3 - 81;
System.out.println("temp is I " + temp);
// return 86 - 81;
} else if (c3 == 'X') {
temp = (int)c3 - 78;
System.out.println("temp is I " + temp);
} else if (c3 == 'L') {
temp = (int)c3 - 26;
System.out.println("temp is I " + temp);
} else if (c3 == 'C') {
temp = (int)c3 + 33;
System.out.println("temp is I " + temp);
} else if (c3 == 'D') {
temp = (int)c3 + 432;
System.out.println("temp is I " + temp);
} else if ( c3 == 'M') {
temp = (int)c3 + 923;
System.out.println("temp is I " + temp);
}
}
if (c1 == 'I') {
j = (int)c1 - 72;
System.out.println("j is I " + j);
} else if(c1 == 'V') {
j = (int)c1 - 81;
System.out.println("j is V " + j);
// return 86 - 81;
} else if (c1 == 'X') {
j = (int)c1 - 78;
System.out.println("j is X " + j);
} else if (c1 == 'L') {
j = (int)c1 - 26;
System.out.println("j is L " + j);
} else if (c1 == 'C') {
j = (int)c1 + 33;
System.out.println("j is C " + j);
} else if (c1 == 'D') {
j = (int)c1 + 432;
System.out.println("j is D " + j);
} else if ( c1 == 'M') {
j = (int)c1 + 923;
System.out.println("j is M " + j);
}
if ( k < j && j>temp ) {
k = j - k ;
num = num + k;
} else if (j==k || j<k || j<temp){
num = num + k ;
// k = j;
}
if (j>k ) {
k = temp;
i += 1;
if (i == s.length()-1) {
num = num + k;
}
} else {
k = j;
if (i == s.length()-1) {
num = num + k;
}
}
}
}
return num;
}
}

Adding binary numbers

Does anyone know how to add 2 binary numbers, entered as binary, in Java?
For example, 1010 + 10 = 1100.
Use Integer.parseInt(String, int radix).
public static String addBinary(){
// The two input Strings, containing the binary representation of the two values:
String input0 = "1010";
String input1 = "10";
// Use as radix 2 because it's binary
int number0 = Integer.parseInt(input0, 2);
int number1 = Integer.parseInt(input1, 2);
int sum = number0 + number1;
return Integer.toBinaryString(sum); //returns the answer as a binary value;
}
To dive into fundamentals:
public static String binaryAddition(String s1, String s2) {
if (s1 == null || s2 == null) return "";
int first = s1.length() - 1;
int second = s2.length() - 1;
StringBuilder sb = new StringBuilder();
int carry = 0;
while (first >= 0 || second >= 0) {
int sum = carry;
if (first >= 0) {
sum += s1.charAt(first) - '0';
first--;
}
if (second >= 0) {
sum += s2.charAt(second) - '0';
second--;
}
carry = sum >> 1;
sum = sum & 1;
sb.append(sum == 0 ? '0' : '1');
}
if (carry > 0)
sb.append('1');
sb.reverse();
return String.valueOf(sb);
}
Martijn is absolutely correct, to piggyback and complete the answer
Integer.toBinaryString(sum);
would give your output in binary as per the OP question.
You can just put 0b in front of the binary number to specify that it is binary.
For this example, you can simply do:
Integer.toString(0b1010 + 0b10, 2);
This will add the two in binary, and Integer.toString() with 2 as the second parameter converts it back to binary.
The original solution by Martijn will not work for large binary numbers. The below code can be used to overcome that.
public String addBinary(String s1, String s2) {
StringBuilder sb = new StringBuilder();
int i = s1.length() - 1, j = s2.length() -1, carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (j >= 0) sum += s2.charAt(j--) - '0';
if (i >= 0) sum += s1.charAt(i--) - '0';
sb.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) sb.append(carry);
return sb.reverse().toString();
}
public class BinaryArithmetic {
/*-------------------------- add ------------------------------------------------------------*/
static String add(double a, double b) {
System.out.println(a + "first val :" + b);
int a1 = (int) a;
int b1 = (int) b;
String s1 = Integer.toString(a1);
String s2 = Integer.toString(b1);
int number0 = Integer.parseInt(s1, 2);
int number1 = Integer.parseInt(s2, 2);
int sum = number0 + number1;
String s3 = Integer.toBinaryString(sum);
return s3;
}
/*-------------------------------multiply-------------------------------------------------------*/
static String multiply(double a, double b) {
System.out.println(a + "first val :" + b);
int a1 = (int) a;
int b1 = (int) b;
String s1 = Integer.toString(a1);
String s2 = Integer.toString(b1);
int number0 = Integer.parseInt(s1, 2);
int number1 = Integer.parseInt(s2, 2);
int sum = number0 * number1;
String s3 = Integer.toBinaryString(sum);
return s3;
}
/*----------------------------------------substraction----------------------------------------------*/
static String sub(double a, double b) {
System.out.println(a + "first val :" + b);
int a1 = (int) a;
int b1 = (int) b;
String s1 = Integer.toString(a1);
String s2 = Integer.toString(b1);
int number0 = Integer.parseInt(s1, 2);
int number1 = Integer.parseInt(s2, 2);
int sum = number0 - number1;
String s3 = Integer.toBinaryString(sum);
return s3;
}
/*--------------------------------------division------------------------------------------------*/
static String div(double a, double b) {
System.out.println(a + "first val :" + b);
int a1 = (int) a;
int b1 = (int) b;
String s1 = Integer.toString(a1);
String s2 = Integer.toString(b1);
int number0 = Integer.parseInt(s1, 2);
int number1 = Integer.parseInt(s2, 2);
int sum = number0 / number1;
String s3 = Integer.toBinaryString(sum);
return s3;
}
}
Another interesting but long approach is to convert each of the two numbers to decimal, adding the decimal numbers and converting the answer obtained back to binary!
Java solution
static String addBinary(String a, String b) {
int lenA = a.length();
int lenB = b.length();
int i = 0;
StringBuilder sb = new StringBuilder();
int rem = Math.abs(lenA-lenB);
while(rem >0){
sb.append('0');
rem--;
}
if(lenA > lenB){
sb.append(b);
b = sb.toString();
}else{
sb.append(a);
a = sb.toString();
}
sb = new StringBuilder();
char carry = '0';
i = a.length();
while(i > 0){
if(a.charAt(i-1) == b.charAt(i-1)){
sb.append(carry);
if(a.charAt(i-1) == '1'){
carry = '1';
}else{
carry = '0';
}
}else{
if(carry == '1'){
sb.append('0');
carry = '1';
}else{
carry = '0';
sb.append('1');
}
}
i--;
}
if(carry == '1'){
sb.append(carry);
}
sb.reverse();
return sb.toString();
}
public String addBinary(String a, String b) {
int carry = 0;
StringBuilder sb = new StringBuilder();
for(int i = a.length() - 1, j = b.length() - 1;i >= 0 || j >= 0;i--,j--){
int sum = carry + (i >= 0 ? a.charAt(i) - '0':0) + (j >= 0 ? b.charAt(j) - '0' : 0);
sb.append(sum%2);
carry =sum / 2;
}
if(carry > 0) sb.append(carry);
sb.reverse();
return sb.toString();
}
I've actually managed to find a solution to this question without using the stringbuilder() function. Check this out:
public void BinaryAddition(String s1,String s2)
{
int l1=s1.length();int c1=l1;
int l2=s2.length();int c2=l2;
int max=(int)Math.max(l1,l2);
int arr1[]=new int[max];
int arr2[]=new int[max];
int sum[]=new int[max+1];
for(int i=(arr1.length-1);i>=(max-l1);i--)
{
arr1[i]=(int)(s1.charAt(c1-1)-48);
c1--;
}
for(int i=(arr2.length-1);i>=(max-l2);i--)
{
arr2[i]=(int)(s2.charAt(c2-1)-48);
c2--;
}
for(int i=(sum.length-1);i>=1;i--)
{
sum[i]+=arr1[i-1]+arr2[i-1];
if(sum[i]==2)
{
sum[i]=0;
sum[i-1]=1;
}
else if(sum[i]==3)
{
sum[i]=1;
sum[i-1]=1;
}
}
int c=0;
for(int i=0;i<sum.length;i++)
{
System.out.print(sum[i]);
}
}
The idea is same as discussed in few of the answers, but this one is a much shorter and easier to understand solution (steps are commented).
// Handles numbers which are way bigger.
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1;
int j = b.length() -1;
int carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (j >= 0) { sum += b.charAt(j--) - '0' };
if (i >= 0) { sum += a.charAt(i--) - '0' };
// Added number can be only 0 or 1
sb.append(sum % 2);
// Get the carry.
carry = sum / 2;
}
if (carry != 0) { sb.append(carry); }
// First reverse and then return.
return sb.reverse().toString();
}
i tried to make it simple this was sth i had to deal with with my cryptography prj its not efficient but i hope it
public String binarysum(String a, String b){
int carry=0;
int maxim;
int minim;
maxim=Math.max(a.length(),b.length());
minim=Math.min(a.length(),b.length());
char smin[]=new char[minim];
char smax[]=new char[maxim];
if(a.length()==minim){
for(int i=0;i<smin.length;i++){
smin[i]=a.charAt(i);
}
for(int i=0;i<smax.length;i++){
smax[i]=b.charAt(i);
}
}
else{
for(int i=0;i<smin.length;i++){
smin[i]=b.charAt(i);
}
for(int i=0;i<smax.length;i++){
smax[i]=a.charAt(i);
}
}
char[]sum=new char[maxim];
char[] st=new char[maxim];
for(int i=0;i<st.length;i++){
st[i]='0';
}
int k=st.length-1;
for(int i=smin.length-1;i>-1;i--){
st[k]=smin[i];
k--;
}
// *************************** sum begins here
for(int i=maxim-1;i>-1;i--){
char x= smax[i];
char y= st[i];
if(x==y && x=='0'){
if(carry==0)
sum[i]='0';
else if(carry==1){
sum[i]='1';
carry=0;
}
}
else if(x==y && x=='1'){
if(carry==0){
sum[i]='0';
carry=1;
}
else if(carry==1){
sum[i]='1';
carry=1;
}
}
else if(x!=y){
if(carry==0){
sum[i]='1';
}
else if(carry==1){
sum[i]='0';
carry=1;
}
} }
String s=new String(sum);
return s;
}
class Sum{
public int number;
public int carry;
Sum(int number, int carry){
this.number = number;
this.carry = carry;
}
}
public String addBinary(String a, String b) {
int lengthOfA = a.length();
int lengthOfB = b.length();
if(lengthOfA > lengthOfB){
for(int i=0; i<(lengthOfA - lengthOfB); i++){
b="0"+b;
}
}
else{
for(int i=0; i<(lengthOfB - lengthOfA); i++){
a="0"+a;
}
}
String result = "";
Sum s = new Sum(0,0);
for(int i=a.length()-1; i>=0; i--){
s = addNumber(Character.getNumericValue(a.charAt(i)), Character.getNumericValue(b.charAt(i)), s.carry);
result = result + Integer.toString(s.number);
}
if(s.carry == 1) { result += s.carry ;}
return new StringBuilder(result).reverse().toString();
}
Sum addNumber(int number1, int number2, int carry){
Sum sum = new Sum(0,0);
sum.number = number1 ^ number2 ^ carry;
sum.carry = (number1 & number2) | (number2 & carry) | (number1 & carry);
return sum;
}
import java.util.*;
public class BitAddition {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int[] arr1 = new int[len];
int[] arr2 = new int[len];
int[] sum = new int[len+1];
Arrays.fill(sum, 0);
for(int i=0;i<len;i++){
arr1[i] =sc.nextInt();
}
for(int i=0;i<len;i++){
arr2[i] =sc.nextInt();
}
for(int i=len-1;i>=0;i--){
if(sum[i+1] == 0){
if(arr1[i]!=arr2[i]){
sum[i+1] = 1;
}
else if(arr1[i] ==1 && arr2[i] == 1){
sum[i+1] =0 ;
sum[i] = 1;
}
}
else{
if((arr1[i]!=arr2[i])){
sum[i+1] = 0;
sum[i] = 1;
}
else if(arr1[i] == 1){
sum[i+1] = 1;
sum[i] = 1;
}
}
}
for(int i=0;i<=len;i++){
System.out.print(sum[i]);
}
}
}
One of the simple ways is as:
convert the two strings to char[] array and set carry=0.
set the smallest array length in for loop
start for loop from the last index and decrement it
check 4 conditions(0+0=0, 0+1=1, 1+0=1, 1+1=10(carry=1)) for binary addition for each element in both the arrays and reset the carry accordingly.
append the addition in stringbuffer
append rest of the elements from max size array to stringbuffer but check consider carry while appending
print stringbuffer in reverse order for the answer.
//The java code is as
static String binaryAdd(String a, String b){
int len = 0;
int size = 0;
char[] c1 = a.toCharArray();
char[] c2 = b.toCharArray();
char[] max;
if(c1.length > c2.length){
len = c2.length;
size = c1.length;
max = c1;
}
else
{
len = c1.length;
size = c2.length;
max = c2;
}
StringBuilder sb = new StringBuilder();
int carry = 0;
int p = c1.length - 1;
int q = c2.length - 1;
for(int i=len-1; i>=0; i--){
if(c1[p] == '0' && c2[q] == '0'){
if(carry == 0){
sb.append(0);
carry = 0;
}
else{
sb.append(1);
carry = 0;
}
}
if((c1[p] == '0' && c2[q] == '1') || (c1[p] == '1' && c2[q] == '0')){
if(carry == 0){
sb.append(1);
carry = 0;
}
else{
sb.append(0);
carry = 1;
}
}
if((c1[p] == '1' && c2[q] == '1')){
if(carry == 0){
sb.append(0);
carry = 1;
}
else{
sb.append(1);
carry = 1;
}
}
p--;
q--;
}
for(int j = size-len-1; j>=0; j--){
if(max[j] == '0'){
if(carry == 0){
sb.append(0);
carry = 0;
}
else{
sb.append(1);
carry = 0;
}
}
if(max[j] == '1'){
if(carry == 0){
sb.append(1);
carry = 0;
}
else{
sb.append(0);
carry = 1;
}
}
}
if(carry == 1)
sb.append(1);
return sb.reverse().toString();
}
import java.io.;
import java.util.;
public class adtbin {
static Scanner sc=new Scanner(System.in);
public void fun(int n1) {
int i=0;
int sum[]=new int[20];
while(n1>0) {
sum[i]=n1%2; n1=n1/2; i++;
}
for(int a=i-1;a>=0;a--) {
System.out.print(sum[a]);
}
}
public static void main() {
int m,n,add;
adtbin ob=new adtbin();
System.out.println("enter the value of m and n");
m=sc.nextInt();
n=sc.nextInt();
add=m+n;
ob.fun(add);
}
}
you can write your own One.
long a =100011111111L;
long b =1000001111L;
int carry = 0 ;
long result = 0;
long multiplicity = 1;
while(a!=0 || b!=0 || carry ==1){
if(a%10==1){
if(b%10==1){
result+= (carry*multiplicity);
carry = 1;
}else if(carry == 1){
carry = 1;
}else{
result += multiplicity;
}
}else if (b%10 == 1){
if(carry == 1){
carry = 1;
}else {
result += multiplicity;
}
}else {
result += (carry*multiplicity);
carry = 0;
}
a/=10;
b/=10;
multiplicity *= 10;
}
System.out.print(result);
it works just by numbers , no need string , no need SubString and ...
package Assignment19thDec;
import java.util.Scanner;
public class addTwoBinaryNumbers {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Enter 1st Binary Number");
int number1=sc.nextInt();
int reminder1=0;
int number2=sc.nextInt();
int reminder2=0;
int carry=0;
double sumResult=0 ;int add = 0
;
int n;
int power=0;
while (number1>0 || number2>0) {
/*System.out.println(number1 + " " +number2);*/
reminder1=number1%10;
number1=number1/10;
reminder2=number2%10;
number2=number2/10;
/*System.out.println(reminder1 +" "+ reminder2);*/
if(reminder1>1 || reminder2>1 ) {
System.out.println("not a binary number");
System.exit(0);
}
n=reminder1+reminder2+carry;
switch(n) {
case 0:
add=0; carry=0;
break;
case 1: add=1; carry=0;
break;
case 2: add=0; carry=1;
break;
case 3: add=1;carry=1;
break;
default: System.out.println("not a binary number ");
}
sumResult=add*(Math.pow(10, power))+sumResult;
power++;
}
sumResult=carry*(Math.pow(10, power))+sumResult;
System.out.println("\n"+(int)sumResult);
}
}
Try this, tested with binary and decimal and its self explanatory
public String add(String s1, String s2, int radix){
int s1Length = s1.length();
int s2Length = s2.length();
int reminder = 0;
int carry = 0;
StringBuilder result = new StringBuilder();
int i = s1Length -1;
int j = s2Length -1;
while (i >=0 && j>=0) {
int operand1 = Integer.valueOf(s1.charAt(i)+"");
int operand2 = Integer.valueOf(s2.charAt(j)+"");
reminder = (operand1+operand2+carry) % radix;
carry = (operand1+operand2+carry) / radix;
result.append(reminder);
i--;j--;
}
while(i>=0){
int operand1 = Integer.valueOf(s1.charAt(i)+"");
reminder = (operand1+carry) % radix;
carry = (operand1+carry) / radix;
result.append(reminder);
i--;
}
while(j>=0){
int operand1 = Integer.valueOf(s2.charAt(j)+"");
reminder = (operand1+carry) % radix;
carry = (operand1+carry) / radix;
result.append(reminder);
j--;
}
return result.reverse().toString();
}
}
static int addBinaryNumbers(String a, String b) {
int firstToDecimal = 0;
int secondToDecimal = 0;
for (int i = a.length() - 1, count = 0; i >= 0; i--, count++) {
firstToDecimal += (Math.pow(2, count) * Integer.parseInt(String.valueOf(a.toCharArray()[i])));
}
for (int i = b.length() - 1, count = 0; i >= 0; i--, count++) {
secondToDecimal += (Math.pow(2, count) * Integer.parseInt(String.valueOf(b.toCharArray()[i])));
}
return firstToDecimal + secondToDecimal;
}
public static void main(String[] args) {
System.out.println(addBinaryNumbers("101", "110"));
}
here's a python version that
def binAdd(s1, s2):
if not s1 or not s2:
return ''
maxlen = max(len(s1), len(s2))
s1 = s1.zfill(maxlen)
s2 = s2.zfill(maxlen)
result = ''
carry = 0
i = maxlen - 1
while(i >= 0):
s = int(s1[i]) + int(s2[i])
if s == 2: #1+1
if carry == 0:
carry = 1
result = "%s%s" % (result, '0')
else:
result = "%s%s" % (result, '1')
elif s == 1: # 1+0
if carry == 1:
result = "%s%s" % (result, '0')
else:
result = "%s%s" % (result, '1')
else: # 0+0
if carry == 1:
result = "%s%s" % (result, '1')
carry = 0
else:
result = "%s%s" % (result, '0')
i = i - 1;
if carry>0:
result = "%s%s" % (result, '1')
return result[::-1]
import java.util.Scanner;
{
public static void main(String[] args)
{
String b1,b2;
Scanner sc= new Scanner(System.in);
System.out.println("Enter 1st binary no. : ") ;
b1=sc.next();
System.out.println("Enter 2nd binary no. : ") ;
b2=sc.next();
int num1=Integer.parseInt(b1,2);
int num2=Integer.parseInt(b2,2);
int sum=num1+num2;
System.out.println("Additon is : "+Integer.toBinaryString(sum));
}
}

Categories