简体   繁体   中英

I have to fill a 2D array with characters and let people search for words (Java)

So I have to fill a 2D array with chars, print out the array, let people search for words, and then print out the number of instances of that word and the array with the instances of that word highlit.

here is my code so far:

import java.util.Scanner;
import java.util.*;

public class testSearchMatrix {

public static void printArray(char[][] myArray){
    for(int i = 0; i < myArray.length; i++){
        for(int j = 0; j < myArray.length; j++){
            System.out.print(myArray[i][j] + " ");
        }
        System.out.println();
    }
}

public static void searchArray(char[][] a){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a query to search: ");
    String query = keyboard.next();
    int queryNum = 0;
    int w = 0;
    for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a[i].length; j++){
            if(a[i][j] == query.charAt(w)){
                queryNum += 1;
            }
        }
    }
    System.out.println(queryNum);
}


public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);
    Random random = new Random();

    //Create an alphabet array so I can use this to fill in the searchBox array
    char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    System.out.println("Please choose an array size: ");
    int a = keyboard.nextInt();

    //Create a square array
    char[][] searchBox = new char[a][a];

    //Fill in the array with random chars
    for(int i = 0; i < searchBox.length; i++){
        for(int j = 0; j < searchBox[i].length; j++){
            int randNum = random.nextInt(25);
            searchBox[i][j] = alphabet[randNum];
        }
    }

    //Implement my method to print the array to the screen
    System.out.println("Here is the square matrix with random letters: ");
    printArray(searchBox);


    System.out.println("Enter a query to search: ");
    searchArray(searchBox);




}
}

This will print out my array but I can't seem to get the search to work.

Modified Your searchArray function

public static void searchArray(char[][] a){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a query to search: ");
        String query = keyboard.next();
        int queryNum = 0;
        String out = null;
        int w = 0;
        for(int i = 0; i < a.length; i++){
            for(int j = 0; j < a[i].length; j++){
                if(a[i][j] == query.charAt(w)){
                    //System.out.println(i+":"+j+a[i][j]);
                    //w+=1;
                    if(out==null)
                    {
                        out=String.valueOf(a[i][j]);
                    }else
                    out=out+a[i][j];

                    for(int f = 1; f < query.length(); f++){
                        if(j+f<5){
                    if(a[i][j+f] == query.charAt(w+f)){
                   //   System.out.println(i+"Index:w+f"+w+f+query.charAt(w+f)+"query.charAt(w+f)Index"+query.indexOf(query.charAt(w+f)));
                    //  System.out.println(i+":"+j+a[i][j+f]);
                        out=out+a[i][j+f];
                        System.out.println(out+":"+query+"here"+out.length()+query.length());
                        if(out.equals(query))
                        {
                            System.out.println("Seach Found ");
                            queryNum += 1;
                            out=null;
                        }
                    } 

                        }


                    }  if(out!=null)
                       if(out.equals(query))
                {
                    System.out.println("Seach Found ");
                    queryNum += 1;
                    out=null;
                }  
                     out=null;
                }
            }
        }
        System.out.println(queryNum);
    }

OuptPut

OuptPut

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM