Hi I need some help with this project I'm working on. I need to write a program which scrambles the intermediate letters of words while keeping the first and last letters the same. The paragraph comes from a .txt file and it is three lines long. I have been able to successfully scramble the first line of the file, but the next two lines aren't showing up. My code I have written is below, any help would be greatly appreciated.
Scanner infile = new Scanner( new FileReader( args[0] ) );
PrintWriter outfile = new PrintWriter( args[1] );
int pos, start=0;
char first, last;
String text = infile.nextLine(); //<<< I think my problem has something to do with this...
Random generator = new Random();
while (text.length()>0){
int whiteSpace = text.indexOf(" ", start);
if (whiteSpace<0){break;} else{
first = text.charAt(start);
last = text.charAt(whiteSpace-1);
if ((whiteSpace-1)-start <=0)
System.out.print(text.charAt(start)+ " ");
else {
String eachWord = text.substring(start+1 , whiteSpace-1) ;
//this will give me the substring to scramble, need a integer to randomize?
System.out.print(first);
// this is the first character for each word. need the last and scramble the middle
while(eachWord.length()>0){
pos = generator.nextInt(eachWord.length()); //outputs a random value of the size
System.out.print(eachWord.charAt(pos));
// onces i choose that racndom character i need to take it out of the list.
if (pos==0)
eachWord = eachWord.substring(1);
else if (pos == eachWord.length()-1 )
eachWord = eachWord.substring(0,pos);
else
eachWord = eachWord.substring(0,pos) + eachWord.substring(pos+1, eachWord.length());
}
System.out.print(last + " ") ;//this is the last charater of each word!
}
start = whiteSpace+1;
}}
int lastWhiteSpace = text.lastIndexOf(" ");
System.out.print(text.charAt(lastWhiteSpace+1));
String lastWord = text.substring(lastWhiteSpace+2 , text.length()-2);
while (lastWord.length()>0){
int lpos = generator.nextInt(lastWord.length());
System.out.print(lastWord.charAt(lpos));
if (lpos==0)
lastWord = lastWord.substring(1);
else if (lpos==lastWord.length()-1)
lastWord = lastWord.substring(0,lpos);
else
lastWord = lastWord.substring(0,lpos)+lastWord.substring(lpos +1,lastWord.length());
}
System.out.print(text.charAt(text.length()-2)+".");
}