Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with Java Code.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool [SOLVED] Help with Java Code.

    Hi guys. Im having a bit of an issue with my java code here.
    Basically what this code should do is:

    1) Asks the user for their first name.
    2) Takes that first name and generates a random number from it based on its length.
    3) Take that number it generates from the length then prints it in the output as a CharAt();

    I guess what my problem is really is the random number generation at the end of the code for a number been 100 and 999. I am not sure whether I have done it correctly.
    Can anyone assist me with this.

    Regards
    Matt

    import java.util.Random;
    import java.util.Scanner;
     
    public class AS1Q2 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner (System.in);
     
    		String firstName;
     
    		System.out.println ("Enter Your First Name: "); // Using print line will allow the user to enter their name below the Console txt showing "Enter Your Name: "
    		firstName = scan.nextLine();
     
     
    		Random random = new Random(); // Generates first value for Letter conversion //
     
    		int nameGen1;
    		nameGen1= random.nextInt(firstName.length()); 
     
    		int nameGen2;
    		nameGen2 = random.nextInt(firstName.length()); 
     
    		int nameGen3;
    		nameGen3 = random.nextInt(firstName.length()); 
     
    		//Random genThree = new Random(); // Generates second value for Letter conversion - not required, left in as apart of Troubleshooting ---//
     
     
    		//Random genFour = new Random(); // Generates third value for Letter conversion  - not required, left in as apart of Troubleshooting ---//
     
     
     
    		//--------------------------Generates Last Three Numbers of Rego Between 100 and 999 -----------------------------------//
     
    		Random generator = new Random();
    		int randomEndNumber;
    		randomEndNumber = generator.nextInt(900) + 100 - 1;
     
    		//----Output---//
     
     
    		System.out.println("Your Registration Number is: "+firstName.toUpperCase().charAt(nameGen1)+""+firstName.toUpperCase().charAt(nameGen2)+""+firstName.toUpperCase().charAt(nameGen3)+" "+randomEndNumber);
     
     
    	}
     
    }
    Last edited by metalx66; March 16th, 2011 at 09:08 PM. Reason: Solved


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with Java Code.

    Hello metalx66. Welcome to the Java Programming Forums.

    This all looks correct to me. I have used System.out.println(); to see what values are being generated.

    To make sure the randomEndNumber is working correctly, try reducing the range.

    import java.util.Random;
    import java.util.Scanner;
     
    public class AS1Q2 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner (System.in); 
    		String firstName;
     
    		// Using print line will allow the user to enter their name below the Console txt showing "Enter Your Name: "
    		System.out.println ("Enter Your First Name: "); 
    		firstName = scan.nextLine();
     
    		// Generates first value for Letter conversion //
    		Random random = new Random(); 
     
    		int nameGen1;
    		nameGen1= random.nextInt(firstName.length()); 
    		System.out.println (nameGen1); 
     
    		int nameGen2;
    		nameGen2 = random.nextInt(firstName.length());
    		System.out.println (nameGen2); 
     
    		int nameGen3;
    		nameGen3 = random.nextInt(firstName.length());
    		System.out.println (nameGen3); 
     
    		// Generates second value for Letter conversion - not required, left in as apart of Troubleshooting ---//
    		//Random genThree = new Random(); 
     
    		 // Generates third value for Letter conversion  - not required, left in as apart of Troubleshooting ---//
    		//Random genFour = new Random();
     
     
    		//--------------------------Generates Last Three Numbers of Rego Between 100 and 999 -----------------------------------//
     
    		Random generator = new Random();
    		int randomEndNumber;
    		randomEndNumber = generator.nextInt(900) + 100 - 1;
    		System.out.println (randomEndNumber); 
     
    		//----Output---//
     
    		System.out.println("Your Registration Number is: "+firstName.toUpperCase().charAt(nameGen1)+""+firstName.toUpperCase().charAt(nameGen2)+""+firstName.toUpperCase().charAt(nameGen3)+" "+randomEndNumber);
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    metalx66 (March 17th, 2011)

Similar Threads

  1. calling c code from java
    By sara in forum Java Native Interface
    Replies: 3
    Last Post: April 6th, 2013, 09:53 PM
  2. Need java code help
    By Gradybaby16 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 19th, 2010, 10:54 AM
  3. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  4. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  5. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM

Tags for this Thread