读取字符并检查它们是否是数字 [英] Reading characters and checking if they're a number

查看:177
本文介绍了读取字符并检查它们是否是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个方法来检查字符后的符号&是数字或字母;如果一个数字,它被翻译成二进制;并且如果它是字母,则将其设置为16,并且如果使用不同的字,则递增1。麻烦的是,这不适用于我,由于某种原因。有什么建议么?

  try {
ReadFile files = new ReadFile(file.getPath());
String [] anyLines = files.OpenFile();

int i;

for(i = 0; i String input = anyLines [i];
String [] lines = input.split(\\\
);

int wordValue = 16;

映射< String,Integer> wordValueMap = new HashMap< String,Integer>();

for(String line:lines){
//如果行不以&开头,则忽略
if(!line.startsWith(& )){
continue;
}

// remove&
line = line.substring(1);

Integer binaryValue = null;

if(line.matches(\\d +)){
binaryValue = Integer.toBinaryString(131072 +
Integer.parseInt(anyLines [i])) .substring(2,18);
}
else if(line.matches(\\w +)){
binaryValue = wordValueMap.get(line);

if(binaryValue == null){
binaryValue = wordValue;
wordValueMap.put(line,binaryValue);
wordValue ++;
}
}
}
}

INPUT :

 & 4 
...
& hello
...
& ok

OUTPUT:

 (5翻译成二进制):0000000000000100 
...
(16翻译成二进制)
...
变成二进制或16 + 1)






您方法的输出:

  101 
1001100
1001100
1001100
1001100
1001100
1001100
1001100
& 5
1110110000010000
& hello
1110001100001000
& goodbye
1110101010001000
(NEXT)
& goodbye
1111000010001000
& hello
1110001110001000
& BILL
1110001100000110
& NEXT
1110101010000111
(BILL)
& BILL
1110101010000111


$ b b

这里是我正在阅读和循环的原始文本(anyLines [i]没有任何修改):

 & 5 
var1
& hello
var2
& goodbye
var2
(NEXT)
& goodbye
var3
& hello
var4
& BILL
var5
& NEXT
var6
(BILL)
& ; BILL
var5

var只是一个有值的变量。



b b pre> String input =这是一个测试行\\\

+& hello\\\

+& 4 \\\

+& 32;

String [] lines = input.split(\\\
);
int wordValue = 26;

映射< String,Integer> wordValueMap = new HashMap< String,Integer>();

for(String currentLine:lines)
{
if(!currentLine.startsWith(&))
{
continue;
}

currentLine = currentLine.substring(1);
整数值;

if(currentLine.matches(\\d +))
{
value = Integer.parseInt(currentLine);
}
else if(currentLine.matches(\\w +))
{
value = wordValueMap.get(currentLine);

if(value == null)
{
int binaryValue = wordValue ++;
wordValueMap.replace(currentLine,binaryValue);
/ *
*这只是为了确保下面的print语句没有
* null值。
* /
value = binaryValue;
}
}
else
{
System.out.println(Invalid input);
break;
}

System.out.println(Integer.toBinaryString(value));
}


解决方案

码。
首先,你必须解析在toBinaryString()调用后得到的值。

  binaryValue = Integer.parseInt(Integer.toBinaryString(131072 + 
Integer.parseInt(anyLines [i]))。substring(2,18));

anyLines [i]作为该代码中parseInt()函数的参数的用法也是不正确。从我看到你的代码,anyLines [i]是一个字符串with newLines和其他东西,它显然不能解析成一个整数。



下面的代码似乎工作。我改变了HashMap的存储,以便它存储正则整数,而不是试图存储二进制表示作为一个整数值。

  String input =这是一个测试行\\\

+& hello\\\

+& 4 \\\

+& 32;

String [] lines = input.split(\\\
);
int wordValue = 26;

映射< String,Integer> wordValueMap = new HashMap< String,Integer>();

for(String currentLine:lines)
{
if(!currentLine.startsWith(&))
{
continue;
}

currentLine = currentLine.substring(1);
整数值;

if(currentLine.matches(\\d +))
{
value = Integer.parseInt(currentLine);
}
else if(currentLine.matches(\\w +))
{
value = wordValueMap.get(currentLine);

if(value == null)
{
int binaryValue = wordValue ++;
wordValueMap.put(currentLine,binaryValue);
/ *
*这只是为了确保下面的print语句没有
* null值。
* /
value = binaryValue;
}
}
else
{
System.out.println(Invalid input);
break
}

System.out.println(Integer.toBinaryString(value));
}

