• Post Reply Bookmark Topic Watch Topic
  • New Topic

name character recall help  RSS feed

 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i'm only on like my 5th class but i just got an assignment and i wanna get a head start. i'm supposed to write an application that stores user first name and last name, and then prints out a string of the first letter of the first name and the first five characters of the users last name. followed by a random number from 10-99... (assume that last name has at least 5 characters). i'm not too worried about the random number part, and of course i can string the names but i'm not sure how to obtain the first letter of first name and the first 5 of last name.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome.

If you look at the API docs for the String class you will find some methods that allow you to get a substring of whatever length you want.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Dustin!

Did you start working on your assignment? If you did, you should post your code here to clarify any of your doubts. Tony's recommendation is just the point you should start from - check the API, try to implement what you want (baby steps first), and come back for some help if needed. But we need to see that you at least tried it.
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys i'll check out the API and get back to you. oh and ROFL at the grammar police on thanks XD
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dustin McDuffy wrote:oh and ROFL at the grammar police on thanks XD

it's not just "thx" that we do this for. You may want to read our FAQ on UseRealWords, as well as some of the other items on HowToAskQuestionsOnJavaRanch.
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kk i'll check that out too. here's what i got so far

import java.util.Scanner;

public class test3
{
public static void main(String[]args)
{
Scanner scan = new Scanner (System.in);

String firstname, lastname, fnm;


System.out.print("Enter your first name: ");
firstname = scan.next();
scan.nextLine();

System.out.print("Enter your last name: ");
lastname = scan.next();
scan.nextLine();

fnm = firstname.substring(0, 1);

System.out.print(" test: " +fnm);




}
}



let me know if i'm on the right track it seems to work
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems ok to me.
Now check the API for a method to extract a substring of a given string (in your case, you need to get first n characters from the string containing last name).

And, in the meantime, check UseCodeTags.

Edit: Oh, sorry, I missed it. You already use the substring method to get the first character of name. Use the same to get first 5 (or whatever) characters of the last name, and then move to the random part.
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The rest came easy after that, and now i gotta better grasp of how the int values after the substring work. so... excellent! one assignment down 6 to go =)
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

might get you a value larger than 99. If the nextInt returns 98 (because 99 is not included), you would end with value 108.

Also, once again, please UseCodeTags, so I (and probably anyone else here) will be willing to help you further, if necessary.
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:This:

might get you a value larger than 99. If the nextInt returns 98 (because 99 is not included), you would end with value 108.

Also, once again, please UseCodeTags, so I (and probably anyone else here) will be willing to help you further, if necessary.


so to get a max of 99 i would want to use



is that right?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to generate random number uniformly distributed (that's the distribution Random class uses) in [a, b) you use:
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if, instead of using generator i do


it will include 99 as a value as well as keeping the range between 10-99itself?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it will not include 99, as I wrote above.
Since you're generating integer values, you might put 100 to be upper bound instead of 99, so you'll then get numbers from [10, 100), and never 100.
 
Dustin McDuffy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i think i understand. thanks for the help!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!