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 j a v a . u t i l . ? ;
import j a v a . i o . ? ;
/? ?
? C l a s s t h a t can s c r a m b l e l e t t e r s
? @author Barb E r i c s o n
? C o p y r i g h t 2006
?/
public c l a s s S c r a m b l e r
{
// //////////// f i e l d s ////////////////////////////
/? ? random number g e n e r a t o r ?/
private Random randNumGen = new Random ( ) ;
// //////////// c o n s t r u c t o r s //////////////////////
/? ?
? No argument c o n s t r u c t o r
?/
public S c r a m b l e r ( )
{}
// ///////////// methods /////////////////////////
/? ?
? Method t o s c r a m b l e t h e l e t t e r s i n t h e s t r i n g
? @param i n p u t t h e s t r i n g t o s c r a m b l e t h e l e t t e r s i n
? @return t h e new s t r i n g w i t h t h e l e t t e r s s c r a m b l e d
?/
public S t r i n g s c r a m b l e ( S t r i n g i n p u t )
{
char [ ] i n p u t A r r a y = i n p u t . toCharArray ( ) ;
L i s t
// move c h a r a c t e r s i n t o t h e l i s t
f o r ( char currChar : i n p u t A r r a y )
c h a r L i s t . add ( currChar ) ;
// g e t s o m t h i n g t h a t can i t e r a t e t h r o u g h t h e l i s t
Iterator iterator = charList . iterator ( ) ;
int index = 0 ;
char [ ] charArray = new char [ c h a r L i s t . s i z e ( ) ] ;
// l o o p t i l l a l l t h e l e t t e r s a r e used
while ( i t e r a t o r . hasNext ( ) )
{
charArray [ i n d e x ] = c h a r L i s t . remove ( randNumGen . n e x t I n t ( c h a r L i s t . s i z e ( ) ) ) ;
i n d e x ++;
}
return new S t r i n g ( charArray ) ;
}
/? ?
? Method t o s c r a m b l e t h e l e t t e r s i n each l i n e o f a f i l e
? @param i n p u t F i l e t h e p a t h f o r t h e i n p u t f i l e
? @param o u t p u t F i l e t h e p a t h f o r t h e o u t p u t f i l e
?/
public void s c r a m b l e F i l e ( S t r i n g i n p u t F i l e , S t r i n g o u t p u t F i l e )
{
S t r i n g l i n e = null ;
S t r i n g s c r a m b l e d L i n e = null ;
// t r y t o do t h e f o l l o w i n g
try {
// c r e a t e t h e b u f f e r e d r e a d e r
BufferedReader reader =
new B u f f e r e d R e a d e r (new F i l e R e a d e r ( i n p u t F i l e ) ) ;
// t r y t o open t h e b u f f e r e d w r i t e r
BufferedWriter writer =
new B u f f e r e d W r i t e r (new F i l e W r i t e r ( o u t p u t F i l e ) ) ;
// Loop w h i l e t h e r e i s more d a t a
while ( ( l i n e = r e a d e r . r e a d L i n e ( ) ) != null )
{
// s c r a m b l e t h e l i n e
scrambledLine = scramble ( l i n e ) ;
// w r i t e o u t t h e s c r a m b l e d l i n e
w r i t e r . write ( scrambledLine ) ;
w r i t e r . newLine ( ) ;
}
// c l o s e t h e r e a d e r and w r i t e r
reader . close ( ) ;
writer . close ( ) ;
} catch ( FileNotFoundException ex ) {
SimpleOutput . showError ( ” Couldn ’ t f i n d ” + i n p u t F i l e +
” please pick i t . ” ) ;
inputFile = FileChooser . pickAFile ( ) ;
scrambleFile ( inputFile , outputFile ) ;
} catch ( E x c e p t i o n ex ) {
SimpleOutput . showError ( ” E r r o r with f i l e ” + i n p u t F i l e ) ;
ex . p r i n t S t a c k T r a c e ( ) ;
}
}
/? ?
? Main method f o r t e s t i n g
?/
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
S c r a m b l e r s c r a m b l e r = new S c r a m b l e r ( ) ;
s c r a m b l e r . s c r a m b l e F i l e ( ” c : / i n t r o ?prog?j a v a / b o o k C l a s s e s F i n a l / textToScramble . t x t ” ,
” c : / i n t r o ?prog?j a v a / b o o k C l a s s e s F i n a l / scrambledText . t x t ” ) ;
}
}
```
You might also like to view...
Which layout would you use if you wanted a new content placeholder to the right of an existing placeholder?
A) Two Content B) Comparison C) Title and Content D) Content with Caption
Answer the following statements true (T) or false (F)
1. “Pretty Good Privacy” is an alternative email security protocol. 2. An Administrative Management Domain is a directory lookup service that provides a mapping between the name of a host on the Internet and its numerical address. 3. E-mail is the most common distributed application that is widely used across all architectures and vendor platforms. 4. A Message Transfer Agent is like a packet switch or IP router in that its job is to make routing assessments and to move the message closer to recipients. 5. A Message Store cannot be located on the same machine as a MUA.