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

Thread: Basic Java Question

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Java Question

    Hi all so I'm very new to Java and am working on a project which requires my program works properly with all name inputs, but I coded it to only output what I need with one.

    What I did was initialize my fullname into first and lastname using substrings, but that obviously won't work for any name because there will be a different number of characters.

    Here's the code :

     
    public class Proj2
    {
        public static void main(String[] args)
        {
     
     
     
    		 String fullname = "dAvId lAwReNt";
           String firstname;
           String lastname;
     
     
           firstname = fullname.substring(0,5);
            lastname = fullname.substring(6,13);
     
    		int count = fullname.length();
     
    		System.out.println("Index of Space:" + fullname.length());
     
           System.out.println("First Name: " + firstname + " has " + firstname.length() + " letters.");
     
    		 System.out.println("Last Name: " + lastname + " has " + lastname.length() + " letters.");
     
     
    		 firstname = firstname.toUpperCase();
    		 lastname = lastname.toUpperCase(); 
     
    			System.out.println("Initial of the first name in uppercase: " + firstname.charAt(0));
     
    			 firstname = firstname.toLowerCase();
     
    			System.out.println("Rest of the first name in lowercase: " + firstname.substring(1));
     
     
    			System.out.println("Initial of the last name in uppercase: " + lastname.charAt(0));
     
    			lastname = lastname.toLowerCase(); 
     
    			System.out.println("Rest of the last name in lowercase: " + lastname.substring(1));
     
     
    			firstname = firstname.toLowerCase();
    		   lastname = lastname.toLowerCase(); 
     
     
    			firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1);
    			lastname = lastname.substring(0, 1).toUpperCase() + lastname.substring(1);
     
     
     
    			 System.out.println("My name is " + firstname + " " + lastname);
           }
           }

    I just need to set firstname and lastname without using substrings since that won't work if i were to replace the name with another with a different amount of characters


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Basic Java Question

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Java Question

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.
    Thanks! I edited my post.

    -Also, I have figured out I need to use IndexOf but I'm not sure how to implement it.
    Any help is greatly appreciated!

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Basic Java Question

    You might also check out String.split().

    Using indexOf(), If you know the index of the space, then you know how to form the substrings which will resolve your initial concern.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Java Question

    I'm not allowed to use string.split yet I don't think
    fullname.indexOf(fullname); Would that give me the index of the space?

    I'm still a little foggy on how to initialize it after finding it


    Edit:

    fullname.indexOf(' ') Gives me "5" for index of space.

  6. #6
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Basic Java Question

    Hi,


    For splitting the fullname you can also use stringtokenizer.

    Thanks,

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Basic Java Question

    Quote Originally Posted by Ganeprog View Post
    For splitting the fullname you can also use stringtokenizer.
    A summary from the API docs:
    StringTokenizer is a legacy class...its use is discouraged in new code.

    I'm still a bit foggy as to the goal...are you wishing to get the full name (last and first) as two Strings? Is the full name supposed to be hard-coded, or flexible from the standpoint of accepting the full name from the command line? If you wish to simply 'split' on a space, see (and I recommend play with the methods from) the API of the String, in particular indexOf(), substring(), and (as alluded to earlier) split().

Similar Threads

  1. Basic java object question
    By BimmyJim in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2013, 08:56 AM
  2. basic java question
    By jim213mm in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 01:30 PM
  3. Basic Java question
    By fred2028 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 13th, 2011, 03:54 PM
  4. Basic java question
    By erosgol in forum Java Theory & Questions
    Replies: 5
    Last Post: September 2nd, 2011, 05:24 PM
  5. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 06:21 PM