Gindarel
Gindarel

Reputation: 1

Why it doesn't read in the question marks?

This program runs perfectly, except when I use questiom mars in the input like: what is this??????

It should count the duplicates in a string, and print out the highest number of how many times it was repeated in a row. Any other character is okay (#,*...), But in this case it gives back the numeber: 1. Which is wrong since there are 6 question marks. That's why I suspect there is something wrong between nextLine() function and "?" or Char class and "?". But I can't find the answer anywhere.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        String kell = scan.nextLine();
        char [] sor = kell.toCharArray();
        int max = kell.length();
        scan.close();
        int rész = 1;
        int vég = 1;
        boolean egyezett = false;

        for(int i=0; i<max-1; i++)
        {   
            if (sor[i]== sor[i+1]){   
                rész++;
                egyezett = true;
            }else{
                if (egyezett){
                    if (rész>vég){
                        vég= rész;
                        egyezett = false;
                        rész = 1;
                    } else{
                        egyezett = false;
                        rész = 1;
                    }
                }                    
            }
        }                                        
        System.out.println(vég);
    }
}

Upvotes: 0

Views: 50

Answers (1)

jingx
jingx

Reputation: 4024

This doesn't have to do with question marks in particular, but rather the fact that the duplicates occur thru the end of the string. sor[i]== sor[i+1] is always going to be true, resz is always increased, but veg never has a chance of getting assigned resz's value.

Upvotes: 1

Related Questions