English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm trying to come up with a program that when the user enters serial number/subject id, the program reads from a bunch of files and finds the corresponding .txt file and displays the name of assignments listed in the .txt file. I'm having a lot of trouble creating the program please help me myself, and I haven't been able to get any help. (My program doesn't even compile, if anyone wishes to try this program out and gets it to work, I really would appreciate the help). Thank you, in advance.

*the answerer who offers me the best help is guaranteed 10 points.

2007-08-04 10:23:38 · 3 answers · asked by jumba 1 in Computers & Internet Programming & Design

3 answers

import java.io.*;

public class prog {

// This is the directory to search
static final String BASE_DIR = "C:/prog/";

// This is the method to scan a file and
// print its contents
void scan(String aFilename) throws FileNotFoundException, IOException {
File inFile = new File(BASE_DIR + aFilename + ".txt");
if (! inFile.exists()) {
// Complain if the name doesn't match
// any file.
System.out.println("File " + inFile + " does not exist.");
return;
}
// Otherwise, print it all
BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( inFile)));
String s = null;
while ((s = in.readLine()) != null) System.out.println(s);
in.close();
}

// This takes a line of user input
// Return false if user hit end-of-file
boolean queryAndPrint() throws FileNotFoundException, IOException {
System.out.print("Enter serial number: ");
String s = (new BufferedReader(new InputStreamReader( System.in ) ) ).readLine();
if (s == null) return false;
scan(s);
return true;
}

// This is the main method which executes forever
// Or until the user enters control-Z on Windows,
// which is end-of-file.
public static void main(String[] args) {
prog p = new prog();
try {
while (p.queryAndPrint()) ;
} catch (Throwable th) {
th.printStackTrace();
}
}
}

===================
Test run: I created "c:\prog\foo.txt" and put two lines in it. One said "hello" and the other "goodbye". Executing the program:

Enter serial number: foo
Hello.
Goodbye.
Enter serial number: bar
File C:\prog\bar.txt does not exist.
Enter serial number:

2007-08-04 10:38:05 · answer #1 · answered by McFate 7 · 1 0

This does exactly as your description wants:

import java.io.*;

public class myprog
{
public static void main(String args[])
{
BufferedReader br,in;
String line;

System.out.println("Enter Serial Number");
try{
br = new BufferedReader(new InputStreamReader(System.in));
in = new BufferedReader(new FileReader(br.readLine() + ".txt"));
while((line = in.readLine()) != null)
{
System.out.println(line);
}
br.close();
in.close();
}catch (IOException e){
e.printStackTrace();
}
}
}

Run on test files in same and other directories.
Use another directory by adding path to beginning of serial number - user can control which directory to search in this way:

Enter Serial Number
c:\serial\new\1409080

and the contents of c:\serial\new\1409080.txt will be output.

2007-08-04 18:35:54 · answer #2 · answered by E.M.Bed 5 · 1 0

What language ?

2007-08-04 21:35:26 · answer #3 · answered by larystoy 3 · 0 0

fedest.com, questions and answers