简体   繁体   中英

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. 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.

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).

NB:- the input will be a whole line together and then the next line as a whole .... till row=8(for this example).

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.

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.

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.

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

}

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