Splitting tokens

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1225 Views - Last Post: 11 July 2012 - 01:53 AM Rate Topic: -----

#1 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Splitting tokens

Posted 09 July 2012 - 12:08 PM

Hi, this is the problem I am trying to solve:
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

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting tokens

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Splitting tokens

Posted 09 July 2012 - 12:09 PM

You have to concatenate the String with the newline character.
addressSB.append(addressArray[1] + "\n");


Was This Post Helpful? 1
  • +
  • -

#3 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 12:23 PM

Wow thank you for the insanely fast reply! I am now getting an "error: class, interface, or enum expected" on most lines after the main method in the createOutputAddress section. I looked online and it says that error is usually caused by misplaced ";"'s in the previous lines but I do not see any out of place. Do you know what I need to do to fix these?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,613
  • Joined: 27-December 08

Re: Splitting tokens

Posted 09 July 2012 - 12:27 PM

Post your revised code and the error message exactly as it appears.
Was This Post Helpful? 1
  • +
  • -

#5 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 12:34 PM

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:42: class, interface, or enum expected
public static String createOutputAddress
^
Main.java:47: class, interface, or enum expected
StringBuilder addressSB = new StringBuilder();
^
Main.java:48: class, interface, or enum expected
String[] addressArray = new String[5];
^
Main.java:51: class, interface, or enum expected
for(int i = 0; i < numTokens; i++)
^
Main.java:51: class, interface, or enum expected
for(int i = 0; i < numTokens; i++)
^
Main.java:51: class, interface, or enum expected
for(int i = 0; i < numTokens; i++)
^
Main.java:54: class, interface, or enum expected
addressSB.append(addressArray[1]+ "\n");
^
Main.java:55: class, interface, or enum expected
addressSB.append(addressArray[2]+ "\n");
^
Main.java:56: class, interface, or enum expected
addressSB.append(addressArray[3]);
^
Main.java:57: class, interface, or enum expected
addressSB.append(',');
^
Main.java:58: class, interface, or enum expected
addressSB.append(' ');
^
Main.java:59: class, interface, or enum expected
addressSB.append(addressArray[4]);
^
Main.java:60: class, interface, or enum expected
addressSB.append( );
^
Main.java:61: class, interface, or enum expected
addressSB.append(addressArray[5]);
^
Main.java:63: class, interface, or enum expected
outputAddress = addressSB.toString();
^
Main.java:64: class, interface, or enum expected
return outputAddress;
^
Main.java:66: class, interface, or enum expected
} // end method
^
17 errors
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Splitting tokens

Posted 09 July 2012 - 12:42 PM

Line 33, you close your class with an extra brace (} ), move it to the end of your file.

You may want to take a look at this post, it will help you learn how to fix Java Errors
Was This Post Helpful? 1
  • +
  • -

#7 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 12:54 PM

Thank you. I am now getting four "error: no suitable method found for append()" so I will check out your guide and hopefully get it working. Thanks again!

Actually I just forgot two '. Now I have three cannot find symbol errors and I remember seeing those in your guide. Thanks again for that link.
Was This Post Helpful? 0
  • +
  • -

#8 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 01:12 PM

Ok so I used the guide to fix all of the errors but one.

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, outputAddress;
	   String [] addressArray = new String[5];

	   StringTokenizer addressTokenizer = null;
	   
	   //address
	   addressTokenizer = addressLabel;
  	   numTokens = addressTokenizer.countTokens(); //count tokens

	   //call createOutputAddress method
      	   outputAddress = createOutputAddress(addressTokenizer, numTokens);

	   //output
      	   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
}



ERROR
Main.java:21: incompatible types
found : java.lang.String
required: java.util.StringTokenizer
addressTokenizer = addressLabel;
^
1 error
Was This Post Helpful? 0
  • +
  • -

#9 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Splitting tokens

Posted 09 July 2012 - 01:58 PM

addressTokenizer is a StringTokenizer Object, and addressTokenizer is a String Object. Because they are different objects you cannot assign a String Object to be a StringTokenizer Object.

Perhaps this is what you were trying to do?
addressTokenizer  = new StringTokenizer(addressTokenizer );

Was This Post Helpful? 1
  • +
  • -

#10 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 02:18 PM

I am trying to make it so that addressTokenizer is the value that the user entered for addressLabel (the mailing address). Then the numTokens is supposed to take the addressTokenizer and count how many tokens there are so that it can use the layout in the createOutputAddress to look like this:

Joe Smith
123 N. Main
Baltimore, MD 21027
Was This Post Helpful? 0
  • +
  • -

