My assignment was to create a Directory Lookup, that prompts the user for an input (inputting a number i.e. 5) and the program returns whether the input number is in the .txt file or not. Having some trouble, have re-edited my code numerous times already, but here's what I've amounted to so far, any tips, help, advice is extremely appreciated !
import java.util.*; // import scanner
import java.io.*;
public class Homeworkhelp3 {
public static void main(String[] arg){
int[] database; //signifies what database we are going to be using
FileInputStream fstream = null;
try {
fstream = new FileInputStream(arg[0]); //reads file that we are looking for to run program
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));//reading data from ("data3.txt")
String size=null;
int linecount = 0;
try {
size = br.readLine();//indicates the size of the file
} catch (IOException e1) {
e1.printStackTrace();
}
database= new int[Integer.parseInt(size)];//sets the data for program
int counter=0;
String strLine;
try {
while ((strLine = br.readLine()) != null); { //parses the data in
database[counter]=Integer.parseInt(strLine);
counter++;//counter incremtented +1 each time, after ever input, allows user to input another number
}
} catch (IOException e) {
e.printStackTrace();
}
Scanner stdin = new Scanner(System.in);// declare scanner
while(stdin.hasNext())//allows continuous input for user to keep inputting numbers for program to search .txt file
{
boolean foundint=false;
int compareint=stdin.nextInt();
for(int i =0;i<Integer.parseInt(size);i++)//number input by user is searched for, loop depicts whether or not if the input is in txt file or not
{
if(compareint==database[i])// is in the list
{
System.out.println(compareint+ " This number is in the list!" + linecount);
foundint=true;
}
}
if(!foundint) //signifies if the input by user is in the .txt file or not
{
System.out.println(compareint+" This number is not in the list");
}
}
System.out.println("Goodbye, Have a nice day !");
}
}