所以我试图在java中制作一个贪婪/宝石抢劫算法。我将珠宝的数字和重量保存到.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是否存在,它不是&#39;。 }
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();
}
}
}
答案 0 :(得分:0)
我相信您希望将权重/值对传递到地图数据结构中,我在下面稍微修改了您的代码以启用此功能:
{
"name": "temp-google-drive-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"googleapis": "^27.0.0"
}
}
答案 1 :(得分:0)
我看到了您的代码,并且我看到您使用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);
}
}