Random method help?
I have a project for school I'm working on that requires us to manipulate Strings. One of the conditions for the project is that if one of the words in the String is 4 letters long, or longer, I have to scramble all the letters inside of the word except for the first and last letter. For example, if I have the String "I like to listen to Steve Aoki", the result should look similar to this " I lkie to ltiesn to Setve Akoi". Is there a way to use the Random method where if a certain letter has been used, it cannot be used again? This is due in about 9 hours and this is the last parameter I have yet to fill. Thanks!
EDIT: So here's the code. I'm truly a beginner in Java, so it's probably needlessly long and very messy. I found out that my teacher simply wants the letters inside the word to be reversed. Now, my only problem is that for some reason, a bunch of my words are cut off. Also, before you say anything, any punctuation at the start and end are to be kept, but any punctuation inside of the word is to be erased, so don't becomes dont but !Amazing stays !Amazing. import java.io.; import java.util.;
public class StringMythsWordManipulation
{
public static void main(String[] args)
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader("sentences.txt"));
String line = br.readLine();
while(line != null)
{
wordMuddle(line);
line = br.readLine();
}
}
catch(IOException ioe){};
}
//TODO public static void wordMuddle(String line) { int length = line.length()-1; int counter = 0; for (int a = 0; a < length; a++) { if (line.charAt(a) == ' ') { String word = line.substring(counter, a); counter = a; int lengthOfWord = word.length()-1; String frontPunctuationOfWord = ""; for (int b = 0; b<=lengthOfWord; ) { if (word.charAt(b) < 'A' || word.charAt(b) > 'z') { b++; }
else
{
frontPunctuationOfWord = word.substring(0,b);
word = word.substring(b, word.length());
break;
}
}
lengthOfWord = word.length()-1;
String backPunctuationOfWord = "";
int lengthOfFullWord = lengthOfWord;
for (int b = 0; lengthOfWord>=b; )
{
if (line.charAt(lengthOfWord) < 'A' || line.charAt(lengthOfWord) > 'z')
{
lengthOfWord--;
}
else
{
backPunctuationOfWord = line.substring(lengthOfWord+1, lengthOfFullWord+1);
word = word.substring(b, lengthOfWord+1);
break;
}
}
System.out.print(frontPunctuationOfWord);
lengthOfWord = word.length()-1;
word = word.replaceAll("[^A-Za-z, '32']", "");
counter = a;
if (lengthOfWord > 3)
{
String jumbleThis = word.substring(1,lengthOfWord-1);
String firstLetter = word.substring(0,1);
String lastLetter = word.substring(lengthOfWord-1,lengthOfWord);
System.out.print(" " + firstLetter);
int lengthOfJumble = jumbleThis.length();
for (int c = 0; lengthOfJumble>c;)
{
System.out.print(jumbleThis.charAt(lengthOfJumble-1));
lengthOfJumble--;
}
System.out.print(lastLetter);
}
else
System.out.print(word);
System.out.print(backPunctuationOfWord);
}
}
}
}
Posted Dec 2, 2012, 7:48 PM UTC · edited