Java 检查字符是整数还是字母
我正在使用Java修改一个文件。以下是我想要实现的目标:Java 检查字符是整数还是字母,java,file,binary,character,integer,Java,File,Binary,Character,Integer,我正在使用Java修改一个文件。以下是我想要实现的目标: 如果在读取时检测到一个&symbol和一个整数,我想删除&symbol并将整数转换为二进制 如果在读取时检测到&symbol和(随机)字,我希望删除&symbol并用整数16替换该字,如果与&symbol一起使用的是不同的字符串,我希望将数字1设置为高于整数16 这里有一个例子来说明我的意思。如果输入的文件包含以下字符串: &myword &4 &anotherword &9 &yetanoth
- 如果在读取时检测到一个&symbol和一个整数,我想删除&symbol并将整数转换为二进制
- 如果在读取时检测到&symbol和(随机)字,我希望删除&symbol并用整数16替换该字,如果与&symbol一起使用的是不同的字符串,我希望将数字1设置为高于整数16
&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);
}
}
}
}
for(i=0;i我会将此作为注释发布,但目前还没有此功能。您遇到了什么问题?错误?结果不正确?16没有正确递增?此外,示例使用“%”,但在您的描述中,您说它应该以“&”开头
Edit2:我认为这是一行一行的,但重读表明您可能试图找到,比如说“我去了&store”,并希望它说“我去了&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.
}
}
Map usedWords=new HashMap();
列表输出=新的ArrayList();
int-wordIncrementer=16;
字符串[]arr=test.split(“\n”);
用于(字符串s:arr)
{
如果(s.startsWith(“&”))
{
String line=s.substring(1).trim();//删除&
尝试
{
整数lineInt=Integer.parseInt(行);
output.add(&“+Integer.tobinarysting(lineInt));
}
捕获(例外e)
{
println(“行不是整数,解析为字符串。”);
字符串outputString=“&”;
if(usedWords.containsKey(行))
{
outputString+=Integer.tobinarysting(usedWords.get(line));
}
其他的
{
outputString+=Integer.toBinaryString(wordIncrementer);
put(line,wordIncrementer++);
}
add(outputString);
}
}
其他的
{
continue;//没有任何东西指示我们应该分析该行。
}
}
我想将此作为评论发布,但目前还没有这个能力。您遇到了什么问题?错误?结果不正确?16没有正确递增?此外,示例使用“%”,但在您的描述中,您说它应该以“&”开头
Edit2:我认为这是一行一行的,但重读表明您可能试图找到,比如说“我去了&store”,并希望它说“我去了&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.
}
}
Map usedWords=new HashMap();
列表输出=新的ArrayList();
int-wordIncrementer=16;
字符串[]arr=test.split(“\n”);
用于(字符串s:arr)
{
如果(s.startsWith(“&”))
{
String line=s.substring(1).trim();//删除&
尝试
{
整数lineInt=Integer.parseInt(行);
add(“&”+Integer.toBinaryString(lineInt));
}
捕获(例外e)
{
println(“行不是整数,解析为字符串。”);
字符串outputString=“&”;
if(usedWords.containsKey(行))
{
outputString+=Integer.tobinarysting(usedWords.get(line));
}
其他的
{
outputString+=Integer.toBinaryString(wordIncrementer);
put(line,wordIncrementer++);
}
add(outputString);
}
}
其他的
{
continue;//没有任何东西指示我们应该分析该行。
}
}
- 您必须以某种方式标记您的输入。似乎您正在将其拆分为几行,然后分别分析每一行。如果这是您想要的,好吧。如果不是,您可以简单地搜索
&
(indexOf('%')
),然后以某种方式确定下一个标记是什么(数字或“单词”,但您要定义单词)
- 对于与您的模式不匹配的输入,您想做什么?任务的描述和示例都没有涉及到这一点
- 您需要有一个已读字符串的字典。请使用
映射
- 您必须以某种方式标记您的输入。似乎您正在将其拆分为几行,然后分别分析每一行。如果这是您想要的,好吧。如果不是,您可以简单地搜索
&
(indexOf('%')
),然后以某种方式确定下一个标记是什么(数字或“单词”,但您要定义单词)
- 对于与您的模式不匹配的输入,您想做什么?任务的描述和示例都没有涉及到这一点
- 您需要有一个已读字符串的字典。请使用
映射
它看起来像是可以与正则表达式相匹配的东西。我不懂Java,但您应该至少有一个正则表达式引擎供您使用。那么正则表达式将是:
regex1:&(\d+)
和
regex2:&(\w+)
或
regex3:&(\d+\w+)
在第一种情况下,如果regex1匹配,您就知道您遇到了一个数字,并且该数字在第一次捕获中
&0000000000010000
&0000000000000100
&0000000000010001
&0000000000001001
&0000000000010010
&0000000000001010
&0000000000010000
Character.isLetter() //tests to see if it is a letter
Character.isDigit() //tests the character to