Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

taking computer science 002, first time coding, but my code isn\'t working. Your

ID: 3713294 • Letter: T

Question

taking computer science 002, first time coding, but my code isn't working.

Your program will read in a text file one line at a time, seramble each word on the line and write the line back out 1. Create a Progó project in NetBeans 2. Copy the text file that you want to scramble into the Prog6 folder. 3 Create a BufferedReader object for reading in the textfile. Use the args array perameter to get the file name. 4 Add the file name to the properties for the projeet 5. For each line in the file, after reading in the line, do the tollowing Use the String split method to break up the line into an array of words 1 Use String toChar Array to break up the String into an array of characters (called 2 Leaving the first and last letters oaf the word in pace da the follow ing length 2 5.2 For each word in the aray do the following: letters). times. t Generate a random number from 2 to length-2 fealled inde Us java util Random nextlnt for this 2 Swap letsers! !1 with leterst undes]. 5.3 Write out sach serumbled word in the urray on a singk lins with a spoce in between them 6. Test the pwgram on your ieau fie 7 Put the usual comment at the top of source code tile

Explanation / Answer

Program with comments:

/*
*
* Make sure that each word in the test file has more than 4 characters each
* because the requirement states that the random number generated must be between 2 and length-2
* There is no specific requirement stated for the scenario of length of word being less than or equal to 4
*
*/

package com.chegg.Prog6;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.Random;

//Main class Prog6

public class Prog6 {

public static void main(String[] args) {

// TODO Auto-generated method stub

//Creating FileReader object to read the file

FileReader fr = null;

//Reading the filepath from the commandline argument. Set the filepath under Arguments from the Project Properties option. You can refer it here - http://netbeans.tusharjoshi.com/2009/02/using-command-line-arguments-in.html

try {

fr = new FileReader(args[0]);

}

//Triggering exception if file not found or if path is not mentioned correctly in the args[0]

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}   

//Creating BufferedReader object and passing the filereader reference to it

BufferedReader br=new BufferedReader(fr);  

//Creating Random class object as per requirement

Random r = new Random();

//Declaring variable line which is used to store each line from the file

String line;

try {

//Reading line by line of the file and storing in line

while ((line = br.readLine()) != null) {

//Using String.split() to break up the line into words and storing into an array arr

String arr[] = line.split(" ");

//Iterating through the words array

for(String s:arr){

//Converting each word into character array called letters

char letters[] = s.toCharArray();

//Retrieving length of the word

int length = letters.length;

//Scrambling the word length-2 times as per requirement

for(int i = 0;i<length-2;i++) {

//Setting up random number generator to generate numbers between 2 to length-2

int low = 2;

int high = length-2;

//Generating the random number and storing in index variable

int index = r.nextInt(high-low) + low;

//Storing the letters[1] into a temp character variable

char temp = letters[1];

//Swapping the letters[1] with letters[index] as per requirement

letters[1] = letters[index];

letters[index] = temp;

}

//Converting the scrambled letters back into a string

String output = new String(letters);

//Displaying the scrambled words with space in between them

System.out.print(output+" ");

}

System.out.println();

}

}

//Triggering any errors

catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

//Closing the BufferedReader stream

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}   

try {

//Closing the FileReader stream

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}   

}

}