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 8 of 8

Thread: Command For Rolling Random Number

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Command For Rolling Random Number

    I'm learning Java and giving myself small tasks just in my spare time, one thing I've done is having a small command line interface, one of the commands is "roll" followed by a number from 1-9. For example roll 3 = random number with 3 digits.
    How do I stop my command line interface from crashing when you input something like "roll L" or other strings that cannot be converted into an integer?
    My way around this is terrible, but it'll have to do, it looks similar to the code below.

    if(theCommand.equalsIgnoreCase("roll 1") || theCommand.equalsIgnoreCase("roll 2") || theCommand.equalsIgnoreCase("roll 3") || theCommand.equalsIgnoreCase("roll 4") || theCommand.equalsIgnoreCase("roll 5") || theCommand.equalsIgnoreCase("roll 6") || theCommand.equalsIgnoreCase("roll 7") || theCommand.equalsIgnoreCase("roll 8") || theCommand.equalsIgnoreCase("roll 9")){
    //Coding goes here
    }

    As apposed to how I had it converting the string into an integer
    (int x = Integer.parseInt(testCommand.substring(5))
    After this was executed there'd be a random number generated, that number would then be multiplied by (10 to the power of x).

    I just need to limit the input to just "roll 1-9"

    Thank you very much,

    If you're confused, just ask, because I am too..


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Command For Rolling Random Number

    Sounds like you may want to wrap the parse in a try catch clause, and be prepared to catch a number format exception.

  3. The Following User Says Thank You to sunde For This Useful Post:

    Brickwood (March 26th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Command For Rolling Random Number

    Quote Originally Posted by sunde View Post
    Sounds like you may want to wrap the parse in a try catch clause, and be prepared to catch a number format exception.
    That's what I want! But I don't know how to make that..

  5. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Command For Rolling Random Number

    try{
      //try something
    } catch(/*some exception*/){
      //do something as a result of catching the exception
    }
    read up on exceptions if necessary.

  6. The Following User Says Thank You to sunde For This Useful Post:

    Brickwood (March 26th, 2011)

  7. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Command For Rolling Random Number

    Quote Originally Posted by sunde View Post
    try{
      //try something
    } catch(/*some exception*/){
      //do something as a result of catching the exception
    }
    read up on exceptions if necessary.
    I know the try and catch, I just don't know how to make the exception or how to set it out.. That's my problem.

  8. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Command For Rolling Random Number

    When you try and parse an integer that contains something other than 0-9, what happens? Is an exception thrown? If so, which?

    Try catching that exception in the catch and do whatever you want in the handling(exit the program, prompt for correct input, etc.)

  9. The Following User Says Thank You to sunde For This Useful Post:

    Brickwood (March 26th, 2011)

  10. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Command For Rolling Random Number

    Quote Originally Posted by sunde View Post
    When you try and parse an integer that contains something other than 0-9, what happens? Is an exception thrown? If so, which?

    Try catching that exception in the catch and do whatever you want in the handling(exit the program, prompt for correct input, etc.)
    I understand how it works, but how is it done?..

    Do I make a variable? A function? I've looked into it and I'm confused by it.. I need examples but I can't find any in the same context as what I need, I really appreciate the help!

    EDIT: Thank you kind sir, I have found out how to do it!
    Last edited by Brickwood; March 26th, 2011 at 02:39 AM. Reason: Solved

  11. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Command For Rolling Random Number

    I'm glad to have helped.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  3. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 11:00 AM
  4. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 08:50 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM