Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
using java, find if the occurrence of a given letter from given range if the occurrence is true for the string

using java, find if occurence of letter from given range if is true for the string I have a list of these strings which are formatted like the following: there are more but here are some examples

9-11 w: wwtxmsjwjwlw separate to: 9-11//int-int/range , w //char/letter , wwtxmsjwjwlw//string we are checking --false

5-6 g: wggghg separate to 5-6 , g , wgggghg --true

1-5 g: dxggdggb seperate to 1-5 , g , dxggdggb --true

5-13 f: lfgdplfffxffswck separate to 5-13 , f, lfgdplfffxffswck --true

6-7 z: zzzzgzzzz separate to 6-7 , z , zzzzgzzzz -- check if z is repeated 6-7 times in string zzzzgzzzz which here z is repeated 8 times so its not true --false these are examples of given strings that are split into 3 different parts that share the same index in the different arrays I put them in. The aim of the question is To take the letter/char occurrence, basically checking if the occurrence from the given range int-int is true in the string of it is true then cnt++, it's not true- nothing happens-cnt doesn't change. -but I want to check if the letter appears in the given range to the string is true- if this makes sense. here is my attempt at coding it:

    public static Scanner reader= new Scanner(System.in);
    public static void main(String[] args){
        int g=1000,cnt2=0;
        String[] a1 = new String[g];
        String[] a2 = new String[g];
        String[] a3 = new String[g];
        int cnt=0;
        String regexp = "(?<one>\\d+-\\d+)\\s*(?<two>\\w+)\\s*:\\s*(?<three>\\w+)";
        for(int i=0;i<10000;i++) {
            String str = reader.nextLine();
            cnt++;
            Pattern pattern = Pattern.compile(regexp);
            Matcher matcher = pattern.matcher(str);
            if (matcher.matches()) {
                a1[i]= matcher.group("one");
                a2[i]= matcher.group("two");
                a3[i]= matcher.group("three"); 
        }
            if(a3[i].matches("[a2[i]{a1[i]}")==true) {//("[a2[i]{a1[i]}") --a1[i] is error since its int-int and not int,int cause in regex range is {int, int} so how can i change that - to ,or change it so it works?
                cnt2++;
            }
        }
        System.out.println(cnt);
        System.out.println(cnt2);

    }

cnt2 is supposed to tell me how many of the inputs match the occurrence of that specific letter, doesn't work so help would be appreciated, let me know please if there are more efficient ways than my idea with the arrays. Help improving the question would also be helpful thanks

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

just solved this question with the following code for those who are interested-

public static Scanner reader= new Scanner(System.in);
    public static void main(String[] args){
        int cnt2=0;
        String a1="";
        String a2="";
        String a3="";
        int cnt=0;
        String regexp = "(?<one>\\d+-\\d+)\\s*(?<two>\\w+)\\s*:\\s*(?<three>\\w+)";
        Pattern pattern = Pattern.compile(regexp);
        String str = reader.nextLine();
        while (!str.equals("-1")) {

            System.out.println(str);
            cnt++;

            Matcher matcher = pattern.matcher(str);
            if (matcher.matches()) {
                a1= matcher.group("one");
                a2= matcher.group("two");
                a3= matcher.group("three");
            }
            int min=Integer.parseInt( ""+ a1.split("-")[0]);
            int max=Integer.parseInt( ""+  a1.split("-")[1]);
            int acurences =("!"+a3+"!").split(a2).length-1;
            if(acurences>=min &&acurences<=max) {
                cnt2++;
            }
            str = reader.nextLine();
        }
        System.out.println(cnt);
        System.out.println(cnt2);

    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda