读取文件并分配键值 java

read a file and assign key, values java

所以我试图在 java 中制作一个 greedy/jewel 抢劫算法。我将珠宝的数量和重量保存到 .txt 文件中。我的程序正在正确读取 .txt 文件,并且我编写了一个可以成功读取它们的程序。这些是我的 .txt 文件中的数字

575 - bag limit
125 3000 (weight, value)
50 100
500 6000
25 30

我 运行 遇到的问题是我正在努力为程序添加权重和值。理想情况下,程序会读取元组并为它们分配键和值。我尝试使用散列图和常规地图,但它们没有用。可能是因为他们在错误的地方。我包括了两个尝试过的地图,并像在我的程序中一样将它们注释掉。希望在分配这些值方面得到一些帮助,以便我可以继续下一步。谢谢!!

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
//    import java.util.HashMap;
//    import java.util.Map;
    public class readstringastext {

        public static void main(String[] args) {
            try {
                File file = new File("test.txt");
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new 
                BufferedReader(fileReader);
                StringBuffer stringBuffer = new StringBuffer();
                String line;
                String weightLimit = Files.readAllLines(Paths.get("test.txt")).get(0); 
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
                fileReader.close();
    }       catch (IOException e) {
                e.printStackTrace();
            }
    }
            HashMap<String, String> map = new HashMap<String, String>();
//
//          for (String string : pairs) {
//              String[] keyValue = string.split(" "); 
//              map.put(keyValue[0], keyValue[1]);
//              System.out.println(keyValue);
//      };
//final class MyEntry<K, V> implements Map.Entry<K, V> {
//  private final K key;
//  private V value;
//  
//  public MyEntry(K key, V value) {
//      this.key = key;
//      this.value = value;
//  }   
//  @Override 
//  public K getKey() {
//      return key;
//  }
//  
//  @Override 
//  public V getValue() {
//      return value;
//  }
//  
//  @Override
//  public V setValue(V value) {
//      V old = this.value;
//      this.value = value;
//      return old;
//  }
//  Map.Entry<String, Object> entry = new MyEntry <String, Object>(key, value);
//  System.out.println(entry.getKey());
//  System.out.println(entry.getValue());
}



    }

尝试 2

public class readstringastext {

    public static HashMap<String, String> map = new HashMap<String, String>();
    public static void weightLimit() {
        String weightLimit = "";
        System.out.println(weightLimit);

// 这是为了查看 weightLimit 是否存在,但它不存在'。 }

public static void main(String[] args){

        try {
            File file = new File("test.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            String weightLimit = "";
            boolean first = true;
            while ((line = bufferedReader.readLine()) != null) {
                if (first) {
                    weightLimit = line;
                    first = false;
                } else {
                    String[] values = line.split(" ");
                    map.put(values[0], values[1]);
                }
            }

            fileReader.close();
        }       catch (IOException e) {
            e.printStackTrace();
        }
    }

}

我相信您想将 weight/value 对传递到地图数据结构中,我在下面稍微修改了您的代码以启用此功能:

  public static void main(String[] args) {
    HashMap<String, String> map = new HashMap<String, String>();

    try {
        File file = new File("test.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        String weightLimit = Files.readAllLines(Paths.get("test.txt")).get(0);
        int count = 0;
        while ((line = bufferedReader.readLine()) != null) {
            if (count != 0) { // ignore the first line
                String[] splitValue = line.split(" ");
                map.put(splitValue[0], splitValue[1]);
            }     
            count++;
        }
        fileReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
   }

我看到了你的代码,我看到你用 Files.readAllLines()standard method 读了两次文件。所以我建议您使用此解决方案,但您也可以使用 Files.readAllLines()

public class Main {

    public static HashMap<String, String> map = new HashMap<String, String>();
    private static String weightLimit = "";

    public static void main(String[] args){

        try {
            File file = new File("test.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            boolean first = true;
            while ((line = bufferedReader.readLine()) != null) {
                if (first) {
                    weightLimit = line;
                    first = false;
                } else {
                    String[] values = line.split(" ");
                    map.put(values[0], values[1]);
                }

            }
            fileReader.close();
        }       catch (IOException e) {
            e.printStackTrace();
        }
        weightLimit();
    }

    public static void weightLimit() {
        System.out.println(weightLimit);
    }
}