adam
adam

Reputation: 9

Java reading txt file from user input and outputting data according to user input

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 ();

}

Upvotes: 0

Views: 6867

Answers (1)

Bahramdun Adil
Bahramdun Adil

Reputation: 6079

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;
        }
    }

}

Upvotes: 1

Related Questions