lundi 8 juin 2015

I made a for loop to swap two indiv char variables in a string

I've initialized i and j as random integers between positions 1 and str.length() -1. Is there a reason why this loop would not print the scrambled version of a word?

for (int x = 0; x < word.length(); x++) 
    {
        if (x == i) 
        {
             chararray[x] = word.charAt(i);
        }
        else if (x == j)
        {
            chararray[x] = word.charAt(j);
        }
    }

  word = new String (chararray);
  System.out.println(word);




Aucun commentaire:

Enregistrer un commentaire