#11 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 03:20 PM

Ok I got it to compile, but now after I enter the info I get an error. Any Idea how to fix this?

import java.util.StringTokenizer;
import javax.swing.JOptionPane;

public class MailingLabel
{
	   public static void main( String [] args )
	{
	   String addressLabel;
	   int numTokens = 0;

	   String addressMessage = "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, outputAddress;
	   String [] addressArray = new String[5];

	   StringTokenizer addressTokenizer = null;
	   
	   //address
	   addressTokenizer = getAddress(addressMessage, guiTitle);
  	   numTokens = addressTokenizer.countTokens(); //count tokens

	   //call createOutputAddress method
      	   outputAddress = createOutputAddress(addressTokenizer, numTokens);

	   //output
      	   outputMessage = "The Mailing Label is:" + "/n" 
                   	 	+ outputAddress;


      JOptionPane.showMessageDialog(null, outputMessage);

   } // end main method

  public static StringTokenizer getAddress(
       String addressMessage, String guiTitle)
   {
      // declare variables
      String inputAddress;
      StringTokenizer addressTokenizer = null;
      int numAddressTokens = 0;

      // get address
         inputAddress = JOptionPane.showInputDialog(addressMessage);
         inputAddress = inputAddress.trim();
         addressTokenizer = new StringTokenizer(inputAddress);
         numAddressTokens  = addressTokenizer.countTokens();

      return addressTokenizer;

   } // end 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
}



Error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at MailingLabel.createOutputAddress(MailingLabel.java:71)
at MailingLabel.main(MailingLabel.java:32)
Was This Post Helpful? 0
  • +
  • -

#12 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 07:29 PM

I am trying to figure out where to put "StringTokenizer (name, ",")" so that my program will use commas as the delimiters. Does anyone know where I should put it? Right now it compiles but when I enter in the data and hit enter it says:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at MailingLabel.createOutputAddress(MailingLabel.java :71)
at MailingLabel.main(MailingLabel.java:32)

Here is my code:

import java.util.StringTokenizer;
	import javax.swing.JOptionPane;
	 
	public class a
	{
	       public static void main( String [] args )
	    {
	       String addressLabel;
	       int numTokens = 0;
	 
	       String addressMessage = "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, outputAddress;
	       String [] addressArray = new String[5];
	 
	       StringTokenizer addressTokenizer = null;
	        
	       //address
	       addressTokenizer = getAddress(addressMessage, guiTitle);
	       numTokens = addressTokenizer.countTokens(); //count tokens
	 
	       //call createOutputAddress method
	           outputAddress = createOutputAddress(addressTokenizer, numTokens);
	 
	       //output
	           outputMessage = "The Mailing Label is:" + "/n"
	                        + outputAddress;
	 
	 
	      JOptionPane.showMessageDialog(null, outputMessage);
	 
	   } // end main method
	 
	  public static StringTokenizer getAddress(
	       String addressMessage, String guiTitle)
	   {
	      // declare variables
	      String inputAddress;
	      StringTokenizer addressTokenizer = null;
	      int numAddressTokens = 0;
	 
	      // get address
	         inputAddress = JOptionPane.showInputDialog(addressMessage);
	         inputAddress = inputAddress.trim();
	         addressTokenizer = new StringTokenizer(inputAddress);
	         numAddressTokens  = addressTokenizer.countTokens();
	 
	      return addressTokenizer;
	 
	   } // end 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
	}


Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,840
  • Joined: 05-April 11

Re: Splitting tokens

Posted 09 July 2012 - 07:36 PM

The StringTokenizer has a constructor that takes the input String and the delimiter.

addressTokenizer = new StringTokenizer(inputAddress, ",");



The StringTokenizer is a legacy class, and instead you should be using the split method of String.
Example:
String address = "this,12,is,a,test";
String[] tokens = address.split(",");


Was This Post Helpful? 1
  • +
  • -

#14 Yokomoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 18-June 12

Re: Splitting tokens

Posted 09 July 2012 - 07:55 PM

Where do I put the split at? I have a method that geths the address so I am unsure if the split code goes in the getAddress method or in the output. I tried putting it after inputAddress = inputAddress.trim(); in the getAddress and I still get the same error. Sorry if these are stupid questions, I am new to this and hvae been struggling for hours. Any help is appreciated.
Was This Post Helpful? 0
  • +
  • -

#15 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Splitting tokens

Posted 09 July 2012 - 08:29 PM

if you try and output System.out.println( addressArray[4] ); , you will see that the result is null. thats because addressArray doesnt have that many indexes fill in.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2