Sei sulla pagina 1di 2

import javax.swing.

*;
public class Lab5Program1 {
public static void main(String[] args)
{
String[] myArray = new String[100];
int myArrayLength;
String myFile = "lab5input.txt";
String isOrIsNot, inputWord;
myArrayLength = inputFromFile(myFile, myArray);
// This line asks the user for input by popping out a single win
dow
// with text input
inputWord = JOptionPane.showInputDialog(null, "Enter a word in a
ll lower case:");
// if the inputWord is contained within wordArray return true
if (wordIsThere(inputWord, myArray))
isOrIsNot = "is"; // set to is if the word is on the lis
t
else
isOrIsNot = "is not"; // set to is not if the word is no
t on the list
// Output to a JOptionPane window whether the word is on the lis
t 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(theList[i].equals(findMe)
return true;
return false;
} // wordIsThere
private static int inputFromFile(String filename, String[] theList)
{
TextFileInput in = new TextFileInput(filename);
int lengthFilled = 0;
String line = in.readLine();
while (lengthFilled < theList.length && line != null )
{
theList[lengthFilled++] = line;
line = in.readLine();
} // while
if ( line != null )
{
System.out.println("File contains too many numbers.");
System.out.println("This program can process only " +
numbers.length + " numbers.");
System.exit(1);
} // if
in.close();
return lengthFilled;
}

} // class Lab5Program1

Potrebbero piacerti anche