简体   繁体   English

用户如何一次在字符串中输入模式并将其存储为Java中的2D字符数组

[英]How to user input a pattern in string at a time and store it as a 2d character array in Java

I am stuck with a particular problem. 我陷入一个特殊的问题。 Any help would be highly appreciable . 任何帮助都是非常可观的。

Suppose i am given rows=8 and column=5. 假设我得到的行数为8,列数为5。 How do i take a input from the user in the following manner. 我如何以以下方式从用户那里获取输入。

Aacvg
Qwn&k
LOpyc
GYhcj
%&fgT
JUIOP
icgfd
Hu*hc 

and then calculate the number of 'c' in the whole user defined pattern, which will give an output=5 here. 然后计算整个用户定义模式中的'c'数,此处将给出输出= 5。

I tried to input each line as String and then convert it into a 2D char array but it wasn't working that way. 我尝试将每行输入为String,然后将其转换为2D char数组,但这种方式无法正常工作。 Also how to limit the number of char per line to the number of column(here 5). 还有如何将每行的字符数限制为列数(此处为5)。

NB:- the input will be a whole line together and then the next line as a whole .... till row=8(for this example). 注意:-输入将是整行,然后是下一行。..直到row = 8(对于此示例)。

I would be very thankful if someone could tell me a way to do that.(I even considered array of arrays.) 如果有人能告诉我这样做的方法,我将非常感激(我什至考虑过数组数组)。

I wrote the following code but it isn't working so if anyone can write the correct way to do it. 我编写了以下代码,但是它无法正常工作,因此如果有人可以编写正确的方法来执行此操作。

Scanner sc = new Scanner (System.in);
for(int i=0;i<row;i++){
            String str;
            str = sc.nextLine();
            for(int j=0;j<column;j++){
                char[] charArray = str.toCharArray();
                a[i][j]= charArray[j];
                if(a[i][j]=='c'){
                     count= count+1;
                }
            }
 }

Instead of fumbling around with arrays I would recommend to make use of the functions provided by the String class. 建议不要使用数组,而建议使用String类提供的函数。

This is one possible approach to achieve this with code based upon your example. 这是根据您的示例使用代码实现此目标的一种可能方法。

    int row = 8;
    int column = 5;
    int count = 0;
    char searchedCharacter = 'c';
    int currentIndex;
    boolean searching;

    Scanner sc = new Scanner(System.in);
    String str;

    for (int i = 0; i < row; i++) {
        str = sc.nextLine();
        currentIndex = -1;
        searching = true;

        while (searching) {
            // indexOf will return -1 if the character was not found in the String
            currentIndex = str.indexOf(searchedCharacter, currentIndex + 1);

            if (currentIndex > -1 && currentIndex < column) {
                count++;
            } else {
                searching = false;
            }
        }
    }

    sc.close();

    System.out.println(count);

This way you won't have to deal with an Array and also not with the comparison of the characters, because your String object will do most of the work for you. 这样,您将不必处理Array ,也不必处理字符的比较,因为String对象将为您完成大部分工作。

It will also prevent you from running in an IndexOutOfBoundsExceptions which can easily happen in your code because you don't check if charArray[j] is actually a valid index. 这也将阻止您在IndexOutOfBoundsExceptions中运行,因为您不检查charArray[j]是否实际上是有效的索引,所以它很容易在代码中发生。

Maybe this is the code you want: 也许这是您想要的代码:

import java.util.Scanner;

public class WTF {

public static void main(String[] args) {
    new WTF().doIt();
}

private void doIt() {
    Scanner sc = new Scanner(System.in);

    int row = 8;
    int column = 5;
    int count = 0;

    for (int i = 0; i < row; i++) {
        System.out.println("Please enter the row no. " + (i + 1) + ":");
        String str = sc.nextLine();
        // cut the string to 5 chars
        str = str.substring(0, column);

        char[] charArray = str.toCharArray();
        for (char c : charArray) {
            if ('c' == c) {
                count++;
            }
        }
    }

    System.out.println("The number of c entered is: " + count);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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