Program #9 requires you to read an input file line by line and scramble each wor
ID: 3652717 • Letter: P
Question
Program #9 requires you to read an input file line by line and scramble each word on that line. Once those words are scrambled you must print the line out with the words in the original order (but scrambled) to an output file. This process repeats for each line of the input file.With each line from the input file you will also print the original words from that line to the screen but sorted by their length rather than alphabetical order.
Input File= A recent study has shown that even when words are jumbled we can read them
as long as the two letters at the front and back are kept in place.
This finding indicates that people have hardwired pattern recognition built into our brains.
Do not use ArrayList
Do not use .asList()
Do not use anything that is called like: Collections.someMethod()
Use String .split(" ") that is built into String
Use String .toCharArray() that is built into String
Use Random .nextInt()
Use .nextInt()
You must write the loop that shuffles the letters of the String by calling .nextInt(
modulus)
And swapping letters.
/*
PROGRAM #9
ALGORITHM:
For each line in the input file
Split the line into an array of words
For each word
split word into an array of chars
shuffle all the chars randomely (but leave first and last chars alone)
make a new String out of the char array
write that new scrambled word to the output file followed by a space
Write a newline to the outfile
Sort that array of words (original unscrambled) by length - shortest to longest
Print those words to the screen all on a single line in their sorted order
close files
*/
import java.io.*;
import java.util.*;
public class Program6
{
// This is an instance member of the class and is "global" to all the code
// You don't have to pass it in to the methods of this class. All the code in this file can see this variable
static Random r = new Random( 17 ); // We seed the generator for repeatable results
public static void main (String[] args) throws IOException
{
// GIVEN AS IS . DO NOT MODIFY. IT VERIFIES THAT THE USER PUT A WORD ON THE COMMAND LINE
if (args.length < 2 ) // Failed to enter input file and output file names
{
System.out.println("FATAL CMD LINE ERROR: Must enter a name of input file and name of output file on the command line! ");
System.exit(0);
}
Scanner infile = new Scanner( new FileReader( args[0] ) );
PrintWriter outfile = new PrintWriter( args[1] );
} // END MAIN
Please help me figure it out. I have an exam on this on Friday please help..
Explanation / Answer
Please rate...
Program6.java
=======================================================
import java.io.*;
import java.util.*;
public class Program6
{
// This is an instance member of the class and is "global" to all the code
// You don't have to pass it in to the methods of this class. All the code in this file can see this variable
static Random r = new Random( 17 ); // We seed the generator for repeatable results
public static void main (String[] args) throws IOException
{
// GIVEN AS IS . DO NOT MODIFY. IT VERIFIES THAT THE USER PUT A WORD ON THE COMMAND LINE
if (args.length < 2 ) // Failed to enter input file and output file names
{
System.out.println("FATAL CMD LINE ERROR: Must enter a name of input file and name of output file on the command line! ");
System.exit(0);
}
Scanner infile = new Scanner( new FileReader( args[0] ) );
PrintWriter outfile = new PrintWriter( args[1] );
//BODY
System.out.println("Sorted words in each Lines: ");
while(infile.hasNextLine())
{
String t=infile.nextLine();
StringTokenizer tokenizer=new StringTokenizer(t);
String ar[]=new String[tokenizer.countTokens()];
int c=0;
while(tokenizer.hasMoreTokens())
{
String t1=tokenizer.nextToken();
//Array [will be used for sorting]
ar[c++]=t1;
//Scrambling
if(t1.length()>3)
{
char t2[]=new char[t1.length()];
int i;
for(i=0;i<t1.length();i++)
{
t2[i]=t1.charAt(i);
}
for(i=1;i<(t1.length()-1);i++)
{
int r1=r.nextInt(t1.length()-2)+1;
int r2=r.nextInt(t1.length()-2)+1;
char t3=t2[r1];
t2[r1]=t2[r2];
t2[r2]=t3;
}
String t4="";
for(i=0;i<t1.length();i++)
{
t4=t4+t2[i];
}
outfile.print(t4+" ");
}
else outfile.print(t1+" ");
}
outfile.println();
//Sorting
int i,j;
for(i=0;i<ar.length;i++)
{
for(j=0;j<(ar.length-2);j++)
{
if(ar[j].length()>=ar[j+1].length())
{
String t5=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t5;
}
}
}
for(i=0;i<ar.length;i++)
{
System.out.print(ar[i]+" ");
}
System.out.println();
//Sorting
}
//BODY
infile.close();
outfile.close();
} // END MAIN
} //END CLASS PROGRAM6
=======================================================
Sample output:
====================================================
File input.txt
===========================================================
File output.txt
===========================================================
Output Screenshot:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.