Java—打印映射到从文件读取的键的值

Java—打印映射到从文件读取的键的值,java,arrays,file-io,mapping,Java,Arrays,File Io,Mapping,我想要一个java中的代码来读取文本文件,在第一列中选择一个值,然后在第二列中打印相应的值,如下图所示 我使用此处显示的代码读取了文件,但无法继续 public class readfile { private Scanner s; public static void main(String[] args) { readfile r = new readfile(); r.openFile(); r.readFile();

我想要一个java中的代码来读取文本文件,在第一列中选择一个值,然后在第二列中打印相应的值,如下图所示

我使用此处显示的代码读取了文件,但无法继续

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

以您的代码为基础(我尝试进行最小的更改),我建议您将读取的值存储在映射中-这将允许您快速获得相应的值

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();
    }   
}
公共类读取文件{
私人扫描仪;
公共静态void main(字符串[]args){
readfile r=新的readfile();
r、 openFile();
//保存文件中的值的映射中的标记
Map tokens=r.readFile();
r、 closeFile();
//本节仅演示如何使用地图
扫描仪=新的扫描仪(System.in);
字符串标记=scanner.next();
//这是一个简单的用户输入循环
//列以获取第二列-直到输入“退出”。
而(!token.equals(“exit”)){
//如果映射中存在第一列中的值
if(令牌.容器(令牌)){
//然后从第二列打印相应的值
System.out.println(tokens.get(token));
} 
token=scanner.next();
}
scanner.close();
}   
公共void openFile(){
试一试{
s=新扫描仪(新文件(“filename.txt”);
}捕获(例外e){
System.out.println(“未找到文件”);
}
}   
//请注意此更改-该方法现在返回一个映射
公共地图读取文件(){
Map tokens=newhashmap();
而(s.hasNext()){
字符串a=s.next();
字符串b=s.next();
tokens.put(a,b);//我们将这两个值存储在一个映射中以供以后使用
}
return tokens;//返回要使用的映射。
}    
公共文件(){
s、 close();
}   
}

我希望这说明了如何从文件中读取任何键值对并将其存储以供以后使用。

以您的代码为基础(我尝试进行最小的更改),我建议您将读取的值存储在映射中-这将允许您快速获得相应的值

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();
    }   
}
公共类读取文件{
私人扫描仪;
公共静态void main(字符串[]args){
readfile r=新的readfile();
r、 openFile();
//保存文件中的值的映射中的标记
Map tokens=r.readFile();
r、 closeFile();
//本节仅演示如何使用地图
扫描仪=新的扫描仪(System.in);
字符串标记=scanner.next();
//这是一个简单的用户输入循环
//列以获取第二列-直到输入“退出”。
而(!token.equals(“exit”)){
//如果映射中存在第一列中的值
if(令牌.容器(令牌)){
//然后从第二列打印相应的值
System.out.println(tokens.get(token));
} 
token=scanner.next();
}
scanner.close();
}   
公共void openFile(){
试一试{
s=新扫描仪(新文件(“filename.txt”);
}捕获(例外e){
System.out.println(“未找到文件”);
}
}   
//请注意此更改-该方法现在返回一个映射
公共地图读取文件(){
Map tokens=newhashmap();
而(s.hasNext()){
字符串a=s.next();
字符串b=s.next();
tokens.put(a,b);//我们将这两个值存储在一个映射中以供以后使用
}
return tokens;//返回要使用的映射。
}    
公共文件(){
s、 close();
}   
}

我希望这说明了如何从文件中读取任何键值对,并将其存储以供以后使用。

如果没有特定的语言,您可能只能在数组上循环,并在第一列中检查valeu,如果相等,则写入第二列感谢@MarekMaszay的回复。我已经编辑了我的帖子,让它更清晰。如果你在再次阅读这篇文章后有任何建议,请与我分享。我对Java比较陌生。这两列的映射在哪里?它们都在同一个文件中吗?它们是否共享一行,如果共享,用什么分隔符分隔列?如果他们不共用一行,他们总是一行接一行吗?@Assafs,谢谢。我想回答您的问题:根据上面的代码,字符串“a”和字符串“b”分别映射到第一列和第二列。它们都在同一个文件中,共享一行,并用“空格”分隔。这意味着它们总是一个接一个的。我对代码做了一点扩展-我希望它能帮助您理解如何从文件中读取键值,并存储它们以供以后使用。如果您认为这是一个很好的解决方案,如果您可以通过单击绿色勾号接受答案,我将不胜感激。如果没有特定的语言,您可能只能在数组上循环并在第一列中检查valeu,如果相等,请写第二列谢谢@MarekMaszay的回复。我已经编辑了我的帖子,让它更清晰。如果你在再次阅读这篇文章后有任何建议,请与我分享。我对Java比较陌生。这两列的映射在哪里?它们都在同一个文件中吗?它们是否共享一行,如果共享,用什么分隔符分隔列?如果他们不共用一行,他们总是一行接一行吗?@Assafs,谢谢。我想回答您的问题:根据上面的代码,字符串“a”和字符串“b”分别映射到第一列和第二列。它们都在同一个文件中,共享一行,并用“空格”分隔。这意味着它们总是一个