Advertisement
StefanTobler

Lesson 1011 Activity

Nov 12th, 2016
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. /*
  2.  * Lesson 1011 Coding Activity
  3.  *
  4.  * For this program you will input an int to represent the octal number
  5.  * and translate to the base ten number. The octal number must be 8 digits or less.
  6.  *
  7.  * Your program should also check that all the digits are 0 - 7, then translate the
  8.  * number to base ten.
  9.  *
  10.  * Sample Run 1:
  11.  * Enter a number in base 8:
  12.  * 1287
  13.  * ERROR: Incorrect Octal Format
  14.  *
  15.  * Sample Run 2:
  16.  * Enter a number in base 8:
  17.  * 123
  18.  * 83
  19.  *
  20.  * Sample Run 3:
  21.  * Enter a number in base 8:
  22.  * 1111111111
  23.  * ERROR: Incorrect Octal Format
  24.  *  
  25.  */
  26.  
  27. import java.util.Scanner;
  28. import java.lang.Math;
  29.  
  30. class Lesson_1011_Activity{
  31.     public static void main(String[] args)
  32.      {
  33.       Scanner scan = new Scanner (System.in);
  34.       System.out.println("Please enter a number in base 8:");
  35.       String x = scan.nextLine();
  36.       int sum = 0;
  37.       int flag = 0;
  38.       if (x.length() > 8)
  39.       {
  40.         System.out.println("ERROR: Incorrect Octal Format");
  41.       }
  42.       else
  43.       {
  44.       for (int i = 0; i < x.length(); i++)
  45.       {
  46.         if (x.charAt(i) > 55)
  47.         {
  48.           System.out.println("ERROR: Incorrect Octal Format");
  49.           flag++;
  50.         }    
  51.       }
  52.       if (flag == 0)
  53.       {
  54.         int a = x.length()-1;
  55.         for(int i = 0; i < x.length(); i++)
  56.         {
  57.             System.out.println((x.charAt(i)-48)*(Math.pow(8, a)));
  58.             sum += (x.charAt(i)-48)*(Math.pow(8, a));
  59.             a--;
  60.         }
  61.         System.out.println(x.charAt(0)-48);
  62.         System.out.println(sum);
  63.       }
  64.        
  65.       }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement