0

Here is my code. It's for class and supposed to simulate "dyslexia" by replacing "dbqp" with eachother randomly.

import java.util.Scanner;

public class Dyslexia
{
  public static void main(String[] args)
  {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a String");
    String str = scanner.nextLine();
    String output = "";
    
    String e = null;
    
    for (int i = 0; i < str.length(); i++)
    {
      int randomInt = (int)(Math.random() * 4) + 1; 
      System.out.println(randomInt);
      
      if (randomInt == 1)
      {
        e = "d";
      }
      if (randomInt == 2)
      {
        e = "b";
      }
      if (randomInt == 3)
      {
        e = "q";
      }
      if (randomInt == 4)
      {
        e = "p";
      }
      output = str.replaceAll("[dbqp]", e);
      System.out.println(output); 
    }
  }
}   

The output I am currently getting (lets say I type in qpdb) is:

1
dddd
3
qqqq
3
qqqq
3
qqqq

Ignore the numbers for debugging but given those random numbers the output I would be aiming for is:

1
d
3
q
3
q
3
q
  • 1
    Could you please provide sample expected input and output which might help us in understanding your problem better? When you say,"I wanted the program to just print out "dqqq" in that instance." Which instance are you referring to? – Rohan Kumar Dec 09 '20 at 17:29
  • https://stackoverflow.com/questions/3316674/how-to-shuffle-characters-in-a-string-without-using-collections-shuffle – NoClue Dec 09 '20 at 17:38

1 Answers1

0

You're doing a replaceAll call which will just set them all to the same value. If you want each character to be considered separately, you need to evaluate and select at random for each character then rebuild a new String with it.

import java.util.List;
import java.util.Random;

public class DyslexiaExample
{
  public static void main(String[] args) {
    System.out.println(simulateDyslexia("Some possible bountiful quotes."));
  }
  
  public static String simulateDyslexia(String sentence) {
    List<Character> vals = List.of('d', 'b', 'q', 'p');
    StringBuilder builder = new StringBuilder();
    
    Random random = new Random();
    
    for(char c : sentence.toCharArray()) {
      if(vals.contains(c)) {
        builder.append(vals.get(random.nextInt(vals.size())));
      }
      else {
        builder.append(c);
      }
    }
    
    return builder.toString();
  }
}

Sample output: Some bossible dountiful buotes.

If you want some consistency between which letter gets swapped with another letter, then consider generating a Map.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DyslexiaExample
{
  public static void main(String[] args) {
    System.out.println(simulateDyslexia("And quite possibly even more boisterous bountiful quotes."));
  }
  
  public static String simulateDyslexia(String sentence) {
    List<Character> vals = List.of('d', 'b', 'q', 'p');
    StringBuilder builder = new StringBuilder();
    
    List<Character> shuffleVals = new ArrayList<>(vals);
    Collections.shuffle(shuffleVals);
    
    Map<Character, Character> swapMap = new HashMap<>();
    for(int i = 0; i < vals.size(); i++)
      swapMap.put(vals.get(i), shuffleVals.get(i));
    
    for(char c : sentence.toCharArray()) {
      if(swapMap.keySet().contains(c)) {
        builder.append(swapMap.get(c));
      }
      else {
        builder.append(c);
      }
    }
    
    return builder.toString();
  }
}

Sample output: Anb puite dossiqly even more qoisterous qountiful puotes.

Tim Hunter
  • 826
  • 5
  • 10