Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are iterating through the string, but you only retain the last character because of this snippet:</p> <pre><code>c = Character.toString(x.charAt(i)); </code></pre> <p>This should be:</p> <pre><code>c += Character.toString(x.charAt(i)); </code></pre> <p>As this will append the current character iteration to the overall string. Replace the snippets with this fix, for the two loops. The <code>c</code> variable will build up the country code and will pass this condition this time:</p> <pre><code>if (c.equals("USA")) { </code></pre> <p>After the first loop and before the second loop, you will need to re-initialize the <code>c</code> variable:</p> <pre><code>c = ""; </code></pre> <p>Once done, you can put that logic in a method of its own, so you avoid duplicate code within the loops. </p> <p>This logic could be simplified by using <code>String.substring</code> instead, as others pointed out, as you work in details with the <code>String.charAt</code> which is more tedious. I thought though that pointing out your logic error was worth it, before giving you other pointers.</p> <p>So talking about other approaches, you could try another one to your country code and name console output. Try to use a <code>HashMap</code> where the keys are the country code and the value is the country's name. You can iterate through the <code>HashMap</code> after that and print out both keys and values. That would be more high-level to your current solution and way shorter in code.</p> <p>EDIT1: I offered the code to the last suggestion but I removed it, as I realized that giving code to assignment related questions is not encouraged.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload