Please modify the following program . The program from Lab4Program1 initialized
ID: 3550181 • Letter: P
Question
Please modify the following program . The program from Lab4Program1 initialized the array of words by an assignment statement.
Modify the program so that it will read from a file, and the name of the file is given as
a command line argument. For example, if the name of the input file is lab4input.txt,
you would run the program using:
c:>java Lab4Program1 lab5input.txt
each command line argument is stored in the
String array of the main method ("args[]"). If the line below is used to run the
program, args[0] should contain the String "lab4input.txt".
import javax.swing.*;
public class Lab4Program1 {
public static void main(String[] args) {
String[] wordArray = { "hello", "goodbye", "cat", "dog", "red", "green", "sun", "moon" };
String isOrIsNot, inputWord;
while(true){
// This line asks the user for input by popping out a single window
// with text input
inputWord = JOptionPane.showInputDialog(null, "Enter a word in all lower case:");
if(inputWord.equals("STOP"))
System.exit(0);
// if the inputWord is contained within wordArray return true
if (wordIsThere(inputWord, wordArray))
isOrIsNot = "is"; // set to is if the word is on the list
else
isOrIsNot = "is not"; // set to is not if the word is not on the list
// Output to a JOptionPane window whether the word is on the list or not
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}
} //main
public static boolean wordIsThere(String findMe, String[] theList) {
for(int i=0;i<theList.length;i++)
if(findMe.equals(theList[i]))
return true;
return false;
} // wordIsThere
} // class Lab4Program1
Explanation / Answer
import javax.swing.*;
import java.io.*;
class Lab4Program1 {
static String[] wordArray = new String[100];
public static int readTextFile(String fileName) {
int i=0;
FileReader file = null;
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
String line = null;
while ((wordArray[i] = reader.readLine()) != null)
{
//System.out.println(wordArray[i]);
//wordArray[i] = line ;
i = i + 1;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
// Ignore issues during closing
}
}
}
return i;
}
public static boolean wordIsThere(String findMe,int i)
{
for(int j=0;j<i;j++)
if(findMe.equals(wordArray[j]))
return true;
return false;
} // wordIsThere// cla
public static void main(String[] args) {
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = null;
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
fileName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
//System.out.println("File name is, " + fileName);
int i;
i = readTextFile(fileName) ;
String isOrIsNot , inputWord;
// This line asks the user for input by popping out a single window
// with text input
inputWord = JOptionPane.showInputDialog(null, "Enter a word in all lower case:");
if(inputWord.equals("STOP"))
System.exit(0);
// if the inputWord is contained within wordArray return true
if (wordIsThere(inputWord,i))
isOrIsNot = "is"; // set to is if the word is on the list
else
isOrIsNot = "is not"; // set to is not if the word is not on the list
// Output to a JOptionPane window whether the word is on the list or not
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}
} //main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.