7 Replies Latest reply on Mar 4, 2013 7:58 PM by doremifasollatido

    Help with something simple

      Hey guys so I'm trying to write a quick program that should be no problem but I hit a wall and have no idea what's going wrong. The part of my program that isn't working is as follows: I'm accepting a String hexadecimal value as input that can be any number of characters long (for example "FF34CD"). I want to convert this to decimal so what I have done is parsed the input string and assigned each character to a slot in an array called hexArray. I then wrote a loop that will populate decimalArray with the corresponding decimal values for the hex info (i.e. 1=1, 2=2, A=10, B=11, and so on). What I don't understand is that the code works fine for an input that is one character long (i.e. "F"), but as soon as I try a longer input, the decimalArray does not get populated at all. I have tried everything I can think of to make this work and I can't seem to figure it out. Any thoughts?
       * Write a description of class FAT here.
       * @author (your name) 
       * @version (a version number or a date)
      public class FAT
          // instance variables - replace the example below with your own
          private String[] hexArray;
          private int[] decimalArray;
          private String hexValue;
          int test;
           * Constructor for objects of class FAT
          public FAT(String hexValueIn)
              hexValue = hexValueIn;
              hexArray = new String[hexValue.length()];
              decimalArray = new int[hexValue.length()];
              test = 0;
           * An example of a method - replace this comment with your own
           * @param  y   a sample parameter for a method
           * @return     the sum of x and y 
          public int convertDecimal()
              int result = 0;
              int arraySize = hexValue.length();
              for(int i = 0; i < arraySize; i++)
                  String entry;
                  entry = hexValue.substring(i,i+1);
                  hexArray[i] = entry;
              for(int i = 0; i < arraySize; i++)
                  if(hexArray[i] == "0")
                      decimalArray[i] = 0;
                  else if(hexArray[i] == "1")
                      decimalArray[i] = 1;
                  else if(hexArray[i] == "2")
                      decimalArray[i] = 2;
                  else if(hexArray[i] == "3")
                      decimalArray[i] = 3;
                  else if(hexArray[i] == "4")
                      decimalArray[i] = 4;
                  else if(hexArray[i] == "5")
                      decimalArray[i] = 5;
                  else if(hexArray[i] == "6")
                      decimalArray[i] = 6;    
                  else if(hexArray[i] == "7")
                      decimalArray[i] = 7; 
                  else if(hexArray[i] == "8")
                      decimalArray[i] = 8;
                  else if(hexArray[i] == "9")
                      decimalArray[i] = 9;  
                  else if(hexArray[i] == "A")
                      decimalArray[i] = 10;
                  else if(hexArray[i] == "B")
                      decimalArray[i] = 11;            
                  else if(hexArray[i] == "C")
                      decimalArray[i] = 12;
                  else if(hexArray[i] == "D")
                      decimalArray[i] = 13;
                  else if(hexArray[i] == "E")
                      decimalArray[i] = 14;
                  else if(hexArray[i] == "F")
                      decimalArray[i] = 15;
              return result;
        • 1. Re: Help with something simple
          It doesn't work at all. You are initialising 'result' to zero, never changing it, and returning it.
          • 2. Re: Help with something simple
            You should get in the habit of putting System.out statements in your code to see the value of variables at certain points. Its harder to catch mistakes by just staring at code, it is far more valuable to see what the code is doing when it is actually running.
            • 3. Re: Help with something simple
              Yeah, I'm sorry, I should have been more specific, I'm not actually looking at the returned value, I'm simply running the convertDecimal code and then inspecting the decimalArray (as I haven't gotten to the part where I return the decimal value because the decimalArray is not populating correctly).

              For some reason it populates completely fine when the input is "F" but not "FF".

              Additionally, here are the following things I have done to rule out some issues:

              - I put a simple counter to make sure that the loop is still running the appropriate amount of times when the input length is >=2. The loop does run.
              - I hardcoded the amount of times the loop should run, rather than using arrayLength, still didn't work.
              - I even completely hardcoded, within the loop, the subscript values for the two arrays (i.e. instead of hexArray(i) and decimalArray(i), I actually wrote in: hexArray[0], decimalArray[0], hexArray[1], decimalArray[1], and so on and it STILL did not work. For some reason, whenever the input is longer than 1 character, not one of the IF statements is returning true, even though it (obviously) should be - like I said I even harcoded it to say:
              if(hexArray[0] == "F")
                   decimalArray[0] = 15;
              ... and so on. And it returns false for the IF statement EVEN IF I inputted, for example "FFFFF" where every single character in the input is an F - how could that if statement possibly be coming back false? And why does it come back true when the input is just 'F'??? I hope this helps clear up the issue I'm having. And thanks in advance for the responses guys you're awesome!

              Edited by: 991431 on Mar 3, 2013 7:51 AM

              Edited by: 991431 on Mar 3, 2013 8:00 AM

              Edited by: 991431 on Mar 3, 2013 8:01 AM
              • 4. Re: Help with something simple
                Google for "java string compare".
                • 5. Re: Help with something simple
                  Thank you! Using .equals() worked. I should have remembered that from when I took Java . . . any thoughts on why it worked using == with an input of 1 character though????

                  Again, thank you very much!
                  • 6. Re: Help with something simple
                    Not sure, if you show the main() when it worked for just "F" and the one when it didn't for "FF", I may see it.
                    • 7. Re: Help with something simple
                      If you just want to convert a hexadecimal value to a decimal value, there is a built-in static method in the Integer class that will do it for you.