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 ??
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.
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 ??
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!
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)
@reddevilggg: Kindly usetags 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.
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!
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.
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!
Take a look at the API - it has all java SE classes, their methods and available variables.How do i find the functions provided by the String ??
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.
Cheers. i'll take a look
Thanks again
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.
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!
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?
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!
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.
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!
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:
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!
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.
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!
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);
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!
There is going to be 2 spaces as i have to assume that the user input 3 names, first, middle and last name.I don't think that lastIndexOf() is going to get you anything useful, since there's only going to be one space.
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.
I've tried the substring and this is the best i can do
The output isScanner 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));
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 ??Please enter your FULL name
John Paul Jones
J510
Paul J
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!
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
Thank you both for your help, and due to your help i've done it . Thanks Again