Word Scramble Program Help
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)+".");
}
Re: Word Scramble Program Help
Quote:
Originally Posted by
beginneratjava
...I have been able to successfully scramble the first line of the file, but the next two lines aren't showing up....
Put print statements just before and just after the statement in your code that reads the first line.
Put print statements just before and just after the statement in your code that reads the second line.
Put print statements just before and just after the statement in your code that reads the third line.
Here, I'll start:
Code java:
.
.
.
// Presumably this is in main()
Scanner infile = new Scanner(new FileReader( args[0]));
PrintWriter outfile = new PrintWriter(args[1]);
int pos, start=0;
char first, last;
System.out.println("Reading first line from " + args[0]);
String text = infile.nextLine(); //<<< I think my problem has something to do with this...
System.out.println("First line:\n" + text + "\nAnd now, on with the show...\n");
Here is my file inwords.txt:
The quality of mercy is not strained.
It droppeth as the gentle rain from heaven
Upon the place beneath: it is twice blest;
It blesseth him that gives and him that takes
And here's the output from a run with inwords.txt as the first argument on the java command line:
Reading first line from inwords.txt
First line:
The quality of mercy is not strained.
And now, on with the show...
The qitualy of mecry is not sarnetid.
Cheers!
Z
Re: Word Scramble Program Help
That doesn't really help me. I can't have that extra stuff printed in my output, and my original code already scrambles the first line, the problem is that it stops there and doesn't do anything to my next lines of text, they don't show up at all, not even unscrambled they just don't show up in my output.
Re: Word Scramble Program Help
Quote:
I can't have that extra stuff printed in my output,
You can always remove it after you use it to see what is going on.
Quote:
and my original code already scrambles the first line, the problem is that it stops there and doesn't do anything to my next lines of text, they don't show up at all, not even unscrambled they just don't show up in my output.
The sooner you figure out what the code is doing, the sooner you can start to make changes to the code.
Re: Word Scramble Program Help
Quote:
Originally Posted by
beginneratjava
.... it stops there and doesn't do anything to my next lines of text...
Well, that was kind of the point of my suggestion: Where in your program to you read more than one line? The program won't read more than one line unless and until you tell it to.
I mean, if you can't find the statement in your program that reads the second line (and the third and so forth), you have to put such statements in the code. These programs don't write themselves according to your intent, right? They do what you tell them to.
Bottom line: Generally speaking, with a Scanner you will need a loop of some kind to read all of the lines in a file. Look at documentation and tutorial material on use of Scanner methods to do the deed.
Cheers!
Z