Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package programa;
- import java.util.Scanner;
- public class Programa {
- static int a, b;
- public static void main(String[] args)
- {
- Scanner leer = new Scanner(System.in);
- System.out.println("Introduzca el primer octal");
- String valor = leer.nextLine();
- try
- {
- int valord = Integer.parseInt(valor, 8 );
- }
- catch (NumberFormatException e)
- {
- System.out.println("El primer número debe ser octal");
- }
- System.out.println("Introduzca el segundo octal");
- String valorb = leer.nextLine();
- try
- {
- int valordb = Integer.parseInt(valorb, 8 );
- }
- catch (NumberFormatException e)
- {
- System.out.println("El segundo número debe ser octal");
- }
- String res = "";
- int val;
- int mayor = valor.length() > valorb.length() ? valor.length() : valorb.length();
- int acarreo = 0;
- boolean seguir = true;
- for (int cont = mayor-1; cont >= 0; cont--)
- {
- val = 0;
- try
- {
- a = Character.getNumericValue(valor.charAt(cont));
- }
- catch (StringIndexOutOfBoundsException e)
- {
- a = 0;
- seguir = false;
- }
- try
- {
- b = Character.getNumericValue(valorb.charAt(cont));
- }
- catch (StringIndexOutOfBoundsException e)
- {
- b = 0;
- seguir = false;
- }
- val = a + b;
- if (acarreo > 0)
- {
- val += acarreo;
- acarreo = 0;
- }
- if (val >= 10)
- acarreo = val/10;
- if (val > 7)
- val -= 8;
- res += val;
- if (!seguir)
- {
- if (val > 0) res += val;
- else if (acarreo > 0) res += val;
- }
- }
- res = new StringBuffer(res).reverse().toString();
- System.out.println(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement