Rocky cohn
Rocky cohn

Reputation: 81

java, code which counts when at certain char from large string input

my question is if i have a string- with two types of chars in the string '#' and '.' and I want to count how many times I land on '#' if each time I move to the right 3 and down one. for example the given string would look like the following:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.
#..#...##.####..###.....#......
..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1//stops the checking

what should happen is the following, 0 means landed on a . and X means it landed on # which is when count should go up by one each time this happens:

...#.#.......##....#.......#...
.#.o..#.##.#..#.......#.....##.
#..#..0##.####..###.....#......
..#...##.0.#...#.#......#...#.#
.##.##.#...#0....#.##..##......
.#...#.#.##.###0.#...#...#.....
.#..##..#....##.##0...##....##.
..#...##....#..###...o....##...
.#..#..#.#....#.#...#.#.0....#.
.##.....#...#..#..#..#...##X...
.#...#....#..#...........###..0//notice here next round it will exceed
.....#...........##.#......#.....0..#...........##.#......#...//line get doubled as if returning to beginning of the 
.....#....##......##..#.#...........X....##......##..#.#......//same here
-1//to stop

so at the start, I move to the third index and down 1 which is to the next row and I check if that char is '#' if yes count++ so in this example- it moves three so the first row moves to ... then goes down to the next row which will be index 3 which in this case is .#. so the third is. and so on. and if it landed on # count++. here's my code but it doesn't seem to work-

public static Scanner reader = new Scanner(System.in);

public static void main(String[] args) {
    String a = "";//starts empty
    int cnt = 0, b = 0;//cnt++ if '#', b helps us move 3 to the right each time
    a = reader.nextLine(); //gets the string
    if (b + 3 > a.length() - 1) {//checks that the length when moving 3 doesn't exceed string length
        b = 0;// if it does exceed then go all the way to the left of the string
    }
    b += 3;//move right 3
    a = reader.nextLine(); //get next line from the input
    while (a != "-1") {//until there is -1 (to stop getting input) it continues to check
        char c = a.charAt(b -
                1);//takes whats at place b(-1 is because first index is 0 and not1) and note this is three to the right and after it moves down a row!// is line 23
        if (c == '#' && a != "") {//checks if string isn't empty and if the char equals #
            cnt++;//if yes count++
        }
        if (b + 3 > a.length() - 1) {//explained above
            b = 0;
        }
        b += 3;
        a = reader.nextLine();
    }


    System.out.println(cnt);
}

errors I get is the following:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 26
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:702)
    at q.qq.main(Advent3.java:23)

help would be appreciated. let me know if something isn't clear or even how to improve:) thanks code after talking in chat and trying to improve:

public class Advent3 {
    public static Scanner reader= new Scanner(System.in);
    public static void main(String[] args){
            int b=4,cnt=0,line=1,s;
            String str="";
            char charsign;
            str=reader.nextLine();
            s=str.length();
            str=reader.nextLine();

            while(str!=""||str!="-1") {
                line++;
                s=str.length();
                if(b%str.length()==0) {
                    charsign= str.charAt(10);
                }
                else {
                    charsign= str.charAt(b%str.length()-1);
                }
                if(charsign=='#') {
                    cnt++;
                    System.out.println(cnt);
                }
                b+=3;
                str=reader.nextLine();
            }   
            System.out.println(str);

            System.out.println(cnt);
    }
    }

the code itself works and I think I'm very close but it doesn't print the right answer! I'm stuck so help would be much-appreciated thanks!

Upvotes: 3

Views: 104

Answers (1)

Oleg Cherednik
Oleg Cherednik

Reputation: 18255

public static int count(Scanner scan) {
    int res = 0;
    int col = -1;
    String str;
    boolean check = false;

    while (!"-1".equals(str = scan.nextLine())) {
        if (col >= str.length())
            col %= str.length() - 1;
        if (check && str.charAt(col) == '#')
            res++;

        col += 3;
        check = true;
    }

    return res;
}

Demo:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println(count(scan));  // 5
}

Input:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.
#..#...##.####..###.....#......
..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1

Output:

...#.#.......##....#.......#...
.#0...#.##.#..#.......#.....##.
#..#.0.##.####..###.....#......
..#...##0..#...#.#......#...#.#
.##.##.#...1.....#.##..##......
.#...#.#.##.##2..#...#...#.....
.#..##..#....##.#3....##....##.
..#...##....#..###..3.....##...
.#..#..#.#....#.#...#.#3.....#.
.##.....#...#..#..#..#...#4#...
.#...#....#..#...........###.4.
..4..#...........##.#......#...
.....5....##......##..#.#......
-1

5

Upvotes: 2

Related Questions