Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-23-2008, 04:27 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
[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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-23-2008, 05:02 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-24-2008, 12:39 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-27-2008, 03:51 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
Hardwired, thank you very much!

It is greatly appreciated.

+rep.
__________________
I am a Java n00b.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big Faceless Report Generator 1.1.39 Java Tip Java Announcements 0 04-26-2008 10:31 PM
random numbers without random class` carlos123 New To Java 4 01-18-2008 12:44 AM
Random Generator padutch2 New To Java 1 12-03-2007 08:43 PM
Help needed writing a program... Francis New To Java 2 11-22-2007 04:03 PM
Database Bean Generator 2.1.2 JavaBean Java Announcements 1 07-17-2007 02:48 PM


All times are GMT +3. The time now is 07:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org