Write a program to read a list of words from a file and then output a new file with the letters scrambled using Random.
What will be an ideal response?
```
import java . u t i l . * ;
import java . i o . * ;
/**
* Clas s t h a t can scramble l e t t e r s
* @author Barb Er icson
* Copyr ight 2006
*/
public clas s Scrambler
{
// //////////// f i e l d s ////////////////////////////
/** random number g ene rator */
private Random randNumGen = new Random( ) ;
// //////////// c o n s t r u c t o r s //////////////////////
/**
* No argument c ons t r u c t o r
*/
public Scrambler ( )
{}
// ///////////// methods /////////////////////////
/**
* Method to scramble the l e t t e r s in the s t r i n g
* @param input the s t r i n g to scramble the l e t t e r s in
* @return the new s t r i n g wi th the l e t t e r s scrambled
*/
public St r ing scramble ( St r ing input )
{
char [ ] inputAr ray = input . toCharArray ( ) ;
Li s t
// move c h a r a c t e r s int o the l i s t
for ( char currChar : inputAr ray )
cha rLi s t . add ( currChar ) ;
// g e t somthing t h a t can i t e r a t e through the l i s t
I t e r a t o r i t e r a t o r = cha rLi s t . i t e r a t o r ( ) ;
int index = 0 ;
char [ ] charArray = new char [ cha rLi s t . s i z e ( ) ] ;
// loop t i l l a l l the l e t t e r s are used
while ( i t e r a t o r . hasNext ( ) )
{
charArray [ index ] = cha rLi s t . remove ( randNumGen . next Int ( cha rLi s t . s i z e ( ) ) ) ;
index++;
}
return new St r ing ( charArray ) ;
}
/**
* Method to scramble the l e t t e r s in each l i n e of a f i l e
* @param i n p u tFi l e the path f o r the input f i l e
* @param o u t p u tFi l e the path f o r the output f i l e
*/
public void s c r ambl eFi l e ( St r ing inputFi l e , St r ing outputFi l e )
{
St r ing l i n e = nul l ;
St r ing scrambledLine = nul l ;
// t r y to do the f o l l owi n g
try {
// c r e a t e the b u f f e r e d r eade r
Buf feredReader r eade r =
new Buf feredReader (new Fi l eReade r ( i n p u tFi l e ) ) ;
// t r y to open the b u f f e r e d wr i t e r
Buf f e r edWr i t e r wr i t e r =
new Buf f e r edWr i t e r (new Fi l eWr i t e r ( outputFi l e ) ) ;
// Loop wh i l e t h e r e i s more data
while ( ( l i n e = r e ade r . r eadLine ( ) ) != nul l )
{
// scramble the l i n e
scrambledLine = scramble ( l i n e ) ;
// wr i t e out the scrambled l i n e
wr i t e r . wr i t e ( scrambledLine ) ;
wr i t e r . newLine ( ) ;
}
// c l o s e the r eade r and wr i t e r
r eade r . c l o s e ( ) ;
wr i t e r . c l o s e ( ) ;
g catch ( Fi leNotFoundException ex ) f
SimpleOutput . showError ( "Couldn ' t f i n d " + i n p u tFi l e +
" p l e a s e pi ck i t . " ) ;
i n p u tFi l e = Fi l eChoos e r . pi ckAFi l e ( ) ;
s c r ambl eFi l e ( inputFi l e , outputFi l e ) ;
g catch ( Except ion ex ) f
SimpleOutput . showError ( "Er ror with f i l e " + i n p u tFi l e ) ;
ex . pr intStackTrac e ( ) ;
}
}
/**
* Main method f o r t e s t i n g
*/
public s tat ic void main ( St r ing [ ] args )
{
Scrambler s c rambl e r = new Scrambler ( ) ;
s c rambl e r . s c r ambl eFi l e ( " c : / int r o¡prog¡java / bo okCla s s e sFina l / textToScramble . txt " ,
" c : / int r o¡prog¡java / bo okCla s s e sFina l / scrambledText . txt " ) ;
}
}
```
You might also like to view...
For each numbering base system, the far right always has a place value of ________
Fill in the blank(s) with correct word
Abstraction separates the purpose of a module from its implementation.
What will be an ideal response?