I'm working to a system called Quiz ...
The last thing that remains are the 'clues'. In开发者_运维知识库 present i have
<id value="100">
<question value="Who said E=mc2"/>
<answear value="Einstein"/>
<clue1 value="E*******"/>
<clue2 value="E******n"/>
<clue3 value="Ei****in"/>
</id>
and I want to remove from xml the clues because is hard to do them manually ... so I made something but i failed
public class Test
{
public static void main(String[] argv) throws Exception
{
System.out.println(replaceSubString("Einstein", "*", 3));
}
static String[] letters = {"e","i"};
public static String replaceSubString(final String str, final String newToken, int max)
{
if ((str == null) || (newToken == null))
return str;
StringBuffer buf = new StringBuffer(str.length());
int start = 0, end = 0;
for(int i = 0; i < letters.length; i++)
{
if(Rnd.get(100) > 50) //50% to add the symbol
{
while ((end = str.indexOf(letters[i], start)) != -1)
{
buf.append(str.substring(start, end)).append(newToken);
start = end + 1;
if (--max == 0)
break;
}
}
}
buf.append(str.substring(start));
return buf.toString();
}
}
Compile Result => 'Einst*in'
the loop doesn't work.. idk .. only the first letter from array is replaced...
if someone offers to help me I would be very grateful..
-Thanks !
How about something like this?
public String generatClue(String answer,int level){
if(level >= answer.length()/2)
return answer.replaceAll("[^ ]","*");
return answer.substring(0,level)
+ answer.substring(level,answer.length()-level).replaceAll("[^ ]","*")
+ answer.substring(answer.length()-level);
}
Output:
generateClue("Einstein",1);
=> E******n
generateClue("Einstein",3);
=> Ein**ein
generateClue("Einstein",4)
=> Einstein
generateClue("Hans Christian Andersen",4)
=> Hans ********* ****rsen
EDIT: Here's one for random characters in the string:
public String generatClue2(String answer,int level){
if(answer.length()==level)
return answer.replaceAll("[^ ]","*");
Random rand=new Random();
for(int i=0; i<level; ++i){
char c;
int n;
do{
n=rand.nextInt(answer.length());
c=answer.charAt(n);
}
while(c == ' ' || c == '*');
answer = answer.substring(0,n) + '*' + answer.substring(n+1);
}
return answer;
}
Output:
generateClue2("Hans Christian Andersen",4);
=> Han* C*ri*tian Ande*sen
generateClue2("Hans Christian Andersen",4);
=> *ans Chr*sti*n An*ersen
generateClue2("Hans Christian Andersen",17);
=> H**s ******i** ***e****
generateClue2("Hans Christian Andersen",23);
=> **** ********* ********
Why not use something like JDOM and grab the XML elements by name?
SAXBuilder b = New SAXBuilder();
Document doc = b.build(pathToFile);
List<?> elements = b.getChildren("clue");
for (Element e : elements ) {
(Element) e.setAttribute("value",
obfuscateClueText(e.getAttribute("value")); //updated, see below
}
In response to your comment, could you write a method that generates the random clue symbols?
private String obfuscateClueText(String clueValue) {
//do something with clueValue
return obfuscatedValue;
}
I tried before this method
public class Test
{
static String s = "Hans Christian Andersen";
public static void main(String[] args)
{
char c = '*';
System.out.println(replaceCharAt(s, 0, c));
}
public static String replaceCharAt(String s, int pos1, char c)
{
StringBuffer buf = new StringBuffer(s);
int max = (s.length()-3), contor = 0;
while (contor < max)
{
for(int i = pos1; i < (s.length()); i++)
{
if(Rnd.get(100) > 50 && contor < max)
{
buf.setCharAt(i, c);
contor++;
}
}
}
return buf.toString();
}
}
the problem is that 'SPACE' from string ..
output: ***s*******i*n *nde**e*
精彩评论