Results 1 to 3 of 3
- 09-28-2011, 02:57 PM #1
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.
Any help is appreciatedJava 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);
- 09-28-2011, 04:15 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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")));
- 09-28-2011, 04:23 PM #3
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 2How 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
-
Out of Bounds. I dont know Why :L
By socboy6579 in forum New To JavaReplies: 10Last Post: 01-13-2011, 12:02 AM -
Hello! and I need help. I dont know were to start
By Fall0ut in forum New To JavaReplies: 10Last Post: 05-19-2010, 06:26 PM -
Using libraries that others dont have(j3d)
By scorpion9 in forum Advanced JavaReplies: 7Last Post: 02-19-2010, 11:24 AM -
Sometimes get the right results sometimes dont
By Battlefeldt in forum New To JavaReplies: 0Last Post: 12-18-2009, 01:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks