读取文件并分配键、值和java

读取文件并分配键、值和java,java,algorithm,greedy,Java,Algorithm,Greedy,所以我尝试用java制作一个贪婪/珠宝盗窃算法。我把珠宝的数字和重量保存到一个.txt文件中。我的程序正在正确读取.txt文件,我已经编写了一个程序,可以成功地读取它们。这些是我的.txt文件中的数字 575 - bag limit 125 3000 (weight, value) 50 100 500 6000 25 30 我遇到的问题是,我正在努力为程序添加权重和值。理想情况下,程序将读取元组并为它们分配键和值。我尝试使用hashmap和常规Map,但它们一直不起作用。可能是因为他们在错误

所以我尝试用java制作一个贪婪/珠宝盗窃算法。我把珠宝的数字和重量保存到一个.txt文件中。我的程序正在正确读取.txt文件,我已经编写了一个程序,可以成功地读取它们。这些是我的.txt文件中的数字

575 - bag limit
125 3000 (weight, value)
50 100
500 6000
25 30
我遇到的问题是,我正在努力为程序添加权重和值。理想情况下,程序将读取元组并为它们分配键和值。我尝试使用hashmap和常规Map,但它们一直不起作用。可能是因为他们在错误的地方。我包括了两个尝试的地图,并在我的程序中注释掉了它们。我希望在分配这些值时能得到一些帮助,这样我就可以继续下一步了。谢谢

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



    }

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

  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());
//        }
   }
publicstaticvoidmain(字符串[]args){
HashMap=newHashMap();
试一试{
File File=新文件(“test.txt”);
FileReader FileReader=新的FileReader(文件);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
StringBuffer StringBuffer=新的StringBuffer();
弦线;
String weightLimit=Files.readAllLines(path.get(“test.txt”)).get(0);
整数计数=0;
而((line=bufferedReader.readLine())!=null){
如果(count!=0){//忽略第一行
String[]splitValue=line.split(“”);
map.put(splitValue[0],splitValue[1]);
}     
计数++;
}
fileReader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
//对于(Map.Entry:Map.entrySet()){
//System.out.println(entry.getKey());
//System.out.println(entry.getValue());
//        }
}

我看到了您的代码,并且我看到您使用
Files.readAllLines()
标准方法阅读了两次文件。因此,我建议您使用此解决方案,但您也可以使用
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);
    }
}
公共类主{
public static HashMap map=new HashMap();
私有静态字符串weightLimit=“”;
公共静态void main(字符串[]args){
试一试{
File File=新文件(“test.txt”);
FileReader FileReader=新的FileReader(文件);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
弦线;
布尔值优先=真;
而((line=bufferedReader.readLine())!=null){
如果(第一){
重量极限=直线;
第一个=假;
}否则{
字符串[]值=行。拆分(“”);
map.put(值[0],值[1]);
}
}
fileReader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
重量限制();
}
公共静态无效权重限制(){
系统输出打印项次(重量限制);
}
}

这很有效!我已成功打印了值[0]和值[1],但无法打印字符串权重限制,也看不到它设置为575的位置。我试着用我的原始字符串weightLimit=Files.readAllLines(path.get(“test.txt”)).get(0)自己设置weightLimit;然后当我打印它输出575四次时,我想是因为你没有初始化它。尝试
weightLimit=“”
当我尝试只打印行时,它输出所有元组。我只是尝试执行weightLimit=“”,但它仍然打印出575个四次哦,就是这样!我将其移动到fileReader.close()之前的“}”之后;这就解决了!非常感谢你的朋友!
  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());
//        }
   }
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);
    }
}