How do I create a if statement that accepts only Integers?
:confused:
Hello I've created a client server program. I'm trying to have the server reply back to the client user that strings in the form of Integers separated by spaces is the only accepted input. I need to have two things.
One method that checks the input and makes sure that its in the form of integers separated by spaces
Seconds method that takes the string parses it into integers, adds them together and prints the SUM.
This is the pseudocode that I've come up with
Code :
clientInput = inFromClient.readLine();
***//Check that the input from client is in proper integer format
***//Adds integers together and returns result
IntegerSum = parse.Integer(clientInput) + '\n';
//Throw an argument exception if format is incorrect
*** if(clientInput.contains(anything other than integers)){
throw IlleagleArgumentException("You can only enter integers in the following format
1 2 10 19 22")}
if(loadon==1){
//Display the input received.
System.out.println("caught: " + clientInput);
//Write out line to socket
outToClient.writeBytes(IntegerSum);
}
Can someone please show me how to do the code in *** properly?
Re: How do I create a if statement that accepts only Integers?
Surely you don't want us to simply do your work for you right? Usually it's best for you to attempt to solve this yourself first, and then if that doesn't work, post your code attempt with specific questions. Else how will we know just what confuses you?
Re: How do I create a if statement that accepts only Integers?
Where can I find documentation to learn on these topics? Its hard to find detailed examples of these things.
Re: How do I create a if statement that accepts only Integers?
Quote:
Originally Posted by
Mnelson
Where can I find documentation to learn on these topics? Its hard to find detailed examples of these things.
It's a common mistake to try to find a specific example of the exact code you're looking for. Rather Just study the basic Java tutorials and the API related to this issue and you'll learn how to code this. For this, you'll need to understand if/else blocks, Java exceptions, and have a look at the Integer class API at its parseInt(...) method.
Re: How do I create a if statement that accepts only Integers?
There are also other classes in the API that you can use. One very common (and useful) class is the Character class.
Just to give you a tip, I suggest you take a look at what methods the Character class can use.
One method you may be interested in is isDigit(char c)..