The Artima Developer Community
Sponsored Link

Java Answers Forum
Substrings

3 replies on 1 page. Most recent reply: Oct 18, 2010 1:27 PM by Chris Druce

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Chris Druce

Posts: 2
Nickname: drucey
Registered: Oct, 2010

Substrings Posted: Oct 18, 2010 5:27 AM
Reply to this message Reply
Advertisement
Hello everyone, i have just started uni and have to pick up Java for the first time. Im working on a program that creates a username taking the first letter from the forename and 6 from the surname.

userid = forename.substring(0,1) + surname.substring(0,6);

This is the code i am using but it does not cater for the input of 5/4/3/2/1 letter surnames. Is there a way i can make it work? So joe blogg could be jblogg and joe bloggs could be jbloggs?

Many thanks

Chris


George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Substrings Posted: Oct 18, 2010 6:19 AM
Reply to this message Reply
public class Test {
 
    public static void main (String[] args) {
        String first = "joe";
        String last = "blogg";
        String username = take(first, 1) + take(last, 6);
 
        System.out.println("first: [" + first + "] last: [" + last + "] " +
                           "username: [" + username + "]");
 
        last = "blog";
        username = take(first, 1) + take(last, 6);
 
        System.out.println("first: [" + first + "] last: [" + last + "] " +
                           "username: [" + username + "]");
    }
 
 
    /*
     * Returns a substring consisting of the first n characters of
     * a string, or else the whole string if it has less than n
     * characters.
     */
    static String take(String str, int n) {
        String substr;
 
        if (str.length() > n) {
            substr = str.substring(0, n);
        }
        else {
            substr = str;
        }
        return substr;
    }
}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Substrings Posted: Oct 18, 2010 7:19 AM
Reply to this message Reply
// Take the minimum of 6 and the surname length.
int len = Math.min(6, surname.length());
 
userid = forename.substring(0,1) + surname.substring(0,len);

Chris Druce

Posts: 2
Nickname: drucey
Registered: Oct, 2010

Re: Substrings Posted: Oct 18, 2010 1:27 PM
Reply to this message Reply
Ah thanks so much guys - i guess the len would be the easier as i should of explained its a user input (Via Scanner). All good to know though,

Thanks again

Flat View: This topic has 3 replies on 1 page
Topic: Quine-McCluskey Tabulation Method Previous Topic   Next Topic Topic: java program help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use