Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Yokomoko

Thread: Incompatible types

  1. #1
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Incompatible types

    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)

    Java Code:
    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
    }
    I am getting an incompatible types error and I cannot figure out how to fix it. If you could help me I would greatly appreciate it!

    ERROR
    Main.java:21: incompatible types
    found : java.lang.String
    required: java.util.StringTokenizer
    addressTokenizer = addressLabel;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Incompatible types

    The error is telling you exactly what's wrong. You're trying to have a StringTokenizer variable, addressTokenizer, be assigned a String object, addressLabel, and that's not how StringTokenizers work. Please check out a StringTokenizer tutorial as it will explain how to use these critters.

  3. #3
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Incompatible types

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

    Java Code:
    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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Incompatible types

    When you get an error like that, the first thing to do is to concentrate on the region of code that is causing the error, which for you is:

    Java Code:
          for(int i = 0; i < numTokens; i++)
             addressArray[i] = addressTokenizer.nextToken(); // **** this line causes the error
    Then put in some debug statements to see what is happening that you don't expect. Something like:

    Java Code:
          for (int i = 0; i < numTokens; i++) { // always use curly braces {} even for one line blocks.
             String nextToken = addressTokenizer.nextToken(); // get token
             System.out.println(nextToken);  // print token to debug
             addressArray[i] = nextToken;  // use token
          }
    Then you'll see that you're splitting your tokens wrong. I'll leave it to you to experiment to see how to fix this.

  5. #5
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Incompatible types

    Ok thank you for pointing me in the right direction!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Incompatible types

    You're welcome. Please let us know how it goes!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Incompatible types

    As a "helpful hint" please note that StringTokenizer has several constructors, and one of them should help you solve this problem. Please check the StringTokenizer API to learn more about this.

  8. #8
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Incompatible types

    That link is like german to me right now haha. I'm thinking that it does not know where to split the tokens. For example at the ,'s

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Incompatible types

    Yes that's right, because it's using a default delimiter (the thing it splits on). What String do you want it to split on? One of the StringTokenizer constructors allows you to set the delimiter.

    You'd better start trying to understand "German". Like any other skill, reading the Java API gets better with practice, so practice, practice, practice.

  10. #10
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Incompatible types

    I want to split it on the commas. I am looking through the constructors now. It's funny how one line of code can throw you off for hours!
    Fubarable likes this.

  11. #11
    Yokomoko is offline Member
    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0

    Default Re: Incompatible types

    Ok I am trying to use str.split(","); but now I am trying to find where to put it. After I have put String[] addressArray = new String[5]; can I just do addressArray = str.split(","); or do I need to put String [] again?

Similar Threads

  1. incompatible types
    By angelicatomnob in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 04:25 PM
  2. incompatible types!!
    By sneha343 in forum New To Java
    Replies: 7
    Last Post: 12-01-2011, 12:49 AM
  3. incompatible types
    By effa in forum New To Java
    Replies: 16
    Last Post: 02-03-2011, 10:50 AM
  4. Incompatible types
    By bayan in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 09:43 AM
  5. Incompatible types
    By coltragon in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •