jeudi 5 janvier 2017

randomly replace random numbers of char in a string

I'm new to Java and I need some help with my code.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class asdf {
    public static void main(String[] args)
    {
        Random generator = new Random();
        int num1;
        String theWord;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string to rearrange");
        theWord = in.nextLine();
        int length = theWord.length();
        Random times=new Random();
        System.out.println("Length of String:"+length);
        char[] chars=theWord.toCharArray();
        Arrays.sort(chars);
        int timez=times.nextInt();
        num1=generator.nextInt(length);
        char c=theWord.charAt(num1);
        String newWord=new String(chars);
        for(int i=0;i+=1;i<timez)
        {
            newWord=newWord.replace(c, '*');
        }
        System.out.println(newWord);
     }
}

So I was trying to rearrange the string and replace some of the chars with '*', but it didn't work and I don't know how to fix it.




Aucun commentaire:

Enregistrer un commentaire