I’m doing an exercise and I don’t know why it’s going wrong (print exercise in the post attachment and my code below), what’s wrong?

Asked

Viewed 73 times

-2

Myio is used by my teacher to replace scanner and etc the strings I’m going to use in the exercise are large and diverse so any example you think about is the basis. is my first post, I’m sorry if I stay out of the pattern!

import java.util.Random;
import java.util.*;

public class Aleatorio {


    public static void alterarString(String in,char firstletra,char secundletra)
    {
        String out = in;

        for(int i = 0;i < alterarString.length(); i++)

        {
            if(in.charAt(i) == firstletra)
            {
                out.charAt(i) == secundletra;
            }
        }
        return out;

    }


    public static boolean theEnd(String s)
    {
        return (s.length() == 3 && s.charAt(0) == 'F' && s.charAt(1) == 'I' && s.charAt(2) == 'M');
    }


    public static void main(String[] args)
    {
        String[] frase = new String[1000];
        int numString = 0; 
        do {
        
        String[] frase[numString] = MyIo.nextLine(); 
        }
        while(theEnd(frase[numString++]) == false);
        numString--;
       
       
        Random gerador = new Random();
        gerador . setSeed (4);

        char secundletra;
        char firstletra;
        for(int i =0;i<numString; i++)
        {

        char firstletra = (char)('a'+(Math.abs( gerador.nextInt())%26)));
        char secundletra = (char)('a'+(Math.abs( gerador.nextInt())%26)));
        MyIo.println(alterarString (alterarString(frase[i],firstletra,secundletra)));

        }
 }
            
  • The variable alterrString within the function alterrString not declared. A typo?

  • The right thing is java.util.Random import;, with a capital "R".

  • @Adrianosiqueira I didn’t even put,the variable I want in the function alterrString is String out,I would need to declare a variable with the same function name?

  • I don’t know if I understand the purpose of the code, but maybe this will help: https://answall.com/a/449163/112052

  • @Gabriel, what exactly is this code supposed to do?

  • -it has to take a string and draw two random minuscule letters for dps replace all occurrences of first letter in the string by the second and returns the string with the changes made @Adrianosiqueira

Show 1 more comment

1 answer

0


See if that’s what you need:

package string;

import java.util.Random;

public class Aleatorio {

    public static String alterarString(String string, char primeiraLetra, char segundaLetra) {
        String pesquisa = String.valueOf(primeiraLetra);
        String novo = String.valueOf(segundaLetra);
        return string.replaceAll(pesquisa, novo);
    }

    public static boolean theEnd(String s) {
        return s.equals("FIM");
    }

    public static void main(String[] args) {
        String[] frase = new String[1000];
        int posicao = 0;

        do {
            frase[posicao] = MyIo.nextLine();
        } while (!theEnd(frase[posicao++]));
        
        posicao--;

        Random gerador = new Random();
        gerador.setSeed(4);

        char primeiraLetra;
        char segundaLetra;
        
        for (int i = 0; i < posicao; i++) {
            primeiraLetra = (char) ('a' + (Math.abs(gerador.nextInt()) % 26));
            segundaLetra = (char) ('a' + (Math.abs(gerador.nextInt()) % 26));
            MyIo.println(alterarString(frase[i], primeiraLetra, segundaLetra));
        }
    }
}
  • You helped so much!! Thank you! as to starting I still error a lot in the matter of writing the right code,I was already programming in C so I went to java in college and I could not follow,Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.