Results 1 to 3 of 3
  1. #1
    reddevilggg's Avatar
    reddevilggg is offline Member
    Join Date
    Jan 2011
    Location
    Stoke
    Posts
    5
    Rep Power
    0

    Default Dont know how to use .lastIndexOf

    I've been doing Java for a couple of week and still trying to get to grips with it. I'm attempting to write a simple program that asks the user to input there full name and the output is the initials. The program i've written, outputs the first initial, then the number of the second and third initial. I know its because i've used an int in the method. I just do not know how to use the .lastIndexof using a char. I've been wrestling with this problem for 2 days and i'm pulling out my hair. Can anyone point my in the right direction.

    Java Code:
    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);
    Any help is appreciated

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Dont know how to use .lastIndexOf

    indexOf(...), and lastIndexOf(...) return ints, which represent the position the regex was found at. So it's running what number the char is located as, after attaining this number, you can use either subString(...), or charAt(...)

    Java Code:
    String hello = "Hello, Hello, Hello"
    System.out.println(hello.charAt(hello.indexOf("H")));
    System.out.println(hello.charAt(hello.lastIndexOf("l")));

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Dont know how to use .lastIndexOf

    I guess you didn't like the answers I already gave you?

    Crossposted: I'm a java beginnier - Page 2
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Out of Bounds. I dont know Why :L
    By socboy6579 in forum New To Java
    Replies: 10
    Last Post: 01-13-2011, 12:02 AM
  2. Hello! and I need help. I dont know were to start
    By Fall0ut in forum New To Java
    Replies: 10
    Last Post: 05-19-2010, 06:26 PM
  3. Using libraries that others dont have(j3d)
    By scorpion9 in forum Advanced Java
    Replies: 7
    Last Post: 02-19-2010, 11:24 AM
  4. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 01:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •