java - Searching for chars from a String in a 2d char array? -
i want able search through array , find chars in string user has input? if user types "message" want return index of 'm' 'e' 's' , on. how this? heres code far:
import java.util.arraylist; import java.util.scanner; public class matrix { private char[][] matrix = new char[6][6]; private int[] usednumbers = new int[50]; {for(int x = 0; x < usednumbers.length; x++) usednumbers[x] = -1;} private final char[] cipher_key = {'a','d','f','g','v','x'}; private final string validchars = "abcdefghijklmnopqrstuvwxyz0123456789"; public matrix() { int random; for(int = 0; < cipher_key.length; i++) { for(int j = 0; j < cipher_key.length; j++) { validation: while(true) { random = (int)(math.random()*validchars.length()-1); for(int k = 0; k < usednumbers.length; k++) { if(random == usednumbers[k]) continue validation; else if(usednumbers[k]==-1) usednumbers[k] = random; } break; } matrix[i][j] = validchars.split("")[random].charat(0); } } } public void searchmatrix(){ scanner console = new scanner (system.in); string phrase = ""; system.out.println("\n enter message " + "to encrypt cipher: \n"); phrase = console.nextline(); char[] phrasesplit = phrase.tochararray(); console.close(); } public string tostring() { string output = " d f g v x\n"; for(int = 0; < cipher_key.length; i++) { output += cipher_key[i] + " "; for(int j = 0; j < cipher_key.length; j++) { output += matrix[i][j] + " "; } output += "\n"; } return output; }// tostring end } i have looked tutorials online cant find 1 me in situation! help? dont know next.
if not mistaking, there. have add nested loops after got phrasesplit.
for (int k=0; k<phrasesplit.length; k++) { (int i=0; i<matrix.length; i++) { (int j=0; j<matrix[i].length; j++) { if (phrasesplit[k] == matrix[i][j]) { system.out.printf("%c @ %d, %d\n", phrasesplit[k], i, j); }//end if }//end j }//end }//end k there may better way it. also, sasha's shuffle() suggestion.
Comments
Post a Comment