title: Java for loop is probably broken or my code is wrong
body: <pre><code>import java.util.Scanner;
  import java.util.HashMap;
  import java.lang.Math;
  import java.util.Map;
  
   // Compiler version JDK 11.0.2
  
   class Main
   {
     public static void main(String args[])
     { 
       Scanner scan = new Scanner(System.in);
       long N = scan.nextLong();
       
       long log = N;
       long digit = 0L;
       
       while(log&gt;=1L)
       {
         log = log/10L;
         digit = digit+1L;
       }
       
       int i = 0;
       long cmonPlease = (long) Math.pow(10,i+1);
       int you = (int) Math.ceil(digit/3)+1;
       int totally = (int) Math.floor(i/3);
       long ruined = N%cmonPlease;
       long my = (long) Math.pow(10,i);
       long code = (long) Math.floor(ruined/my);
       
       long[][] format = new long[you+1][3];
       for(i=0; i&lt;digit; i++)
       {
         cmonPlease = (long) Math.pow(10,i+1);
         totally = (int) Math.floor(i/3); 
         ruined = N%cmonPlease;
         my = (long) Math.pow(10,i);
         code = (long) Math.floor(ruined/my); 
         format[totally][i%3]=code;
       }
       
       
       
       /*for(i=0; i&lt;format.length; i++) {
          for(int j=0; j&lt;format[i].length; j++) {
              System.out.println(&quot;Values at arr[&quot;+i+&quot;][&quot;+j+&quot;] is &quot;+format[i][j]);
          }
      }*/
       
       
       HashMap&lt;Long,String&gt; ones = new HashMap&lt;Long,String&gt;();
       ones.put(0L,&quot;zero&quot;);
       ones.put(1L,&quot;one&quot;);
       ones.put(2L,&quot;two&quot;);
       ones.put(3L,&quot;three&quot;);
       ones.put(4L,&quot;four&quot;);
       ones.put(5L,&quot;five&quot;);
       ones.put(6L,&quot;six&quot;);
       ones.put(7L,&quot;seven&quot;);
       ones.put(8L,&quot;eight&quot;);
       ones.put(9L,&quot;nine&quot;);
       
       HashMap&lt;Long,String&gt; teens = new HashMap&lt;Long,String&gt;();
       teens.put(0L,&quot;ten&quot;);
       teens.put(1L,&quot;eleven&quot;);
       teens.put(2L,&quot;twelve&quot;);
       teens.put(3L,&quot;thirteen&quot;);
       teens.put(4L,&quot;fourteen&quot;);
       teens.put(5L,&quot;fifteen&quot;);
       teens.put(6L,&quot;sixteen&quot;);
       teens.put(7L,&quot;seventeen&quot;);
       teens.put(8L,&quot;eightteen&quot;);
       teens.put(9L,&quot;nineteen&quot;);
       
       HashMap&lt;Long,String&gt; tens = new HashMap&lt;Long,String&gt;();
       tens.put(2L,&quot;twenty&quot;);
       tens.put(3L,&quot;thirty&quot;);
       tens.put(4L,&quot;fourty&quot;);
       tens.put(5L,&quot;fifty&quot;);
       tens.put(6L,&quot;sixty&quot;);
       tens.put(7L,&quot;seventy&quot;);
       tens.put(8L,&quot;eighty&quot;);
       tens.put(9L,&quot;ninety&quot;);
       
       HashMap&lt;Long,String&gt; illions = new HashMap&lt;Long,String&gt;();
       illions.put(format[0][0],&quot;&quot;);
       if(digit&gt;3L)
       {
         illions.put(format[1][0],&quot;thousand&quot;);
       }
       if(digit&gt;6L)
       {
         illions.put(format[2][0],&quot;million&quot;);
       }
       if(digit&gt;9L)
       {
         illions.put(format[3][0],&quot;billion&quot;);
       }
       
       Map&lt;Long, Map&lt;Long, String&gt;&gt; position = new HashMap&lt;&gt;();
       for(i=0;i&lt;you;i++)
       {
         if(format[i][1]&gt;1L)
         {
           position.put(format[i][1],tens);
           position.put(format[i][0],ones);
         }
         else if(format[i][1]==1L)
         {
           position.put(format[i][1],teens);
         }
         else if(format[i][1]==0L)
         {
           position.put(format[i][0],ones);
         }
         position.put(format[i][2],ones);
       }
       
       String out = &quot;&quot;;
       for(i=you-1;i&gt;=0;i--)
       {
         if(format[i][0]==0&amp;format[i][1]==0&amp;format[i][2]==0)
         {
           i=i-1;
         }
         
         if(format[i][2]!=0L)
         {
           out = out + position.get(format[i][2]).get(format[i][2]);
           out = out + &quot; hundred &quot;;
         }
         
         if(format[i][1]==1L)
         {
           out = out + position.get(format[i][1]).get(format[i][0]);
         }
         else if(format[i][1]&gt;1L)
         {
           out = out + position.get(format[i][1]).get(format[i][1])+&quot; &quot;+position.get(format[i][0]).get(format[i][0]);
         }
         
         if(format[i][1]==0L&amp;format[i][0]!=0L)
         {
           out= out + position.get(format[i][0]).get(format[i][0]);
         }
         
         out = out + &quot; &quot;+illions.get(format[i][0]);
         
         if(i!=0L)
         {
           out = out + &quot; &quot;;
         }
       }
       System.out.println(out);
     }
   }
  </code></pre>
  <p>Can somebody check what's wrong with my code? I just can't get my head over it.</p>
  <p>I was solving a Dcoder challenge and had to go through a whole bunch of mental gymnastics to get to the point I'm at now. It mostly works, say if you put non repeated digits like 987654</p>
  <blockquote>
  <p>987654 nine hundred eighty seven thousand six hundred fifty four</p>
  <p>Process finished.</p>
  </blockquote>
  <p>But in case of repeated digits two things happen:</p>
  <ol>
  <li>It repeats the illions' elements multiple times even though <strong>i</strong> is supposed to decrease.</li>
  <li>The tens' elements won't get added to <strong>out</strong> but ones' does</li>
  </ol>
  <blockquote>
  <p>88888
  eight eight thousand eight hundred eight eight thousand</p>
  <p>Process finished.</p>
  </blockquote>
  <p>The program is to take any integer N such that, 0≤N≤10¹⁰ and convert it in numerals.</p>
  <p>I personally think the problem is somewhere in how java deals with division or something but I can be wrong.</p>
  <p>This is my first StackOverflow question and also being a novice developer (as you can see from my code) please excuse any mistakes.</p>