I have an input of
-157,118
-170,12
-74,139
-144,42
-155,196
122,-88
-187,-143
156,-18
-67,126
44,-102
....
像这样。我必须检查一个文件中的每个数字(我有更多这样的输入)并且必须找到哪一对(+ ve,+ ve)。请任何人帮助我。我能读文件中的数字。我已经这样做了..
public class EulerGift {
public static void main(String[] args) {
// List<String> coordinateList=new ArrayList<String>();
File file = new File("D:/coordinate.txt");
try {
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String value = sc.next();
String[] tokens = value.split(",");
for (int i = 0; i < tokens.length; i++) {
System.out.println("("+Integer.parseInt(tokens[i])+")");
// System.out.println(Integer.parseInt(tokens[i]));
}
}
} catch (FileNotFoundException e) {
System.err.format("File Not Found");
}
}
}
答案 0 :(得分:1)
如果你只想得到积极的一对:
String value = sc.next();
String[] tokens = value.split(",");
int[] values = new int[2];
values[0] = Integer.parseInt(tokens[0])
values[1] = Integer.parseInt(tokens[1])
if(values[0]>0 && values[1]>0)
System.out.println("(" + values[0] + "," + values[1] + ")");