title: Scanner for hex-input not working as expected inside loop
body: <p>I need to solve this problem where i need to convert hexadecimal numbers to decimals.</p>
  <h3>Issue</h3>
  <p>I have compiled it without any errors, but my output came out wrong. Can someone show how to fix these errors?</p>
  <h2>Code</h2>
  <pre><code>import java.util.Scanner;
  import java.io.*;
  class HexToDecException extends Exception  {
     
      public HexToDecException(String msg) {
          super(msg);
      }
  }
  
  public class Main {
      public static int hexToDec(String hex) throws HexToDecException {
          char currentChar;
          int dec=0;
          int power = 0;
      
          for(int i=hex.length()+1;i&gt;=0;i--){// by reverse
              currentChar = hex.charAt(i);
             
              if(currentChar&gt;='0' &amp;&amp; currentChar&lt;='9'){
                  dec+= Math.pow(16,power)*(currentChar - '0');
                  power++;
              }
              else if(currentChar&gt;='a' &amp;&amp; currentChar&lt;='f'){
                  dec+= Math.pow(16,power)*(currentChar - 'a' + 10);
                  power++;
              }
              else if(currentChar&gt;='A' &amp;&amp; currentChar&lt;='G'){
                  dec+= Math.pow(16,power)*(currentChar - 'A' + 10);
                  power++;
              }
              else
                  throw new IllegalArgumentException(&quot;Invalid Character in Hexadecimal string&quot;);
          }
          
          return power;
      }
      public static void main(String[] args){
          Scanner scan = null;
          try{
              scan = new Scanner(System.in);
              int n = scan.nextInt();
              for(int i=0;i&lt;n;i++)
                  System.out.println(hexToDec(scan.next()));
          }
          catch(Exception ex){
              ex.printStackTrace();
          }
          
          finally{
              System.out.println(&quot;That's all.&quot;);
          }
      }
  }
  </code></pre>
  <h3>Example</h3>
  <p>Input:</p>
  <pre><code>5 6B 5 4AZ A5 9
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>107  5 Invalid hexadecimal number 165 9
  </code></pre>
  <p>Output that i got:</p>
  <pre><code>That's all
  </code></pre>