1

How can implement a check in this method to only print out "Message" if it is an integer. But if the message is not integer then don't do anything, just wait for next one, i tried parsing it but if i print out "message" and if "message" is not integer it will print out just empty string.

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();
}
ovonel
  • 13
  • 4
  • 2
    Can you point out to the line or lines in your code that you did parsing? – Kick Buttowski Sep 16 '14 at 15:07
  • No, no i have tried to parse it and this is without parsing, i'll add parse part at the bottom right away. – ovonel Sep 16 '14 at 15:10
  • it is very unclear what you are asking or saying? can you clarify that ? – Kick Buttowski Sep 16 '14 at 15:12
  • Ok, i get string 'm' from buffered reader, that string can be anything. But i want to print out if the recieved string is an int and if it is not an int then just ignore that string ad continue reading. – ovonel Sep 16 '14 at 15:15

5 Answers5

2

Using Integer.parseInt is one way to tell whether the string is an integer; another simple way is to see if the string consists entirely of digits (with at least one):

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

or, if you need to allow negative numbers,

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

The main difference with this method are that it will allow strings of digits that are too large to fit into an int. This could be a plus or a minus, depending on your needs.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

Putting the printing in a try-catch for parsing should do the job. Add this after you declare the message variable:

//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...

That way if the message successfully parses to an int, you do the printing and push the string. If it doesn't you neither print it nor push it. You could do something else instead (in the catch block), but it doesn't seem that you want to.

Mshnik
  • 7,032
  • 1
  • 25
  • 38
0

How about using Regex

\d ---> Any digit, short for [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");
    }

output:

123456789
-123456789
I am empty sentence

Source: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
-2

Use can format your code as :

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

}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • 2
    Exception in thread "Thread-2" java.lang.RuntimeException:Uncompilable source code - illegal start of type This is the error i get. – ovonel Sep 16 '14 at 15:11
  • @Aeshang http://meta.stackoverflow.com/questions/271482/how-come-wrong-answers-get-upvoted – idmean Sep 16 '14 at 16:43
  • @wumm but once i fixed the catch block then ? I though it was obvious that OP would understand that... – StackFlowed Sep 16 '14 at 16:46
  • 1
    @Aeshang I can't tell you. I just came across the post and since it seemed like you were not aware of the discussion about your post I posted the link. – idmean Sep 16 '14 at 16:48
-2

Can this help?

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

 }
 else if(!(Character.isDigit(evt.getKeyChar())){
   //.......     
    }
user3260589
  • 168
  • 1
  • 4
  • 12