Java,检查是否为int并执行某些操作(如果它不是int)然后忽略

时间:2014-09-16 15:05:57

标签: java

如果它是一个整数,如何在此方法中执行检查以仅打印出“Message”。 但如果消息不是整数然后不做任何事情,只是等待下一个,我尝试解析它,但如果我打印出“消息”,如果“消息”不是整数,它将打印出空字符串。

void startListenForTCP (String ipaddress){




Thread TCPListenerThread;
   TCPListenerThread = new Thread(new Runnable() {
       @Override

       public void run() {

           Boolean run = true;
           String serverMessage = null;
           InetAddress serverAddr = null;


           try {

               Socket clientSocket = new Socket(ipaddress, 7420);

               try
               {
                   mc.pushNumbers("Connection initiated... waiting for outputs!"+"\n");
                   char[] buffer = new char[2048];
                   int charsRead = 0;  
                   BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                   while ((charsRead = in.read(buffer)) != -1)
                   {


                        String message = new String(buffer, 0, charsRead);

                       String m = message;

                       //I need to print out 'm' if it an numberhere

                       System.out.println("Result:"+m);
                       mc.pushNumbers(m);


                   }}
               catch(UnknownHostException e) {

                   mc.pushNumbers("Unknown host..."+"\n");
               } catch(IOException e) {
                   mc.pushNumbers("IO Error..."+"\n");
               } finally {
                   clientSocket.close();
               }

           } catch (IOException e) {
               e.printStackTrace();
                 mc.pushNumbers("Connection refused by machine..."+"\n");
           }
       }

   });
TCPListenerThread.start();
}

5 个答案:

答案 0 :(得分:2)

使用Integer.parseInt是判断字符串是否为整数的一种方法;另一个简单的方法是查看字符串是否完全由数字组成(至少有一个):

if (m.matches("\\d+")) {
    mc.pushNumbers(m);
}

或者,如果您需要允许负数,

if (m.matches("-?\\d+")) {
    mc.pushNumbers(m);
}

这种方法的主要区别在于它允许数字字符串太大而无法容纳int。根据您的需要,这可能是加号或减号。

答案 1 :(得分:0)

将打印放在try-catch中进行解析应该可以胜任。在声明消息变量后添加它:

//Rest of your code...

String message = new String(buffer, 0, charsRead);
try{
    int i = Integer.parseInt(message);
    System.out.println(i);
    mc.pushNumbers(message);
} catch(NumberFormatException e){
    //Input was not a number... Do nothing else
}

//Rest of your code...

这样,如果消息成功解析为int,则执行打印并推送字符串。如果它不是你既不打印它也不推它。你可以做一些其他事情(在catch块中),但它似乎不是你想要的。

答案 2 :(得分:0)

如何使用Regex

  

\ d --->任何数字,[0-9]

的缩写
   String regex = "[0-9]+|-\\d+"; <---- **Accepting positive and negative number** 

    // Accepting negative number
    String number= "-123456789";
    if (data.matches(regex)) {
        System.out.println(number);
    } else {
        System.out.println(" I am empty sentence");
    }
    // Accepting Positive number
    String number= "123456789";
    if (data.matches(regex)) {
        System.out.println(number);
    } else {
        System.out.println(" I am empty sentence");
    }

    String string = "I am a String";
    if (string.matches(regex)) {
        System.out.println(string);
    } else {
        System.out.println("I am empty sentence");
    }

输出:

123456789
-123456789
I am empty sentence

来源:http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

答案 3 :(得分:-2)

使用可以将代码格式化为:

try {
  int i = Integer.parseInt(yourString);
  mc.pushNUmber(i);
} catch (NumberFormatException e){

}

答案 4 :(得分:-2)

这有帮助吗?

 if(Character.isDigit(evt.getKeyChar())){
   // put something here

 }
 else if(!(Character.isDigit(evt.getKeyChar())){
   //.......     
    }
相关问题