CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Posts
    20

    point me in the right direction, would be very helpful--Java-HELP PLEASE

    Hello to all the Java Gurus....Please help!
    I am very close to completing this assignment and I can not figure out what I am doing wrong. The output is suppose to be my 3 initials(Capitalized) and then a 2 digit random number after the initials. my output is giving me 2 digits after each initial. Can some one please help me. OUTPUT is suppose to look like this......DES34.


    import java.util.Random;
    import java.util.Scanner;
    import java.lang.String;

    public class UserID
    {
    public static void main(String args[]) {

    Scanner login = new Scanner(System.in);

    System.out.println("Enter your full Name: ");
    String Fullname = login.nextLine();
    Fullname = (Fullname).toUpperCase();
    String[] splits = Fullname.split(" ");
    for(String Fname: splits){;
    char Finame = Fname.charAt(0);
    char endname = Finame;

    Random r = new Random();

    String userid = "";
    for (int i = 10; i < 90;++i){
    int rand = r.nextInt(1) + 90;
    userid = String.valueOf(rand);}

    String User = ("" + endname + userid);

    System.out.print(User);
    }
    }
    }

    Output:

    Enter your full Name:
    dan eben stevens
    D90E90S90
    Last edited by dstevens; January 20th, 2013 at 06:43 PM. Reason: Misspelling

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: point me in the right direction, would be very helpful--Java-HELP PLEASE

    I have reformatted and slightly amended your code ...
    Code:
    import java.util.Random;
    import java.util.Scanner;
    import java.lang.String;
    
    public class UserID
    {
        public static void main(String args[]) {
            Scanner login = new Scanner(System.in);
    
            System.out.println("Enter your full Name: ");
            String Fullname = login.nextLine();
            Fullname = (Fullname).toUpperCase();
            String[] splits = Fullname.split("\\s+");
            for(String Fname: splits) {
                char Finame = Fname.charAt(0);
                char endname = Finame;
    
                Random r = new Random();
    
                String userid = "";
                for (int i = 10; i < 90; ++i) {
                    int rand = r.nextInt(1) + 90;
                    userid = String.valueOf(rand);
                }
    
                String User = (endname + userid);
    
                System.out.println(User);
            }
        }
    }
    It should become clear from the indentation that your first "for" loop (the one which iterates round the "splits" array) also generates and appends the "random" number. Changing the final print to a println makes this more obvious:
    Code:
    Enter your full Name: 
    John peter smith
    J90
    P90
    S90
    So maybe your first for loop should be more like:
    Code:
            String endname = "";
            for(String Fname: splits) {
                char Finame = Fname.charAt(0);
                endname = endname + Finame;
            }
    Note that this (above) is not good code - it's a very inefficient way of appending characters to a string, but I don't want to introduce the complexity of using a string buffer to do it the correct way.
    Now, looking at the "random number" generator:
    Code:
                String userid = "";
                for (int i = 10; i < 90; ++i) {
                    int rand = r.nextInt(1) + 90;
                    userid = String.valueOf(rand);
                }
    Random.nextInt(1) will always return 0. Javadoc for Random class states : Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive). It is being executed 79 times by the for loop. I suspect what you really want is something like:
    Code:
            String userid = "";
            int rand = r.nextInt(90) + 1;
            userid = String.valueOf(rand);
    Which will generate a random number between 0 and 89, then add 1 to it giving a random number between 1 and 90.
    Then simply concatenate the "endname" and "userid" variables and you're done.
    Code:
    Enter your full Name: 
    john peter smith
    JPS51

  3. #3
    Join Date
    Jan 2013
    Posts
    20

    Re: point me in the right direction, would be very helpful--Java-HELP PLEASE

    TY very much AlexVV, I am quite new to Java and trying to learn quickly the basics of Java. I have posted a few other codes and if you would be nice enough to point me or guide me in the right direction, I would be very helpful. You were a lot of help on this matter, Thanks again

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured