#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Printf logic loop?


    I wrote this program to take in either a binary string, or hexadecimal string from file then spits out the values converted in binary, decimal, and hexadecimal form.
    I have most of the program done, I just want the output to all be aligned.
    Heres my code:

    Code:
    import java.io.*; 
    import java.util.*; 
    public class InAndOut
    {
        
        public static void main (String [] args) 
        {
            boolean fileNotOpen = false;
            Scanner kb = new Scanner(System.in);
            String input;       
            Scanner inFile;     
            FileInputStream in = null;
            
            while (!fileNotOpen)
            {
                System.out.println("Enter full path name of input file below:");
                input = kb.nextLine();
                try
                {
                    in = new FileInputStream(input);
                    fileNotOpen = true;
                }
                catch (FileNotFoundException e)
                {
                    System.out.println(input + " is not valid.  Please try again.");
                    System.out.println("Could not open file; program exiting");
                    System.exit(1);
                }
            }
            System.out.printf("%11s%20s%22s\n","Binary","Decimal","Hexadecimal");
            inFile = new Scanner(in);
            while(inFile.hasNextLine())
            {
                String number = inFile.nextLine();
                   char value;
                   int a = 0;
               
                
                   while( a < number.length()) 
                   {
                       value = number.charAt(a);
                       if (value == '1' || value == '0')
                       {
                           a++; 
                        }
                        else
                        {
    
                            a--;
                            break;
                        }
    
                    }
                   if(a==number.length())
                   {
                        double j=0;
                        for(int i=0;i<number.length();i++){
                            if(number.charAt(i)== '1')
                            {
                                j=j+ Math.pow(2,number.length()-1-i);
                            }
    
                        }
                       int n = (int)j;
                       String hex = "";
                       int remainder;
                       String hex1;
                       do
                 
                       {
                           remainder = n % 16;
                           if (remainder < 10)
                           {
                               hex = remainder + hex;
                            }
                            else if (remainder == 10)
                            {
                                hex = "A" + hex;
                            }
                            else if (remainder == 11)
                            {
                                hex = "B" + hex;
                            } 
                            else if (remainder == 12)
                            {
                                hex = "C" + hex;
                            }
                            else if (remainder == 13)
                            {
                                hex = "D" + hex;
                            }
                            else if (remainder == 14)
                            {
                                hex = "E" + hex;
                            }
                            else if (remainder == 15)
                            {
                                hex = "F" + hex;
                            }
                            n = n / 16;
                            
                        }
                        while (n > 0);
                        hex1 = hex;
                       
                        System.out.printf("%s%14d%20s\n",number,(int)j,hex1);
                   }   
                    else
                    {
                        String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};  
                        String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
                        String results;  
                        String result="";  
                         for(int i=0;i<number.length();i++)  
                         {  
                             char t=number.charAt(i);  
                             String t2=""+t+"";  
                             for(int b=0;b<hex.length;b++)  
                             {  
                                 if(t2.equalsIgnoreCase(hex[b]))  
                                 {  
                                     result=result+binary[b];  
                                    }  
                                }  
                            }  
                         results = result;
                         double d=0;
                        for(int i=0;i<results.length();i++){
                            if(results.charAt(i)== '1')
                            {
                                d=d+ Math.pow(2,results.length()-1-i);
                            }
    
                        }
                        System.out.printf("%s%14d%20s\n",result,(int)d,number);
                        
                    }
                   
                
            }
        }
       
       }
    This is the output I want:


    Enter full path name of input file below:
    4ben
    Binary Decimal Hexadecimal
    0100110000000001 19457 4C01
    1011111100000011 48899 BF03
    1100010010000101 50309 C485
    0011100100101111 14639 392F
    1001001010011110 37534 929E
    0110101011111110 27390 6AFE
    0010100000110100 10292 2834
    111110110100101 32165 7DA5
    1000100010010010 34962 8892
    1010011111110011 42995 A7F3
    1101100000011110 55326 D81E
    1010100011010110 43222 A8D6
    1000010100101111 34095 852F
    0011101110100001 15265 3BA1
    10001110110011 9139 23B3
    111110011101110 31982 7CEE
    101001111011100 21468 53DC
    100100011101100 18668 48EC
    1111100000100111 63527 F827
    1100011100011101 50973 C71D
    0111100011001001 30921 78C9
    1011100110111 5943 1737
    1110110100110101 60725 ED35
    1011000110101 5685 1635
    1001000100000110 37126 9106

    I was thinking either a logic loop to test string length then increasing the printf justification to adjust. Will this work? Any ideas?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The formatting control Strings used by the printf() method will allow you to position and align the text in many different ways. Try some experimenting with it to get the alignments you want.

    See the API doc for the Formatter class for details.

IMN logo majestic logo threadwatch logo seochat tools logo