Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Variations on a theme – 111 – Project 1

Posted on September 25, 2024 By William Jojo
Uncategorized
        String number;
        int radix;
        for (int i = 0; i < args.length; i++)
        {
            number = args[i].substring(0,args[i].indexOf(':'));
            radix = Integer.parseInt(args[i].substring(args[i].indexOf(':') + 1));
            System.out.println(number + " base " + radix + " is " + Integer.parseInt(number, radix) + " base 10.");
        }

        if(args.length>0){ //Checking for input from user
            for(String input:args){ //Loop to parse
                //Find the numerical position of the colon
                int ColonPosition = input.indexOf(":");

                //Use substring to get the first half and the second half
                String number = input.substring(0,ColonPosition);
                int radix = Integer.parseInt(input.substring(ColonPosition+ 1));

                //Converting the number to base 10 with radix
                int base10 = Integer.parseInt(number, radix);

                //Print out conversion
                System.out.println(number+" base "+radix+" is "+base10+" base 10");

            }
        } else {
            //Error message
            System.out.println("Please provide the input!");
        }

        int x, colIndex, radix, base10value;
        String befCol = "" ;
        String aftCol;
        String valueToken;

        // Looping through each argument
        for (x = 0; x < args.length; x++) {
            //
            valueToken = args[x];

            colIndex = valueToken.indexOf(":");
            //Check for missing colon
            if (colIndex == -1) {
                System.out.println("Error: Missing colon in '" + valueToken + "'");
            } else
                befCol = valueToken.substring(0, colIndex);
            aftCol = valueToken.substring(colIndex + 1);
            // trapping conversion error
            try {
                //parse radix(string) to integer
                radix = Integer.parseInt(aftCol);
                //convert radix to base 10
                base10value = Integer.parseInt(befCol, radix);

                System.out.println(befCol + " base " + radix + " is " + base10value + " base 10.");
            } catch (Exception e) {
                System.out.println("Invalid number format: " + valueToken );
            }
        }

        for (String arg : args) {

            int colonIndex = arg.indexOf(':');

            String value = arg.substring(0, colonIndex);

            String radixString = arg.substring(colonIndex + 1);

            int radix = Integer.parseInt(radixString);

            int base10Value = Integer.parseInt(value, radix);

            System.out.println(value + " base " + radix + " is " + base10Value + " base 10");
        }
        int x;
        for (x=0; x < args.length; x++) {
            int index = args[x].indexOf(":");
            String num = args[x].substring(0,index);
            int radix = Integer.parseInt(args[x].substring(index + 1,args[x].length()));
            int result = Integer.parseInt(num,radix);
            System.out.println(num + " base " + radix + " is " + result + " base 10");
        }

        // Loop through each command-line argument
        for (String items : args) {

            // find the colon
            int colon = items.indexOf(':');

            // gather the number and the radix
            String value = items.substring(0, colon);
            String radixString = items.substring(colon + 1);

            // Change the string to and int
            int radix = Integer.parseInt(radixString);

            // Convert the value to base 10
            int base10result = Integer.parseInt(value, radix);

            System.out.println(value + " base " + radix + " is " + base10result + " base 10");

        }
        int x;
        //Looping through the arguments provided & for each argument:
        // 1) Creating a substring from the first character to the colon which
        // represents the number to be converted
        // 2) Creating a substring from the colon to the last character which
        // represents the radix
        // 3) Converting the substring of the number to be converted to a number in
        //  base 10
        // 4) Printing those characters in a readable format

        // 2 Point Extra Credit: If there is not a colon, say so AND continue reading
        // to the next argument
        // 3 Point Extra Credit: Use Try/Catch statements to handle possible exceptions
        for (x=0; x < args.length; x++) {
            try {
                System.out.println(args[x].substring(0, args[x].indexOf(":")) + " base " +
                        args[x].substring(args[x].indexOf(":") + 1) + " is " +
                        parseInt(args[x].substring(0, args[x].indexOf(":")),
                                parseInt(args[x].substring(args[x].indexOf(":") + 1))) +
                        " base 10.");
            }
            catch (Exception eRef){
                if (eRef instanceof NumberFormatException)
                    System.out.println("This number or its radix could not be read as an integer.");
                else if (args[x].indexOf(":") == -1)
                    System.out.println("There was not a colon found in this entry");
            }
        }
        int x;
        int realVal;
        for (x = 0; x < args.length; x++) {
            System.out.println("args[" + x + "] = " + args[x]);

            int colonPos = args[x].indexOf(':');
            String value = args[x].substring(0, colonPos);
            String radix = args[x].substring(colonPos + 1);

            realVal = Integer.parseInt(value, Integer.parseInt(radix) ) ;

            System.out.println(value + " base " + radix + " is " + realVal + " base 10");
        }
    }

Post navigation

❮ Previous Post: Chapter 6502-5 – Beyond the Basics
Next Post: Project 4 Showcase ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.