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

时间:2020-12-28 15:44:33

I am stuck with a particular problem. Any help would be highly appreciable .

我遇到了一个特殊的问题。任何帮助都会非常值得一提。

Suppose i am given rows=8 and column=5. How do i take a input from the user in the following manner.

假设我被赋予rows = 8和column = 5。如何以下列方式从用户那里获取输入。

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. Also how to limit the number of char per line to the number of column(here 5).

我试图将每一行输入为String,然后将其转换为2D char数组,但它不是那样工作的。另外如何将每行的char数限制为列数(此处为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;
                }
            }
 }

2 个解决方案

#1


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.

这样您就不必处理数组,也不需要处理字符的比较,因为您的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]是否实际上是一个有效的索引。

#2


0  

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);
}

}

#1


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.

这样您就不必处理数组,也不需要处理字符的比较,因为您的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]是否实际上是一个有效的索引。

#2


0  

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);
}

}