我正在使用Java修改文件。这就是我想要完成的事情:
这是我的意思的一个例子。如果输入的文件包含这些字符串:
&myword
&4
&anotherword
&9
&yetanotherword
&10
&myword
输出应为:
&0000000000010000 (which is 16 in decimal)
&0000000000000100 (or the number '4' in decimal)
&0000000000010001 (which is 17 in decimal, since 16 is already used, so 16+1=17)
&0000000000000101 (or the number '9' in decimal)
&0000000000010001 (which is 18 in decimal, or 17+1=18)
&0000000000000110 (or the number '10' in decimal)
&0000000000010000 (which is 16 because value of myword = 16)
这是我到目前为止所尝试的内容,但尚未成功:
for (i=0; i<anyLines.length; i++) {
char[] charray = anyLines[i].toCharArray();
for (int j=0; j<charray.length; j++)
if (Character.isDigit(charray[j])) {
anyLines[i] = anyLines[i].replace("&","");
anyLines[i] = Integer.toBinaryString(Integer.parseInt(anyLines[i]);
}
else {
continue;
}
if (Character.isLetter(charray[j])) {
anyLines[i] = anyLines[i].replace("&","");
for (int k=16; j<charray.length; k++) {
anyLines[i] = Integer.toBinaryString(Integer.parseInt(k);
}
}
}
}
我希望我表达得足够清楚。关于如何完成这项任务的任何建议?
答案 0 :(得分:2)
Character.isLetter() //tests to see if it is a letter
Character.isDigit() //tests the character to
答案 1 :(得分:1)
看起来你可以匹配正则表达式。我不懂Java,但你应该至少有一个正则表达式引擎。然后正则表达式将是:
regex1:&amp;(\ d +) 和 regex2:&amp;(\ w +)
或
regex3:&amp;(\ d + | \ w +)
在第一种情况下,如果regex1匹配,你知道你遇到了一个数字,那个数字进入了第一个捕获组(例如:match.group(1))。如果regex2匹配,你知道你有一个单词。然后,您可以将该单词查找到字典中,查看其关联的数字是什么,或者如果不存在,则将其添加到字典中并将其与下一个空闲数字相关联(16 +字典大小+ 1)。
另一方面,regex3将匹配数字和单词,因此您可以自行查看捕获组中的内容(这只是一种不同的方法)。如果正则表达式都不匹配,那么您的序列无效,或者您需要其他一些操作。请注意,正则表达式中的\ w仅匹配单词字符(即:字母,_和可能的其他几个字符),因此&amp;çSomeWord或&amp; * SomeWord根本不匹配,而捕获的组在&amp; Hello中。世界将只是“你好”。
正则表达式libs通常为匹配的文本提供长度,因此您可以向前移动那么多,以便跳过已经匹配的文本。
答案 2 :(得分:0)
我会将此作为评论发布,但尚未具备此功能。你遇到的问题是什么?错误?结果不正确? 16没有正确递增?此外,示例使用'%',但在您的说明中,您说它应该以'&amp;'开头。
编辑2:认为这是一行一行,但重新阅读表明你可能试图找到说“我去了&amp; store”并希望它说“我去了&amp; 000010000”。所以你想要用空格分割,然后迭代并将字符串传递给你的'replace'方法,类似于下面的方法。Edit1:如果我理解你要做什么,那么这样的代码应该有用。
Map<String, Integer> usedWords = new HashMap<String, Integer>();
List<String> output = new ArrayList<String>();
int wordIncrementer = 16;
String[] arr = test.split("\n");
for(String s : arr)
{
if(s.startsWith("&"))
{
String line = s.substring(1).trim(); //Removes &
try
{
Integer lineInt = Integer.parseInt(line);
output.add("&" + Integer.toBinaryString(lineInt));
}
catch(Exception e)
{
System.out.println("Line was not an integer. Parsing as a String.");
String outputString = "&";
if(usedWords.containsKey(line))
{
outputString += Integer.toBinaryString(usedWords.get(line));
}
else
{
outputString += Integer.toBinaryString(wordIncrementer);
usedWords.put(line, wordIncrementer++);
}
output.add(outputString);
}
}
else
{
continue; //Nothing indicating that we should parse the line.
}
}
答案 3 :(得分:0)
&
(indexOf('%')
),然后以某种方式确定下一个标记是什么(数字或“单词”,但是您想要定义单词)。Map<String, Integer>
。答案 4 :(得分:0)
这个怎么样?
String input = "&myword\n&4\n&anotherword\n&9\n&yetanotherword\n&10\n&myword";
String[] lines = input.split("\n");
int wordValue = 16;
// to keep track words that are already used
Map<String, Integer> wordValueMap = new HashMap<String, Integer>();
for (String line : lines) {
// if line doesn't begin with &, then ignore it
if (!line.startsWith("&")) {
continue;
}
// remove &
line = line.substring(1);
Integer binaryValue = null;
if (line.matches("\\d+")) {
binaryValue = Integer.parseInt(line);
}
else if (line.matches("\\w+")) {
binaryValue = wordValueMap.get(line);
// if the map doesn't contain the word value, then assign and store it
if (binaryValue == null) {
binaryValue = wordValue;
wordValueMap.put(line, binaryValue);
wordValue++;
}
}
// I'm using Commons Lang's StringUtils.leftPad(..) to create the zero padded string
String out = "&" + StringUtils.leftPad(Integer.toBinaryString(binaryValue), 16, "0");
System.out.println(out);
这是打印输出: -
&0000000000010000
&0000000000000100
&0000000000010001
&0000000000001001
&0000000000010010
&0000000000001010
&0000000000010000
仅供参考,10的二进制值为“1010”,而不是原始帖子中所述的“110”。