Shuffle the characters of a string in Java
This is a program which accepts a string using the BufferdReader and InputStreamReader class, and then shuffle the characters present inside the string and generate a new string which is displayed in the console. Basically the characters of the string are stored in a Character List using the for each loop. Then the characters in that list is randomly removed and appended in a StringBuilder object output.
Screenshots [Demo]
The Code:
Founds Bugs, or want to give suggestions,
Drop them here !
Screenshots [Demo]
The Code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; class Brain { public static void main(String []as)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter The String: "); String input=br.readLine(); List<Character> characters = new ArrayList<Character>(); for(char c:input.toCharArray()){ characters.add(c); } StringBuilder output = new StringBuilder(input.length()); while(characters.size()!=0){ int randPicker = (int)(Math.random()*characters.size()); output.append(characters.remove(randPicker)); } System.out.println("THe Shuffled is : "+output.toString()); } }
Founds Bugs, or want to give suggestions,
Drop them here !
Comments
Post a Comment