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

When 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; this is the code I have come up with.
import java.io.IOException;
import java.util.Scanner;
import java.io.File;
public class prog{
public void prog(){
System.out.print("Enter a serial number.");
Scanner in;
try{
if (I.equals("English07")){
in = new Scanner(new File("English07"));
File f = new File(“C:\Documents and Settings\Administrator\Desktop.txt”);
String I = new String ("English07");
I = in.nextLine();
System.out.println(I);
}
if(II.equals("chemistry07")){
in = new Scanner(new File("chemistry07"));
File f = new File(“C:\Documents and Settings\Administrator\Desktop.txt”);
String II = new String ("chemistry07");
II = in.nextLine();
System.out.println(II);
}
}catch(IOException i){
System.out.println("Error: " + i.getMessage());
}
}
}

-TY

2007-08-04 07:41:28 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Some recommendations:

(1) Don't take user input in a constructor method. Constructors should be quick initialization, in general.

(2) Don't write identical code over and over; refactor it into a separate method, where it can be written and maintained in only one place.

(3) Don't hard-wire the same constant into the code (C:\Documents and Settings ...) multiple times.

(4) Don't play around with Administrator's files, if you don't have to. Use a folder in C or something like that.

(5) It's almost never necessary to write "String s = new String("foo");". In almost every case "String s = "foo";" will work just fine. And in your code, the "String l = new String()" is immediately overwritten with l = in.nextLine(), so the initialization is completely unnecessary. Java doesn't require you to pre-allocate space for Strings.

-------------------------------------------------
How about this:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

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 first line
void scan(String aFilename) throws FileNotFoundException, IOException {
File inFile = new File(BASE_DIR + aFilename + ".txt");
if (! inFile.exists()) {
System.out.println("File " + inFile + " does not exist.");
return;
}
BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( inFile)));
// Print out the first line only.
// Is that what you wanted?
// It's what your code does, though.
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
public static void main(String[] args) {
prog p = new prog();
try {
while (p.queryAndPrint()) ;
} catch (Throwable th) {
th.printStackTrace();
}
}
}

2007-08-04 10:11:26 · answer #1 · answered by McFate 7 · 1 0

that's the 'annoying' thank you to serialize gadgets, the code occasion provided via your submit. examine out the link below, take word how concise the code is for IO making use of Scanner. The link exhibits the fashionable thank you to r/w a text cloth record. ok, then I frequently have an open-end documents shop like ArrayList. I study the text cloth record. If it has text cloth, I placed it into the ArrayList as a String. Loop the ArrayList text cloth now loaded in memory... Then, I take each and every line of text cloth String[] shapeFLDS = arrayList.get( num ).s.chop up( " " ); shapeFLDS is short-term, yet i understand container 0 is an merchandise, container a million is var call, container 2 is howmany, something fields are dimensions. It seems such as you opt for 7 classes for each geometric. each and every of those classes would have one or 2 constructors. shape is an Interface and it describes all the geometry.

2016-10-13 23:14:37 · answer #2 · answered by ? 3 · 0 0

Try this

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-05 10:43:26 · answer #3 · answered by E.M.Bed 5 · 0 0

fedest.com, questions and answers