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

Thread: I'm a java beginnier

  1. #1
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I'm a java beginnier

    i'm trying to write a program that displays your initials after you input your full name, only using the string methods .

    I'm at a loss, can anyone help ??


  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: I'm a java beginnier

    What have you tried? Where are you stuck?
    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
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    I've got this so far, but the program has to be for ANY name

    import java.util.Scanner;
    public class Initials {
    public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    System.out.println("Please enter your FULL name");

    String name=input.nextLine();
    String index = name.substring(0,1);



    System.out.println(index);

    This just gives the first initial, but i dont know what to do if the user enters three names (First , middle and last names)

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I'm a java beginnier

    @reddevilggg: Kindly use
     
    tags to enclose your code or read the forums rules.
    Okay, so far now, what you have tried is good enough for a beginner. Forexample,
    Andrew Hawks Jhonson is the input.
    Now you will have to get AHJ.
    First substring, you got already.
    Now find the space as there are many functions provided by String.
    You can easily find the space and then get the index of that location. Add one and get the Character/substring if there is something.
    And so on keep tracking unless you are done with the string buffer/got null.

  5. #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: I'm a java beginnier

    There are quite a few ways to do this. You could split on spaces or iterate over each char, testing whether each is an initial (what that means is up to you). Check out the API for useful methods.
    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!

  6. #6
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    Sorry about not enclosing the code in tags.

    You say
    "Now find the space as there are many functions provided by String.
    You can easily find the space and then get the index of that location. Add one and get the Character/substring if there is something."

    This is my problem, i understand the logic and i know what i want to do, but i'm still at a loss how to do it.
    How do i find the functions provided by the String ??

    I need to find the spaces then charAt +1, maybe.

  7. #7
    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: I'm a java beginnier

    The API is your best friend: String (Java Platform SE 6)
    Java Platform SE 6
    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!

  8. #8
    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: I'm a java beginnier

    How do i find the functions provided by the String ??
    Take a look at the API - it has all java SE classes, their methods and available variables.
    String (Java Platform SE 6)
    There are a number of methods you could use to implement the provided suggestions, and its worth taking some time to study.

    Edit: too slow once again, Kevin beat me to it.

  9. #9
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    Cheers. i'll take a look

    Thanks again

  10. #10
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    I'm still having problems, as you can see in the code below, the output displays the first initial then the others as an int (number of spaces), i know i've got the code as an int, but this is the only way i can get it to output anything. I dont know how to get any char method to work. Can you point me in the right direction?? I'm pulling my hair out !!

    import java.util.Scanner;
    public class Initials {
        public static void main(String[] args) {
     
            Scanner input = new Scanner (System.in);
            System.out.println("Please enter your FULL name");
     
     
            String name=input.nextLine();
            String index = name.substring(0,1);
            int mid = name.indexOf(" ") +1;
            int last = name.lastIndexOf(" ") +1;
     
            System.out.println(index + mid + last);
     
        }
    }
    Last edited by reddevilggg; September 28th, 2011 at 05:48 AM.

  11. #11
    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: I'm a java beginnier

    Why are you using the indexOf() method? More accurately, why are you simply printing out the value returned from it? That gives you the position of the space, but that's not what you want to print out as an answer. You want the next letter, right? And what method gives you a part of a String given a position?
    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!

  12. #12
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    i'm using the indexOf method because its part of a tutorial i'm following to make sure that i'm understanding how to use it (ironic, i know). It gives the poistion of the space +1, which will give the initial not the space. I've checked the output and it does give the correct position for the initial, but as a number, because i'm incorrectly using int (i think).

    I dont understand your last question
    And what method gives you a part of a String given a position?

  13. #13
    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: I'm a java beginnier

    You aren't incorrectly using int, you're just not taking the next step. You have the position of the character you want. Now you need a method that gets that character. Hint: You're already using one of the methods you could use to retrieve it.

    Think about it this way. If I tell you my name is "Kevin Workman" and ask you what my last initial is, you aren't going to say 6. You're going to tell me the 6th letter (indexes start at zero).
    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!

  14. #14
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    yeah, i understand the logic behind it. Thats as far as it goes. I been trying to include a char method, but i just keep get red errors. I'm lost.

  15. #15
    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: I'm a java beginnier

    You already use the subString() method. That's one way to get the character at a certain location. Have you tried that?
    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!

  16. #16
    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: I'm a java beginnier

    Nevermind, I guess I'm wasting my time here.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/49066-dont-know-how-use-lastindexof.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    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!

  17. #17
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    Yeah, i've tried loads of ways, but being a beginner it's the syntax that lets me down. i'm not sure exactly what or how to get it correct. Everything i do either doesnt work or is an error.

    I apologise, being a newbie here im not up to speed with the rules.
    Last edited by reddevilggg; September 28th, 2011 at 09:51 AM.

  18. #18
    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: I'm a java beginnier

    You already have the syntax though. You already use the substring() method. I don't understand what you're stuck on. Make an attempt and post what you have instead of giving up and crossposting.
    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!

  19. #19
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    This is it so far, i just do not understand what i need to do

    import java.util.Scanner;
    public class Initials {
        public static void main(String[] args) {
     
            Scanner input = new Scanner (System.in);
            System.out.println("Please enter your FULL name");
            String name=input.nextLine();
     
     
            String index = name.substring(0,1);
            int mid = name.indexOf(" ") +1;
            int last = name.lastIndexOf(" ") +1;
            String midInitial = name.substring(mid);
     
     
            System.out.println(index + midInitial);

  20. #20
    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: I'm a java beginnier

    Okay, and what does that output?

    Take a look at the String API: String (Java Platform SE 6)

    Pay special attention to the different substring() methods. See the one that takes two arguments, a beginning and an end index? Compare that to the one that takes a single argument. Do you now understand why you're getting the output you're getting?

    PS- I don't think that lastIndexOf() is going to get you anything useful, since there's only going to be one space.
    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!

  21. #21
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    I don't think that lastIndexOf() is going to get you anything useful, since there's only going to be one space.
    There is going to be 2 spaces as i have to assume that the user input 3 names, first, middle and last name.

    I've tried loads of things, nothing works for me, the longer i stay on this tutorial the less i seem to know, i feel like i'm going backwards.

    Thanks for helping, but i dont think i'm going to get it.

  22. #22
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    I've tried the substring and this is the best i can do

    Scanner input = new Scanner (System.in);
    System.out.println("Please enter your FULL name");
    String name=input.nextLine();

    String index = name.substring(0,1);
    int mid = name.indexOf(" ") +1;
    int last = name.lastIndexOf(" ") +1;

    System.out.println(index + mid + last);
    System.out.println(name.substring(mid, last +1));
    The output is
    Please enter your FULL name
    John Paul Jones
    J510
    Paul J
    Which shows the first initial, then first space +1 and second space +1. Then what is inclusive of the name substring. This is the best i can do after hours of playing with it. Can you help ??

  23. #23
    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: I'm a java beginnier

    Maybe you should write a function that takes two parameters- a String, and an int. The function returns the String value of the character at that position. You already do that.

    When you have that function working, then figure out what you have to pass into it (hint: the first argument can always be the full String) to get the characters you care about.
    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!

  24. #24
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I'm a java beginnier

    Yeah the reason is only index is a String variable and mid, last are integer variables. As far as i can guess, mid and last are index of array where that character is. You must find out that character and print that.
    Like;
    String middle=name.substring(mid, mid+1) etc

  25. #25
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a java beginnier

    Thank you both for your help, and due to your help i've done it . Thanks Again