Results 1 to 4 of 4
- 05-23-2008, 02:27 PM #1
[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.
- 05-23-2008, 03:02 PM #2
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
- 05-23-2008, 10:39 PM #3
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); } }
- 05-27-2008, 01:51 PM #4
Similar Threads
-
Big Faceless Report Generator 1.1.39
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-26-2008, 08:31 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
Random Generator
By padutch2 in forum New To JavaReplies: 1Last Post: 12-03-2007, 06:43 PM -
Help needed writing a program...
By Francis in forum New To JavaReplies: 2Last Post: 11-22-2007, 02:03 PM -
Database Bean Generator 2.1.2
By JavaBean in forum Java SoftwareReplies: 1Last Post: 07-17-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks