I'm trying to find the first occurrence of a user-inputted character in a 2d array. If the character does not exist in the array, the program will return -1. If it does the index will be returned. Can't figure out how to initialize my index, nor do I know how to set the inputted letter to the element within the array. import java.util.Scanner;
public class characterSearch {
public static void main(String[] args) {
char[][] search = makeArray();
}
public static char[][] makeArray() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int numRows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int numColumns = sc.nextInt();
char[][] array = new char[numRows][numColumns];
System.out.println("Enter " + array.length + " rows and " + array[0].length + " columns");
for (int row = 0; row < array.length; row++)
for (int column = 0; column < array[0].length; column++)
array[row][column] = (char)(Math.random() * (122 - 97) + 97);
return array;
}
public static int findLetter(char[][] a) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char letter = in.next().charAt(0);
int index = letter.indexOf(a.length);
for (int i = 0; i < a.length; i++) {
if (a[i] = letter) {
return i;
}
}
return -1;
}
}
Aucun commentaire:
Enregistrer un commentaire