espen thorsen
espen thorsen

Reputation: 1

incompatible types error String[]

I get this error message when i try to run my program

error: incompatible types
        epost = split[3];
                     ^
required: String[]
found:    String

here is my code:

String [] split = ordre.split(" ");
String [] epostadr;
while(split >= 3) {
    String [] epostadr = split[3]; 
}   

I want to save the epostadr in split[3] but it wont let me do that because split only saves Strings while epostadr is a String [], what can i do to change this? Any help would be greatly appreciated.

Upvotes: 0

Views: 2231

Answers (4)

mrfreester
mrfreester

Reputation: 1991

It's hard to know exactly what you're trying to do from this code so I'll go through and let you know what's happeneing. It looks like you're trying to take a string stored in the variable ordre, and split it so that each word has it's own index in a string array called split.

So if ordre contained the string "My name is Jones."

String [] split = ordre.split(" ");

That line would create an array named split containing the following values {My, name, is, Jones}

Here is the part that maybe you can clarify, it looks like you want those values to be in the string array epostadr, or maybe just the 3rd index which in this case would be "Jones" since indexes start with 0.

Putting the values in epostadr would be redundant since split already contains those values. But if you really wanted to copy it you could do this.

String [] epostadre = split;

If you wanted just the 3rd index, epostadre can't be a string array, but must be declared as a string and you would do this...

String epostadre = split[3];

Here you're declaring a String, which will hold one value, and setting it equal to the string that is contained in the 3rd index of split, which is Jones. split[0] = "My" split[1] = "name" and so on.

I hope that helps, let me know if you need more clarification.

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76888

First off, you don't have an array:

String [] epostadr;

This declares a variable than can have an array reference assigned to it.

Then you have:

String [] epostadr = split[3]; 

This makes no sense. split[3] is a String; you can't assign that to a variable declared as a String array.

If you need epostadr to be an array, you need to create one, assign it, then put the String in a specific location:

String [] epostadr = new String[maxNumberOfStrings];
...
epostadr[index] = split[3];

Edit: this is ignoring that the rest of your code doesn't actually do what you think it does. Your while loop (if it were written correctly) will loop forever; split.length is never going to change. Given these issues you may well want to invest in a beginner's guide to Java/programming, or at the very least go through the Java tutorials available on Oracle's website.

Upvotes: 2

aug
aug

Reputation: 11714

When you use split on a String, it makes it into a String[] so you got that right when making split as a String[]. However, in each array slot, there is a String. You are basically trying to making epostadr, which you declared as a String[], a String and that's where the incompatible types come from. A String[] can't be a String.

Upvotes: 0

amit
amit

Reputation: 178411

String [] epostadr = split[3]; 

split[3] is of type String while epostadr is of type String[]

Maybe you want to declare epostadr as String? [not sure I am following what you are trying to achieve]

Upvotes: 4

Related Questions