| | |
String Manipulations
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Solved Threads: 0
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!
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!
It must been your lucky day today
I have nothing to do so I typed that code for you
I have nothing to do so I typed that code for you
Java Syntax (Toggle Plain Text)
import java.util.Scanner; class PrintMyName { public static void main(String [] args) { Scanner scan = new Scanner(System.in); // variable initation String input; String firstName = null; String middleName = null; String lastName = null; int emptySpace = 0; int counter = 0; int firstEmpty = 0; int secondEmpty = 0; System.out.println ("Enter a person's name: "); input=scan.nextLine(); input = input.trim(); // remove any empty characters before and after submited string System.out.println("\nName entered " + input + "\n"); for(int i = 0; i < input.length(); i++) { if(input.charAt(i)== ' ') { emptySpace++; if(emptySpace == 1) { firstEmpty = i; } else if(emptySpace == 2) { secondEmpty = i; } else { input = input.substring(0, i); // remove extra chcarcters after last name } } } if(emptySpace == 1) { firstName = input.substring(0, firstEmpty); lastName = input.substring(firstEmpty+1, input.length() ); System.out.println(lastName + ", " + firstName); System.out.println(reverse(lastName) + " " + reverse(firstName) ); System.out.println(reverse(firstName) + " " + reverse(lastName) ); } else { firstName = input.substring(0, firstEmpty); middleName = input.substring(firstEmpty+1, secondEmpty); lastName = input.substring(secondEmpty+1, input.length() ); System.out.println(lastName + ", " + firstName + " " + middleName.charAt(0) + "."); System.out.println(reverse(lastName) + " " + reverse(firstName) + " " + reverse(middleName) ); System.out.println(reverse(firstName) + " " + reverse(middleName) + " " + reverse(lastName) ); } } public static String reverse(String str) { StringBuilder reverseStr = new StringBuilder(str); reverseStr.reverse(); return reverseStr.toString(); } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Java's String Tokenizer (Java)
- problem seperating string into individual letters. (C++)
- C# : When exactly should a StringBuilder be used? (C#)
Other Threads in the Java Forum
- Previous Thread: Players taking turns
- Next Thread: Ftp
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner screen sell server set size sms sort sql string swing terminal threads tree web websites windows






