java sentence generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wohast
    New Member
    • Feb 2008
    • 2

    java sentence generator

    Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else.

    ask user:
    "Please enter your name followed by a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442)."

    how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (?

    also

    how do i use the input integer (1442) as a seed for my random number generator?

    Random rand = new Random(number);
    randNum = rand.nextInt(9) ;


    thanks in advance...this is a description of the assignment in case what I have said is not clear:

    "Use JOptionPane to design a simple English language sentence generator with a random component, so that the user never knows quite what to expect for the output sentence, unless one “special” name is entered. This sentence generator should read expressions composed of a person’s name followed by a positive integer between 1 and 9999 enclosed in parentheses, such Cupid(1442). For output, it should produce a complete sentence about the person, such as “Cupid is a great Java programmer!” The procedure for completing the sentence is as follows:

    The program should pass the input integer as a “seed” to a pseudo-random number generator object (see below).
    The random number generator will use the seed to produce a single-digit integer between 0 and 9 as output.
    Write your program so that the sentence completion is based upon the output random integer, or a match with the “special” name.
    To reduce the number of sentences that you have to code, you may use one sentence completion for multiple random integers or “special” name match, as shown in the examples below:

    Random number is 0, 1, or 2: “ is new to Java, and so his code is sometimes off target.”
    Random number is 3, 4, or 5: “ is right on the mark with his Java conditional statements.”
    Random number is 6, 7, or 8: “ really knows how to pair up a great algorithm with great code!”
    Input name matches defined “special” name, or random number is 9: “, you’re the best!”"
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by wohast
    how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (?

    also

    how do i use the input integer (1442) as a seed for my random number generator?

    Random rand = new Random(number);
    randNum = rand.nextInt(9) ;
    1. Have you read the API for substring? In fact, scan the API for String for other handy methods.
    2. Yes, that's how you can use Random.


    For starters, write a test program:
    [CODE=Java]public class Test {
    public static void main(String[] args) {
    String input = "Cupid(1442 )";
    ...
    String name = ...
    int seed = ...
    System.out.prin tln(name);
    System.out.prin tln(seed);
    }
    }[/CODE]
    Don't cheat and assume fixed sizes for the parts!

    Comment

    • wohast
      New Member
      • Feb 2008
      • 2

      #3
      thanks for the reply...

      Code:
          
      public static void main(String[] args)
      
        {
          String inputString;
          String firstName;
          
          inputString = JOptionPane.showInputDialog
                     ("Please enter your name followed by a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442).");
          firstName = inputString.substring(0, 
          }
      }
      inputString.sub string(0, *******)

      obviously the name that they input is going to start at 0 and will finish at the (.
      what i am trying to figure out is if there is anything i can put in place of the stars so that it will end reading the name at the open parentheses.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by wohast
        obviously the name that they input is going to start at 0 and will finish at the (.
        what i am trying to figure out is if there is anything i can put in place of the stars so that it will end reading the name at the open parentheses.
        Have you scanned through the API of String for a likely method yet?

        And come to think of it, are you aware of the existence of the API? http://java.sun.com/javase/6/docs/api/

        Comment

        • rollypopgirl
          New Member
          • Feb 2008
          • 1

          #5
          Originally posted by wohast
          Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else.

          ask user:
          "Please enter your name followed by a positive integer between 1 and 9999 enclosed in parentheses, such as Cupid(1442)."

          how do i use substring to take out just the name that they enter, so that it starts at the first letter they enter, and ends at the (?

          also

          how do i use the input integer (1442) as a seed for my random number generator?

          Random rand = new Random(number);
          randNum = rand.nextInt(9) ;


          thanks in advance...this is a description of the assignment in case what I have said is not clear:

          "Use JOptionPane to design a simple English language sentence generator with a random component, so that the user never knows quite what to expect for the output sentence, unless one “special” name is entered. This sentence generator should read expressions composed of a person’s name followed by a positive integer between 1 and 9999 enclosed in parentheses, such Cupid(1442). For output, it should produce a complete sentence about the person, such as “Cupid is a great Java programmer!” The procedure for completing the sentence is as follows:

          The program should pass the input integer as a “seed” to a pseudo-random number generator object (see below).
          The random number generator will use the seed to produce a single-digit integer between 0 and 9 as output.
          Write your program so that the sentence completion is based upon the output random integer, or a match with the “special” name.
          To reduce the number of sentences that you have to code, you may use one sentence completion for multiple random integers or “special” name match, as shown in the examples below:

          Random number is 0, 1, or 2: “ is new to Java, and so his code is sometimes off target.”
          Random number is 3, 4, or 5: “ is right on the mark with his Java conditional statements.”
          Random number is 6, 7, or 8: “ really knows how to pair up a great algorithm with great code!”
          Input name matches defined “special” name, or random number is 9: “, you’re the best!”"
          HEY WHAT'S UP
          HAVE YOU ALREADY FINISHED YOUR PROGRAM? SEND ME YOUR EMAIL MAYBE WE CAN HELP EACH OTHER. IM DOING THE SAME PROGRAM RIGHT NOW :S

          Comment

          Working...