0

I have a text file that looks like this

BEG#Belgrave#19 February 1962
FSS#Flinders Street#12 September 1854
TSN#Tecoma#1 February 1924

im trying to write a program, asking the user to input the filename (i can do this part), then the user is prompted to enter in a "Code". The program is then to read the txt file, and output information according the the unique code. for example:

java Codes

Enter file name >> stationsMaster.txt

Enter station code >> FSS

Station name: "Flinders" has code "FSS" date built: 12 September 1854

here is the code of what i have done so far, im just really stuck on how to write the code so that the program reads through the text file and outputs the according information from the user input.

import java.util.*;
import java.io.*;
public class Codes
{
public static void main (String [] args) throws IOException
{
 Scanner keyboard = new Scanner (System.in);
 System.out.print("Enter File Name");
 String filename = keyboard.nextLine();
 File f = new File (filename);
 Scanner fin = new Scanner (f);
 String stationcode = fin.nextLine();
 String stationname = fin.nextLine();
 String date = fin.nextLine ();


while (fin.hasNextLine ( ) )
 {

System.out.print (date);
System.out.print(stationname);

 }

fin.close ();

}
1
  • 2
    Always try to break down your big problem into a lot of small problems. In this case you need to know how to read a file line by line, then split the string at each "#" and compare the station codes. If the codes match, output all details about the station. Commented Aug 11, 2016 at 9:42

1 Answer 1

1

You can try something like this: hope this can solve your problem

public class Test {
    private Map<String, Station> stationMap = new HashMap<>();

    public static void main(String[] args) throws Exception {
        // first read the file and store the data to the map
        Test test = new Test();
        test.readFile();

        // now ask the user for the station code
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Please enter the code: ");
            String code = scanner.nextLine();
            Station station = test.stationMap.get(code.toUpperCase());
            if (station == null) {
                System.out.println("There is no such station present fot this code!");
                continue;
            }
            System.out.println("Station name: "+station.getName());
            System.out.println("Station code: "+station.getCode());
            System.out.println("Station built date: "+station.getBuiltDate());
        }
    }

    private void readFile() {
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("path/to/file")))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] strs = line.split("#");
                Station station = new Station(strs[0], strs[1], strs[2]);
                stationMap.put(station.getCode().toUpperCase(), station);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class Station {
        private String name;
        private String code;
        private String builtDate;

        public Station(String name, String code, String builtDate) {
            this.name = name;
            this.code = code;
            this.builtDate = builtDate;
        }

        public String getName() {
            return name;
        }

        public String getCode() {
            return code;
        }

        public String getBuiltDate() {
            return builtDate;
        }
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

ah thanks yeah it does, but im told i have to write it without using bufferReader, im restriced to string class methods, i thought it may have somthing to do with using substrings, indexOf
@adam At any case you all need to read the file, then why you do not read the only once and then access data from RAM, not from file HD. it is more fast than the way you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.