Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

parseInt and subString commands for Hexadecimal Conversion

$
0
0
Hello all, just a quick question. As far as I can gather from the APIs the parseInt should convert from a string to an integer following the base you give it (i.e. base 16). Similarly, the subString function should restrict which part of the string it's looking at. With this in mind, is there any reason in particular why the following code fails completely?

     public static void getColourAmountCMYK() {
        Scanner in = new Scanner(System.in);
        String fullString = "";
        int R = "";
        int G = "";
        int B = "";

        while (fullString == "") {
            System.out.print("  Please enter your 6 digit value: ");
            while (!in.hasNext()) {
                showError();
                System.out.print("  Please enter your 6 digit value: "); //in.next();
            }
            if (in.next().length() != 6) {
                showError();
            } else {
                fullString = in.next();
            }
        }

        System.out.println("Calculating.....");

        String part1 = fullString.substring(1, 3);
        R = Integer.parseInt(part1);

        G=Integer.parseInt(fullString.substring(3,5),16);
        B=Integer.parseInt(fullString.substring(5,7),16);
        System.out.println(R + " and " + G + " and " + B + "!");

        System.out.println();
    } 



The idea is, as I'm sure you gathered, that the user enters a 6 char long string which is then converted into RGB values (i.e. 1,2 is red; 3,4 is green; 5,6 is blue) - but netbeans is having an issue with R,G and B being either int or String variables, so I'm confused as to why it's failing. A pointer in the right direction would be very much appreciated :)


Thank you in advance, Will

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>