Results 1 to 6 of 6
  1. #1
    hhale is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Random Strings help!

    So I have this problem and it is completely stumping me..

    Here is what I have to do so you can get an idea : I need to make a program that will read a string and long from the keyboard. Then I have to use the long as a seed for creating a Random object.

    Now, using the Random object, I need to generate two random numbers. The first random number will be the starting position for creating a substring from the original string, and, the second random number will be the end point of the string. Next, the two numbers will be used to extract a substring from the original string. Finally, the random numbers should be generated so the substring extracted contains at least two characters.

    My teacher gave us this problem and Im not skilled enough to figure it completely out..


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    class Randomstrings
    { 	
    	static Random seed = new Random();
    	
    	public static void main (String [] args )
    	{
    	Scanner scan = new Scanner( System.in);	
    	
    	System.out.print(" Enter a string: ");
    	String first = scan.nextLine();
    	
    	System.out.print(" Enter a seed: ");
    	long alpha = scan.nextLong();
     	
    	System.out.println( "\n The original string: " + first );
    	
    	Random generator1 = new Random();
    	int r = generator1.nextInt();
    	 
    	Random generator2 = new Random();
    	int s = generator2.nextInt();
    	
    	System.out.println( "\n Creating a substring from " + r + " to " + s);
    	
    	}
    }
    That is what I have so far... butcher it please.. any help is awesome! Thanks!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Random has another nextInt method, namely one where you provide the "maximum" value, so provide that the length of the String (-1 so the substring will be at least 1 character long) the first time, and the length of the String - the first number the second time (then the end index will be the first number plus the second number and the second index has no chance of being either beyond the end of the String, nor smaller than the first index).

    And, why are you not using the Random constructor that takes a long as the seed, since the instructions plainly state that you should be doing this.

  3. #3
    hhale is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default ..

    Ok so Im going back over my notes/book for info on the Random class, I probably missed something..

    I'm taking this one step at a time.. but for the output, I can get it to repeat the original string.. but I also have to state a random substring, the random substring in all CAPS, the random substring will all letters lower-case and the random substring with the first and last letter capitalized...

    I feel like I'm missing a good bit of info on the output of the program.. I have most of the input correct up till the Random class. Any pointers?

    Appreciate the help

  4. #4
    ruby&oliver is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    I think I am in your class... I am having the same issue getting the random # to be 2 characters in length.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by hhale View Post
    Ok so Im going back over my notes/book for info on the Random class, I probably missed something..

    I'm taking this one step at a time.. but for the output, I can get it to repeat the original string.. but I also have to state a random substring, the random substring in all CAPS, the random substring will all letters lower-case and the random substring with the first and last letter capitalized...

    I feel like I'm missing a good bit of info on the output of the program.. I have most of the input correct up till the Random class. Any pointers?

    Appreciate the help
    Okay? So try to implement those suggestions, and post your modified code here.

    FYI
    Random (Java Platform SE 6)

    Edit: As far as upper casing there is a method in String.

    String (Java Platform SE 6)

    Edit: And, if it is to be at least 2 characters (rather than at least one which I pointed out how to do), that should be a rather simple adjustment to my previous suggestions.
    Last edited by masijade; 09-23-2009 at 12:46 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    In addition to the above, use only one Random() object. If you have two (as you have), each seeded with the same long, then nextInt() will return the same value on the first as the second.

    You have a perfectly good one in the static "seed" at the top.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 06:24 PM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:44 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
  •