Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描器取1-3个单词_Java_Arrays_Algorithm_Multidimensional Array_Java.util.scanner - Fatal编程技术网

Java 扫描器取1-3个单词

Java 扫描器取1-3个单词,java,arrays,algorithm,multidimensional-array,java.util.scanner,Java,Arrays,Algorithm,Multidimensional Array,Java.util.scanner,所以我做了这个扫描器,我希望它能用1到3个字符串。1是它可以接受的最小字符串数,3是最大字符串数。到目前为止,我的输出是: You can search 1 - 3 words only : 3 Type a name: word1 Type a name: word2 Type a name: word3 相反,由于我的程序将执行多个字符串搜索,是否有一种方法可以将这些单词放在多个数组中 public static void main(String[]args) throws IOExce

所以我做了这个扫描器,我希望它能用1到3个字符串。1是它可以接受的最小字符串数,3是最大字符串数。到目前为止,我的输出是:

You can search 1 - 3 words only : 3
Type a name: word1 
Type a name: word2
Type a name: word3
相反,由于我的程序将执行多个字符串搜索,是否有一种方法可以将这些单词放在多个数组中

public static void main(String[]args) throws IOException{

    int nnames;
    String[] stringSearch;

    System.out.print("You can search 1 - 3 words only : ");
    Scanner in = new Scanner(System.in);
    nnames = Integer.parseInt(in.nextLine().trim());

    stringSearch = new String[nnames];
    for (int i = 0; i < stringSearch.length; i++){
            System.out.print("Type a name: ");
            stringSearch[i] = in.nextLine();

    }
publicstaticvoidmain(字符串[]args)引发IOException{
内基因组;
字符串[]字符串搜索;
System.out.print(“您只能搜索1-3个单词:”);
扫描仪输入=新扫描仪(系统输入);
nnames=Integer.parseInt(in.nextLine().trim());
stringSearch=新字符串[nnames];
for(int i=0;i
像这样的东西

    public static void main(String[]args) throws IOException{
        int nnames;
        int ind = 0;
        int maxSearch = 10000;//this is max times we can do searching
        String[][] stringSearch = new String[maxSearch][];

        System.out.print("You can search 1 - 3 words only(else means stop search) : ");
        Scanner in = new Scanner(System.in);
        nnames = Integer.parseInt(in.nextLine().trim());

        while(nnames>=1 && nnames<=3){
            stringSearch[ind] = new String[nnames];
            for (int i = 0; i < stringSearch.length; i++){
                System.out.print("Type a name: ");
                stringSearch[ind][i] = in.nextLine();
            }
            ind++;
            nnames = Integer.parseInt(in.nextLine().trim());
        }
    }
publicstaticvoidmain(字符串[]args)引发IOException{
内基因组;
int ind=0;
int maxSearch=10000;//这是我们可以进行搜索的最大次数
字符串[][]stringSearch=新字符串[maxSearch][];
System.out.print(“您只能搜索1-3个单词(否则表示停止搜索):”;
扫描仪输入=新扫描仪(系统输入);
nnames=Integer.parseInt(in.nextLine().trim());

而(nnames>=1&&nnames“[我希望]将这些单词放在多个数组中,因为我的程序将执行多个字符串搜索。”抱歉,我不理解这部分。为什么需要多个数组来执行多个字符串搜索?实际上,如果您只是将字符串传递到
void performSearch(string)中,那么字符串存储在哪里根本不重要
method。是否希望有多个数组以避免锁定?如果是,则searchin中没有锁定。它是只读的(主要是)。您能告诉我们您需要搜索的字符串是什么以及您将在哪里搜索它们吗?您是否将这三个单词放在多个文本中进行搜索?如果您不想设置maxSearch,您可以使用ArrayList,在输入结束时,您可以将ArrayList转换为String[][]