Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default writing a program to choose a sentence and random stuck

    Using java.util.Random to choose a random sentence and print it


    Hi everyone,

    First post here! To preface, I am extremely new to Java and programming in general. The extent of my work with the program is simple JOptionPane's and simple math problems and the use of the Scanner to plug numbers into equations.

    Currently I am trying to write a simple program that selects a String sentence from a list of 4 sentences at random and print it. I am stuck as to how to do this. Here is my syntax so far:


    Code:

    --------------------------------------------------------------------------------
    Java Code:
    import java.util.Random;
     public class Sentences {
         public static void main(String[] args) {
         
        Random sentences = new Random();
             
        String sent1 = "This is a test!";
         String sent2 = "This is a test quote!";
         String sent3 = "This is a test quote again!";
         String sent4 = "This is a test quote again again!";
     
            System.out.println ();  
        }  
    }
    Last edited by Eranga; 01-24-2012 at 08:56 AM. Reason: code tags added

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: writing a program to choose a sentence and random stuck

    Dont use 4 single string variables, use a string array, so you can generate with help of the random class a integer from 0 to 3. 0 = array[0] = string1 !

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: writing a program to choose a sentence and random stuck

    Keep your sentences in an array (or any other collect. forget about is if you don't know about it). Pick the random number within the valid range (which is through the number of sentences you have) and pick the sentence and display. In that way you know need to stick to four sentences.

    And also please use code tags when you are posting code segments next time. Sometimes unformatted codes are difficult to read.

  4. #4
    vinci123 is offline Member
    Join Date
    Aug 2011
    Location
    INDIA
    Posts
    64
    Rep Power
    0

    Default Re: writing a program to choose a sentence and random stuck

    or for more info do read head first java .
    also use an array instead of variables
    I can and I will

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2012, 10:12 AM
  2. stuck on writing Date application
    By gabrielpr12 in forum New To Java
    Replies: 2
    Last Post: 01-07-2012, 09:31 PM
  3. Random sentence generator
    By BrAD19924 in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 03:58 AM
  4. Replies: 2
    Last Post: 04-03-2009, 05:37 AM
  5. Program where the user can choose the algorithm
    By le_albina@hotmail.com in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 05:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •