Results 1 to 4 of 4
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default [SOLVED] Help with program please (Random Generator Help needed)

    Hey. Here is my problem. I know how to asks a user for their first and last name. But, I can't get it to where it randomly generates one letter from the first name and the last name. The letter that is randomly selected from each name then becomes the first letter in that name (first or last).

    Example:

    Say I input my name.

    John Doe

    and it random selects the "O" from the first name and the "E" from the last name.

    Output = Ojhn Eod.




    ...So bascially my only request is to get the code for the random generator to do this tasks. Hope you can help!
    I am a Java n00b.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Look at the Random class in the API. Then you just tell the String to take that character position in the string to be removed and the add it to the beginning.
    My IP address is 127.0.0.1

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class RandomLetter {
        static Random seed = new Random();
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            do {
                System.out.print(" enter first name ");
                String first = scanner.nextLine();
                System.out.print(" enter last name ");
                String last = scanner.nextLine();
                System.out.printf("first = %s  last = %s%n", first, last);
                String alterFirst = alter(first);
                String alterLast = alter(last);
                System.out.printf("alterFirst = %s  alterLast = %s%n",
                                   alterFirst, alterLast);
                System.out.println("\"x\" & enter to quit, else enter");
            } while(!scanner.nextLine().trim().toLowerCase().equals("x"));
            scanner.close();
        }
    
        private static String alter(String in) {
            int index = seed.nextInt(in.length());
            String letter = String.valueOf(in.charAt(index));
            int end = in.length();
            return letter + in.substring(0, index) + in.substring(index+1, end);
        }
    }

  4. #4
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Hardwired, thank you very much!

    It is greatly appreciated.

    +rep.
    I am a Java n00b.

Similar Threads

  1. Big Faceless Report Generator 1.1.39
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-26-2008, 09:31 PM
  2. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  3. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 PM
  4. Help needed writing a program...
    By Francis in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 03:03 PM
  5. Database Bean Generator 2.1.2
    By JavaBean in forum Java Software
    Replies: 1
    Last Post: 07-17-2007, 01:48 PM

Posting Permissions

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