String Manipulations

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 13
Reputation: alexasmith is an unknown quantity at this point 
Solved Threads: 0
alexasmith alexasmith is offline Offline
Newbie Poster

String Manipulations

 
0
  #1
Oct 7th, 2007
I am in a beginners java class and I have an assignment that is really difficult (for me). the assignment is to prompt the user to enter a person's full name in the order: first middle last.

Output will include:
the prompt
the original name
the name in the form : last, first MI
the entire name backwards
the name in FML order with each part backwards

The output should use only one blank to separate the parts regardless of the
number of blanks originally entered in the name.

Example:
Enter a person's name: John Paul Jones
John Paul Jones
Jones, John P.
senoJ luaP nhoJ
nhoJ luaP senoJ

Enter a person's name: John Smith
John Smith
Smith, John
htimS nhoJ

I don't know how to get this information to print out, I know i am most likely not using the correct methods etc. please help if you can !! Here is what I have...

public static void main(String [] args)
{

Scanner scan = new Scanner(System.in);


// variable initation
String input;
String firstName;
String middleName = "";
String lastName;


int lengthofInput;
int counter = 0;

System.out.println ("Enter a person's name: ");
input=scan.nextLine();

counter = 0;
while(input.charAt(counter)!=' ')
counter++;

//First Name:

firstName = input.substring(0,counter);

input = input.substring(counter, input.length());
input = input.trim();

//Reset counter to 0 at the beginning of the next line

counter = 0;
while(input.charAt(counter)!=' ')
counter++;

input = input.substring(counter, input.length());
input = input.trim();


//Middle Name:

if(input.lengthOf(' ')
{
counter = 0
while(input.charAt(counter)!=' ')
counter++;

middleName = input.substring(0,counter);


input = input.substring(counter, input.length());
input = input.trim();

counter = 0;

input = input.substring(counter, input.length());
input = input.trim();
}


//Last name:

counter = 0
while(input.charAt(counter)!=' ')
counter++;

lastName = input.substring(0,counter0;

input = input.substring(counter, input.length());
input = input.trim();



new StringBuffer(username).reverse() + "</tt>";//an idea i have don't know if it works


System.out.println (firstName+" "+middleName+" "+lastName);
System.out.println (lastName+","+firstName+" "+middleNameInt" .");
System.out.println ();
System.out.println ();

Thanks Alexa!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,192
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: String Manipulations

 
0
  #2
Oct 8th, 2007
It must been your lucky day today
I have nothing to do so I typed that code for you
  1. import java.util.Scanner;
  2.  
  3. class PrintMyName
  4. {
  5. public static void main(String [] args)
  6. {
  7.  
  8. Scanner scan = new Scanner(System.in);
  9.  
  10.  
  11. // variable initation
  12. String input;
  13. String firstName = null;
  14. String middleName = null;
  15. String lastName = null;
  16.  
  17. int emptySpace = 0;
  18. int counter = 0;
  19. int firstEmpty = 0;
  20. int secondEmpty = 0;
  21.  
  22. System.out.println ("Enter a person's name: ");
  23. input=scan.nextLine();
  24. input = input.trim(); // remove any empty characters before and after submited string
  25.  
  26. System.out.println("\nName entered " + input + "\n");
  27.  
  28. for(int i = 0; i < input.length(); i++)
  29. {
  30. if(input.charAt(i)== ' ')
  31. {
  32. emptySpace++;
  33. if(emptySpace == 1)
  34. {
  35. firstEmpty = i;
  36. }
  37. else if(emptySpace == 2)
  38. {
  39. secondEmpty = i;
  40. }
  41. else
  42. {
  43. input = input.substring(0, i); // remove extra chcarcters after last name
  44. }
  45. }
  46. }
  47.  
  48. if(emptySpace == 1)
  49. {
  50. firstName = input.substring(0, firstEmpty);
  51. lastName = input.substring(firstEmpty+1, input.length() );
  52. System.out.println(lastName + ", " + firstName);
  53. System.out.println(reverse(lastName) + " " + reverse(firstName) );
  54. System.out.println(reverse(firstName) + " " + reverse(lastName) );
  55. }
  56. else
  57. {
  58. firstName = input.substring(0, firstEmpty);
  59. middleName = input.substring(firstEmpty+1, secondEmpty);
  60. lastName = input.substring(secondEmpty+1, input.length() );
  61. System.out.println(lastName + ", " + firstName + " " + middleName.charAt(0) + ".");
  62. System.out.println(reverse(lastName) + " " + reverse(firstName) + " " + reverse(middleName) );
  63. System.out.println(reverse(firstName) + " " + reverse(middleName) + " " + reverse(lastName) );
  64. }
  65. }
  66.  
  67. public static String reverse(String str)
  68. {
  69. StringBuilder reverseStr = new StringBuilder(str);
  70. reverseStr.reverse();
  71. return reverseStr.toString();
  72. }
  73. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC