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 !");
    }
}

Recommended Answers

All 2 Replies

Having some trouble

Please explain.

What happens when you compile and execute the program?
If there are any errors, please copy and paste the full text here.

If the program executes, please copy the full contents of the command prompt window and paste it here.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Having some trouble

And what is your trouble with it???

By the way, you should keep everything inside one try and have multiple catch instead. Something similar to below (adjusted from your original code).

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
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in));//reading data from ("data3.txt")
      String size=null;
      int linecount = 0;
      size = br.readLine();//indicates the size of the file

      database= new int[Integer.parseInt(size)];//sets the data for program
      int counter=0;
      String strLine;
      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
      }

      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 !");
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    catch (IOException e1) {
      e1.printStackTrace();
    }
    catch (Exception ex) {  // catch all other exceptions that aren't expected
      ex.printStackTrace();
    }
  }
}

PS: This thread looks like a duplicated with this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.