Guys I need help with this. Its a project that I am doing. I dont want you to give me or fix the code. I want to be able to do that. But I just need someone to tell me what I am doing wrong. I have been staring at it for the last 3 hours and my brain has reached its threshold limit.
Ok the objectives of this project is to take a string of words with spaces in between and then for each word keep the first and the last character but scramble whatever is in the middle so here is my...please don't laugh i am a novice at coding especially in java
/**
*
* @author Lawrence
*/
import java.util.*;
public class scramble {
public static void main(String[] args) {
int randomPos, start=0;
char first, last;
/* String text = "This is a pretty simple paragraph. The season is autumn" +
"and the air is crisp and clear. The leaves are turning a "+
"variety of colors and then sadly drop to the ground";
*/
String text = "What person willing interesting";
Random generator = new Random();
for (int i=0; i<text.length(); i++){
int whiteSpace = text.indexOf(" ", start);
if (whiteSpace<0){break;} else{
first = text.charAt(start);
last = text.charAt(whiteSpace-1);
System.out.print(first);
// this is the first character for each word. need the last and scramble the middle
String eachWord = text.substring(start+1 , whiteSpace-1) ;
//this will give me the substring to scramble, need a integer to randomize?
int b = (whiteSpace-1)-(start+1);
// this gives how many numbers to scramble
for (int n=0; n < b; n++){
randomPos = generator.nextInt(b); //outputs a random beween 0&b)
char a = eachWord.charAt(randomPos); //takes the character at pos
System.out.print(a);
// onces i choose that racndom character i need to take it out of the list.
if((start+1)-randomPos <=1){
eachWord = text.substring(randomPos , whiteSpace-1);}
else if((whiteSpace-1)-randomPos <=1){
eachWord = text.substring(start+1 , randomPos); }
else {
eachWord = text.substring(start+1 , randomPos)+ text.substring(randomPos , whiteSpace-1);}
}
System.out.println(last+ " "+ eachWord) ;//this is the last charater of each word!
start = whiteSpace+1;
}
}
}
}
Thank you in advanced