title: Comparing 2 strings by ascii values in Java
body: <p>I have to write a method to compare strings alphabetically and return an <code>int</code>. I can't use any built-in functions and I'm supposed to use a <code>for</code> loop. </p>
  
  <p>I'm unsure about how to deal with the strings being of different lengths. At the moment my main issue is that the code is only comparing the first char of each string then returning the int, but I can't put <code>return comparison;</code> outside of the for loop</p>
  
  <pre><code>public class poop {
      public static int Compare(String s1, String s2) {
  
         for (int i = 0; i &lt; s1.length() &amp;&amp; i &lt; s2.length(); i++) {
  
             int comparison = 0;
             int ascii1 = 0;
             int ascii2 = 0;
             //convert chars into their ascii values
             ascii1 = (int) s1.charAt(i); 
             ascii2 = (int) s2.charAt(i); 
  
             //treat capital letters as lower case
             if (ascii1 &lt;= 95) {
                 ascii1 += 32;
             } if (ascii2 &lt;= 95) {
                 ascii1 += 32;
             }
  
             if (ascii1 &gt; ascii2) {
                 comparison = 1;
             } else if (ascii1 &lt; ascii2) {
                   comparison = -1;
             } else { 
                   comparison = 0;
             }
  
         } 
         return comparison;
      }
  
      public static void main(String[] args) {
          String s1 = "aba";
          String s2 = "aaa";
          System.out.println(Compare(s1,s2));
    }
  }
  </code></pre>