Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do I create a if statement that accepts only Integers?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I create a if statement that accepts only Integers?



    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

       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?


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How do I create a if statement that accepts only Integers?

    Quote Originally Posted by Mnelson View Post
    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.

  5. #5
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default 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)..
    Last edited by Staticity; July 19th, 2012 at 04:02 PM.
    Simplicity calls for Complexity. Think about it.

Similar Threads

  1. How Compare Integers using the if statement and && operator? Can it be done?
    By sethxhero7 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 31st, 2012, 03:48 AM
  2. [SOLVED] (Simple) How to use an if statement to respond to random integers
    By DusteroftheCentury in forum Loops & Control Statements
    Replies: 9
    Last Post: January 27th, 2012, 09:15 PM
  3. Replies: 2
    Last Post: April 21st, 2011, 10:29 AM
  4. hello.,i would like to create a method that accepts ...
    By amr in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2010, 04:13 PM
  5. Create table statement from DatabaseMetaData
    By sid7 in forum JDBC & Databases
    Replies: 6
    Last Post: August 28th, 2010, 01:23 PM

Tags for this Thread