Create a program called MailingLabel.java The program should ask the user to enter a name and address in an input dialog box with each component of the address seperated by a comma. For example, "Joe Smith, 123 N. Main Baltimore, MD,21027" The program should then use the comma as a delimiter and create a single string that displays each token on a seperate line, except that the city/state/zip should appear on the same line. Create a formatting string to hold all formatting information. Use one application of the String.format method to format the mailing label. Output should be directed to a GUI window using the JoptionPane class. (Hint: Use the trim method of the string class to remove leading and trailing spaces from tokens)
Right now I have written the program but based on the errors, I seem to be having trouble getting the output to be on separate lines like this:
Joe Smith
123 N. Main
Baltimore, MD 21027
I have been trying to use \n's so far but if you can help me get it working I would greatly appreciate it!
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class MailingLabel
{
public static void main( String [] args )
{
String addressLabel;
int numTokens = 0;
addressLabel = JOptionPane.showInputDialog( "Enter The mailing address separated by commas\n"
+ " in the following order:\n"
+ " (First and last name, Address, City, State, ZIP Code)");
String guiTitle = "Mailing Label";
String outputMessage;
String [] addressArray = new String[5];
addressTokenizer = addressLabel;
StringTokenizer addressTokenizer = null;
numTokens = addressTokenizer.countTokens();
outputAddress = createOutputAddress(addressTokenizer, numTokens);
outputMessage = "The Mailing Label is: /n"
+ outputAddress;
JOptionPane.showMessageDialog(null, outputMessage);
} // end main method
public static String createOutputAddress
(StringTokenizer addressTokenizer, int numTokens)
{
// declare variables
String outputAddress;
StringBuilder addressSB = new StringBuilder();
String[] addressArray = new String[5];
// get address tokens and build output
for(int i = 0; i < numTokens; i++)
addressArray[i] = addressTokenizer.nextToken();
addressSB.append(addressArray[1]\n);
addressSB.append(addressArray[2]\n);
addressSB.append(addressArray[3]);
addressSB.append(',');
addressSB.append(' ');
addressSB.append(addressArray[4]);
addressSB.append( );
addressSB.append(addressArray[5]);
outputAddress = addressSB.toString();
return outputAddress;
} // end method
ERRORS
Main.java:47: illegal character: \92
addressSB.append(addressArray[1]\n);
^
Main.java:47: not a statement
addressSB.append(addressArray[1]\n);
^
Main.java:47: ';' expected
addressSB.append(addressArray[1]\n);
^
Main.java:48: illegal character: \92
addressSB.append(addressArray[2]\n);
^
Main.java:48: not a statement
addressSB.append(addressArray[2]\n);
^
Main.java:48: ';' expected
addressSB.append(addressArray[2]\n);
^
Main.java:59: reached end of file while parsing
} // end method
^
7 errors

New Topic/Question
Reply




MultiQuote






|