Repeating Java Array -


i'm new java , still learning, keep in mind. i'm trying write program user can type in keyword , it'll convert numbers , put in array. problem array needs keep repeating int's.

my code is:

string keyword=inputdata.nextline(); int[] key = new int[keyword.length()]; (int k = 0; k < keyword.length(); ++k) {     if (keyword.charat(k) >= 'a' && keyword.charat(k) <= 'z')     {         key[k]= (int)keyword.charat(k) - (int)'a';     } } 

right if try key[i] higher keyword.length throws outofbounds error. need to infinte.

so basically, if keyword.length() 3 need able see if key[2] same key[5] , key[8] , on.

thanks help!

well, it's easiest fix code bit of refactoring first. extract uses of keyword.charat(k) local variable:

for (int k = 0; k < keyword.length(); ++k) {     char c = keyword.charat(k);     if (c >= 'a' && c <= 'z')     {         key[k] = c'a';     } } 

then can fix issue % operator:

// assume want different upper bound? (int k = 0; k < keyword.length(); ++k) {     char c = keyword.charat(k % keyword.length());     if (c >= 'a' && c <= 'z')     {         key[k] = c - 'a';     } } 

that's assuming make key longer keyword - , want change upper bound of loop too. example:

int[] key = new int[1000]; // or whatever (int k = 0; k < key.length; ++k) {     char c = keyword.charat(k % keyword.length());     if (c >= 'a' && c <= 'z')     {         key[k] = c - 'a';     } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -