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();
}
4
  • 2
    Can you point out to the line or lines in your code that you did parsing? Commented Sep 16, 2014 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. Commented Sep 16, 2014 at 15:10
  • it is very unclear what you are asking or saying? can you clarify that ? Commented Sep 16, 2014 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. Commented Sep 16, 2014 at 15:15

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

-2

Can this help?

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

 }
 else if(!(Character.isDigit(evt.getKeyChar())){
   //.......     
    }

1 Comment

This is very odd code. Presumably isDigit returns a Boolean, so you don't need an else if just an else.
-2

Use can format your code as :

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

}

4 Comments

Exception in thread "Thread-2" java.lang.RuntimeException:Uncompilable source code - illegal start of type This is the error i get.
@wumm but once i fixed the catch block then ? I though it was obvious that OP would understand that...
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.