Results 1 to 4 of 4
Thread: Help .....PLEASE!
- 03-25-2011, 03:26 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Help .....PLEASE!
code:
Actually I want to print M.K.Gandhi but it is printing .Gandhi can you point out the problem.public class T_alu_pain
{
public static void main()
{String s="Mohan Karamchand Gandhi";
s=" "+s;
int l=s.length();
int i,p=0;
char c;
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c==' ')
{
p=i;
}
}
for(i=0;i<=l;i++)
{
c=s.charAt(i);
if(c==' ')
{
System.out.print(c+".");
}
for(i=p+i;i<l;i++)
{
System.out.print(s.charAt(i));
}
}
}
}
- 03-25-2011, 03:36 PM #2
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
please use the [ code ] , [ / code ] tags when posting code.
It will make my job easier, and everyone else who is trying to help you.
first off the declaration of the main method is wrong, it should be:
basically if you want it to print M.K.Gandhi, you would have to do something like this:Java Code:public static void main(String...args) or public static void main(String[] args)
Ofcourse this is all based on fact that you only want to do this for just this String, if you want to do it for any name you would have to do some additional work.Java Code:String name = "Mohan Karamchand Gandhi"; StringBuffer newName = new StringBuffer(); since you already know that first letter is M, you would do: newName.append("M."); than you can do a search newName.append(name.charAt(name.indexOf("K"))); and then finally: newName.append(".Gandhi");Last edited by f1gh; 03-25-2011 at 03:40 PM.
- 03-25-2011, 03:37 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Your Output is " . Gandhi" !
-->
if(c==' ')
{
System.out.print(c+".");
}
translate this into words --> if c is a blank character, then it will print c (which is a blank character!!!) plus the point ! But you want print the next character then or? :)
-->
if (c == ' ') {
System.out.print(s.charAt(i+1) + ".");
(in one line: System.out.println(s.split(" ")[0].charAt(0)+"."+s.split(" ")[1].charAt(0)+" "+s.split(" ")[2]);
D - its bad, extract the split to one extra call :D )
(or use Pattern and Matcher :D )Last edited by eRaaaa; 03-25-2011 at 03:44 PM.
- 03-25-2011, 04:12 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
There are numerous ways to tackle the problem:
You could split the original string and than check if the name is 2 words or 3 words. If it's 2 words than you just take the first character of the first+"." and take the entire second word and concatenate them.
If it's 3 words in array after split, than you can just take first letter of 1stWord+"." and first word of second word 2ndWord+"." and than join the entire third word.
Java Code:String name = "King William Charles"; String[] splitName = name.split(" "); String newName = ""; if(splitName.length == 2) { newName += splitName[0].charAt(0)+"."+splitName[1]; } if(splitName.length == 3) { newName += splitName[0].charAt(0)+"."+splitName[1].charAt(0)+"."+splitName[2]; } System.out.println(newName);Last edited by f1gh; 03-25-2011 at 04:13 PM. Reason: edit: didn't realize eRaaaa had already given same answer


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks