-
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)
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;
-
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.
-
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?
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)
-
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:
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:
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.
-
Re: Incompatible types
Ok thank you for pointing me in the right direction!
-
Re: Incompatible types
You're welcome. Please let us know how it goes!
-
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.
-
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
-
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.
-
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!
-
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?