另外,我希望你知道这会失败的数字后跟一个词,如句子。但如果你的输入永远不会有任何,这是一个非问题。


I am trying to get a method that checks if character after the symbol & is a number or a letter; if a number, it is translated into binary; and if it is a letter, it is set to 16 and is incremented by 1 if a different word is being used. The trouble is, this doesn't work for me, for some reason. Any suggestions?

try {
       ReadFile files = new ReadFile(file.getPath());
       String[] anyLines = files.OpenFile();

       int i;

       for (i=0; i<anyLines.length; i++) {
           String input = anyLines[i];
           String[] lines = input.split("\n");

           int wordValue = 16;

           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.toBinaryString(131072 +
                              Integer.parseInt(anyLines[i])).substring(2,18);
               }
               else if (line.matches("\\w+")) {
                   binaryValue = wordValueMap.get(line);

                   if (binaryValue == null) {
                       binaryValue = wordValue;
                       wordValueMap.put(line, binaryValue);
                       wordValue++;
                   }
               }
           }
       }

INPUT:

&4
...
&hello
...
&ok

OUTPUT:

(5 translated into binary) : 0000000000000100
...
(16 translated into binary)
...
(17 translated into binary, or 16+1)


Here's the output of your method:

101
1001100
1001100
1001100
1001100
1001100
1001100
1001100
&5
1110110000010000 
&hello
1110001100001000 
&goodbye
1110101010001000 
(NEXT)
&goodbye
1111000010001000 
&hello
1110001110001000 
&BILL
1110001100000110 
&NEXT
1110101010000111 
(BILL)
&BILL
1110101010000111 

And here's the original text that i'm reading and looping through (anyLines[i] without any modifications):

&5
var1 
&hello
var2 
&goodbye
var2 
(NEXT)
&goodbye
var3 
&hello
var4 
&BILL
var5 
&NEXT
var6 
(BILL)
&BILL
var5 

var is just a variable that has a value. I already took care of those.


Here's my attempt:

String input = "This is a test line\n"
           + "&hello\n"
           + "&4\n"
           + "&32";

String[] lines = input.split("\n");
int wordValue = 26;

Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

for (String currentLine : lines)
{
if (!currentLine.startsWith("&"))
{
    continue;
}

currentLine = currentLine.substring(1);
Integer value;

if (currentLine.matches("\\d+"))
{
    value = Integer.parseInt(currentLine);
}
else if (currentLine.matches("\\w+"))
{
    value = wordValueMap.get(currentLine);

    if(value == null)
    {
        int binaryValue = wordValue++;
        wordValueMap.replace(currentLine, binaryValue);
        /*
         * This is just there to ensure that the print statement below doesn't have a 
         * null value.
         */
        value = binaryValue;
    }
}
else
{
    System.out.println("Invalid input");
    break;
}

System.out.println(Integer.toBinaryString(value));
}

解决方案

There are numerous problems with your code. To begin with, you'll have to parse the value you get after the toBinaryString() call.

binaryValue = Integer.parseInt(Integer.toBinaryString(131072 +
                          Integer.parseInt(anyLines[i])).substring(2,18));

The usage of anyLines[i] as an argument to the parseInt() function in that code is also incorrect. From what I see of your code, anyLines[i] is a string with newLines and other things in it, which obviously cannot be parsed into an integer.

The code below seems to work. I've changed the storage of the HashMap so that it stores regular integers, instead of attempting to store a binary representation as an integer value.

String input = "This is a test line\n"
               + "&hello\n"
               + "&4\n"
               + "&32";

String[] lines = input.split("\n");
int wordValue = 26;

Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

for (String currentLine : lines)
{
    if (!currentLine.startsWith("&"))
    {
        continue;
    }

    currentLine = currentLine.substring(1);
    Integer value;

    if (currentLine.matches("\\d+"))
    {
        value = Integer.parseInt(currentLine);
    }
    else if (currentLine.matches("\\w+"))
    {
        value = wordValueMap.get(currentLine);

        if(value == null)
        {
            int binaryValue = wordValue++;
            wordValueMap.put(currentLine, binaryValue);
            /*
             * This is just there to ensure that the print statement below doesn't have a 
             * null value.
             */
            value = binaryValue;
        }
    }
    else
    {
        System.out.println("Invalid input");
        break;
    }

    System.out.println(Integer.toBinaryString(value));
}

As a side note, I hope you know that this will fail in the event of a number followed by a words, like sentences. But if your input will never have any of that, it's a non-issue.

这篇关于读取字符并检查它们是否是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