Results 1 to 5 of 5
- 10-04-2012, 11:10 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
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(',');
- 10-05-2012, 04:08 AM #2
Re: Last Name, First Name switch Program help.
Have you read the API doc for the String class's indexOf() method? That's a good place to start.professor wants us to use a indexof method
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.Last edited by Norm; 10-05-2012 at 04:10 AM.
If you don't understand my response, don't ignore it, ask a question.
- 10-05-2012, 05:21 AM #3
Re: Last Name, First Name switch Program help.
Please go through BB Code List - Java Programming Forum and edit your post accordingly.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-05-2012, 10:03 AM #4
Arma virumque cano
- Join Date
- Oct 2012
- Location
- Indianapolis
- Posts
- 20
- Rep Power
- 0
Re: Last Name, First Name switch Program help.
You are probably going to want to use a string.substring() function.
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.Java 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); } }
This can be done by adjusting the index.
Good luck
- 10-05-2012, 05:14 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Switch
By java4amanda in forum New To JavaReplies: 13Last Post: 03-21-2012, 09:53 AM -
program automatically chooses the default in switch inside loop
By jethph in forum New To JavaReplies: 4Last Post: 02-13-2011, 12:03 PM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks