Last Name, First Name switch Program help.
Hello all, I am currently taking my first basic programming course and have an assignment I'm having trouble with.
Problem: Have user input a name in the format ( " Last Name, First Name "), and display the New name as (" First Name, Last Name " ) With the first letters of both names being capitalized, and the rest of the letters being lowercase.
My professor wants us to use a indexof method to find the ',' in the inputted name to separate the two names.
Does anyone know how to do this? It sounds simple enough but I am completely new to this Java stuff and don't get it.
This is how far I have gotten with the help of a classmate.
import javax.swing.JOptionPane;
public class NameTags
{
public static void main(String[] args)
{
String name;
String Str1;
String Str2;
int index;
Str1=JOptionPane.showInputDialog(" Last Name,First Name: ");
name= Str1;
name.length();
index= Str1.indexOf(',');
Re: Last Name, First Name switch Program help.
Quote:
professor wants us to use a indexof method
Have you read the API doc for the String class's indexOf() method? That's a good place to start.
You need to add the ending }s for the main method and class and then compile and execute the code.
Add a call to the System.out.println(<put vars here>) to print out the values of the variables so you can see what the code has done when it was executed.
Re: Last Name, First Name switch Program help.
Please go through BB Code List - Java Programming Forum and edit your post accordingly.
db
Re: Last Name, First Name switch Program help.
You are probably going to want to use a string.substring() function.
Code:
import javax.swing.JOptionPane;
public class NameTags
{
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("Last Name, First Name: ");
//provided the user uses a comma, name will now equal something like: "Smith, Joe"
//To find the comma:
int index = name.indexOf(",");
//You will need 2 substrings, and since this is homework, I will leave you to figure out how to finish it:
//String strLastName = name.substring(0, index of the comma);
//String strFirstName = name.substring(index of the comma, name.length());
//Display the results
//exit the program
System.exit(0);
}
}
In order to get this right, you want to omit the comma that the user entered, otherwise it will still be there when you display your results.
This can be done by adjusting the index.
Good luck
Re: Last Name, First Name switch Program help.
Wow thank you guys so much I really appreciate it. I will try to press on and let you know how it goes!