package noveltry; // package name starts with lower case import java.util.Random
ID: 3581155 • Letter: P
Question
package noveltry; // package name starts with lower case
import java.util.Random;
public class FirstNovel {
String novel; // variable names start with lower case
String [] words; // no need for static variable
public FirstNovel(String novel) {
this.novel = novel;
this.words = novel.split(" ");
}
public void switchUp() { // method names start with lower case
String [] switchedUp = new String[words.length]; // initialise the array
for (String word : words) {
Random random = new Random();
int index = random.nextInt(words.length);
while (switchedUp[index] != null) { // find a new position for the word if something is already there
index = random.nextInt(words.length);
}
switchedUp[index] = word;
}
for (String word : switchedUp) { // loop through all words and print them out
System.out.print(word + " ");
}
System.out.println();
}
public static void main(String[] args){
FirstNovel novel = new FirstNovel("You should test with more than two words");
novel.switchUp(); // call the public method, not the static one
}
}
How could i make this program take in a 50,000 word novel? Having huge trouble.
Explanation / Answer
Modified Program
Inorder to satisfy the large data to be processed, its better to take data into File and Read that using File Reader and BufferedReader. Reading each line from File and split into words and remaining code follows as you have done.
package noveltry; // package name starts with lower case
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
public class FirstNovel {
String novel; // variable names start with lower case
String [] words; // no need for static variable
public FirstNovel1(String novel) {
this.novel = novel;
this.words = novel.split(" ");
}
public void switchUp() { // method names start with lower case
String [] switchedUp = new String[words.length]; // initialise the array
for (String word : words) {
Random random = new Random();
int index = random.nextInt(words.length);
while (switchedUp[index] != null) { // find a new position for the word if something is already there
index = random.nextInt(words.length);
}
switchedUp[index] = word;
}
for (String word : switchedUp) { // loop through all words and print them out
System.out.print(word + " ");
}
System.out.println();
}
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("D://Sample.txt"); // Keep any huge data contains 50000 words
BufferedReader br = new BufferedReader(fr);
String line;
//StringBuffer sb = new StringBuffer();
FirstNovel1 ns= null;
while((line=br.readLine())!=null)
{
ns = new FirstNovel1(line);
ns.switchUp(); // call the public method, not the static one
}
//FirstNovel novel = new FirstNovel(sb.toString());
//ns.switchUp(); // call the public method, not the static one
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.