Here's what I have so far, I'm just having trouble writing the second part that will say that the string in the text file isn't there if the user enters a number thats not saved in the text file (data3.txt). Also how would I make it so that the program keeps running and doesn't stop after 1 user input? Thanks!
// Import io so we can use file objects
import java.io.*;
public class searchfile {
public static void main(String args[]) {
try {
// Open the file data3.txt as a buffered reader
BufferedReader bf = new BufferedReader(new FileReader("data3.txt"));
// Start a line count and declare a string to hold our current line.
int linecount = 0;
String line;
// Let the user know what we are searching for
// i.e. just printing to the console, not actually searching.
System.out.println("Searching for " + args[0] + " in file...");
// Loop through each line, stashing the line into our line variable.
while (( line = bf.readLine()) != null)
{
// Count lines. We read a line this variable (linecount) increments by one
linecount++;
for(int index = 0; true; index++) {
try {
int indexfound = line.indexOf(args[index]);
// If greater than -1, means we found the word
if (indexfound > -1) {
System.out.println("Word was found at position " + indexfound +" on line " + linecount);
else
System.out.println("word not found");
}
}
catch(ArrayIndexOutOfBoundsException ex) {
break;
}
}
}
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
}