944,051 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 6828
  • Java RSS
Oct 7th, 2007
0

String Manipulations

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alexasmith is offline Offline
13 posts
since Oct 2007
Oct 8th, 2007
0

Re: String Manipulations

It must been your lucky day today
I have nothing to do so I typed that code for you
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Players taking turns
Next Thread in Java Forum Timeline: Ftp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC