简体   繁体   中英

Java - printing value mapped to a key, which is read from file

I would like a code in java to read a text file, select a value in first column then prints its corresponding value in the second column as shown in the image below.

I managed to read the file using the code shown here but I'm unable to continue.

public class readfile {
    private Scanner s;

    public static void main(String[] args) {
        readfile r = new readfile();
        r.openFile();
        r.readFile();
        r.closeFile(); 
    }   

    public void openFile() {
        try {
            s = new Scanner (new File("filename.txt")); 
        } catch(Exception e) {
            System.out.println("file not found "); 
        }
    }   

    public void readFile() {
        while(s.hasNext()) {
            String a = s.next();
            String b = s.next();
            System.out.printf("%s  %s\n",a, b);    
        }
    }   

    public void closeFile() {
        s.close();
    }   
}

我想编码选择第一列中的值并在第二列中显示其对应值的代码,如该图所示

Taking your code as the basis for this (I tried to make the most minimal changes) I suggest you store the read values in a Map - this will allow you to get the corresponding value quickly.

public class readfile {
    private Scanner s;

    public static void main(String[] args) {
        readfile r = new readfile();
        r.openFile();

        //tokens in a map that holds the values from the file
        Map<String,String> tokens = r.readFile();
        r.closeFile();

        //this section just demonstrates how you might use the map
        Scanner scanner = new Scanner(System.in);
        String token = scanner.next();

        //This is a simple user input loop. Enter values of the first 
        //column to get the second column - until you enter 'exit'.
        while (!token.equals("exit")) {
            //If the value from the first column exists in the map
            if (tokens.containsKey(token)) {
                //then print the corresponding value from the second column
                System.out.println(tokens.get(token));
            } 
            token = scanner.next();
        }
        scanner.close(); 
    }   

    public void openFile() {
        try {
            s = new Scanner (new File("filename.txt")); 
        } catch(Exception e) {
            System.out.println("file not found "); 
        }
    }   

    //Note this change - the method now returns a map
    public Map<String,String> readFile() {
    Map<String, String> tokens = new HashMap<String,String>();
    while(s.hasNext()) {
        String a = s.next();
        String b = s.next();
        tokens.put(a,b); //we store the two values in a map to use later
    }
    return tokens; //return the map, to be used.
    }    

    public void closeFile() {
        s.close();
    }   
}

I hope this illustrates how you can read any key-value pair from a file and store it for use at a later phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM