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 5 of 5

Thread: Studying for a test, can't figure out my code won't work

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Studying for a test, can't figure out my code won't work

    The question asks to create a program that will read a first and last name from the
    keyboard and create a string that consists of the first letter of the first name, followed by
    the first four characters of the last name followed by a random two-digit integer (10-99).
    You can assume that the last name will have at least four characters in it.
    Here's my code:

    public static void main(String[] args) {
    String name1,name2;
    Scanner scan = new Scanner (System.in);
    Random rand = new Random ();
    int num1;

    num1 = rand.nextInt(89) + 10;
    System.out.print ("Enter your first name: ");
    name1 = scan.nextLine();
    System.out.print ("Enter your last name: ");
    name2 = scan.nextLine();
    System.out.println (name1.charAt (0) + name2.charAt(0) + name2.charAt(1) + name2.charAt(2) + name2.charAt(3) +num1);

    It runs w/o errors, asks for the first and last names, but the result has been a 3 digit number everytime! The last three times ran it gave me 589, 514, 509, and 496. When I remove all references to the name2.charAt(#), it DOES give the first character of name1.

    What have I done wrong? I've looked through my textbook and didn't see any examples that are helpful...

    Any input would be appreciated...
    Thanks,
    -C


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Studying for a test, can't figure out my code won't work

    You're adding a bunch of chars together. Chars can be seen as a subset of ints. Adding them together like that is like adding some ints together. You're going to get a number.

    Look at the String and Char api for useful functions for String conversion, and add those instead. Adding Strings is actually a shortcut for creating a StringBuilder and appending the Strings. There is no such shortcut for chars.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Studying for a test, can't figure out my code won't work

    While I'm at it, why use the charAt() function at all? Looks like a job for substring() to me.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. The Following User Says Thank You to KevinWorkman For This Useful Post:

    coolidge (September 27th, 2011)

  5. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Studying for a test, can't figure out my code won't work

    Thanks KW, the use of substring was the trick. I'm new to java and programming so I'm still fumbling through things from time to time. I sometimes tend to make things harder for myself when I'm learning something new...LOL...

    Thanks again,

    -C

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Studying for a test, can't figure out my code won't work

    Quote Originally Posted by coolidge View Post
    Thanks KW, the use of substring was the trick. I'm new to java and programming so I'm still fumbling through things from time to time. I sometimes tend to make things harder for myself when I'm learning something new...LOL...

    Thanks again,

    -C
    That's true for everybody, so don't be too hard on yourself. You should see me trying to make a basic website with html and css.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 01:37 AM
  2. need to figure how this thing will work
    By frosst in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 26th, 2011, 01:45 AM
  3. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM
  4. I need help with my ohm's law applet code. can't figure it out!
    By dracula2001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 06:16 PM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM