0

My data is Like this:

Smith, Bob; Data; More Data
Doe, John; Data; More Data

If you look below you'll see that I'm trying to split the FullName into first and last. The error is:

"The method split(String) in the type String is not applicable for the arguments (char)"

String line = scanner.nextLine();
String[] data = line.split(";");
String[] fullName = data[0].split(',');
Henry
  • 149
  • 1
  • 3
  • 12

3 Answers3

9

I explained in my comment that the error could be fixed quite easily: ','",".

I'll just give some details as to why this is the case. In Java, single characters enclosed in 's are character literals, which are very different from string literals, enclosed in "s. If you're coming from the world of Python, for example, this might take some time to get used to since there ' and " can be used essentially synonymously.

In any case, the 1-argument split() method of String accepts only a String which is in turn parsed as a regular expression. That's why you need the double quotes.

Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
1

Switch the single quotes to double

String[] fullName = data[0].split(",");

cmd
  • 11,622
  • 7
  • 51
  • 61
0

String.split(String) takes a String as the lone parameter.

public String[] split(String regex)

    Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument split method with the
    given expression and a limit argument of zero. Trailing empty strings are
    therefore not included in the resulting array.

    The string "boo:and:foo", for example, yields the following results with
    these expressions:

        Regex   Result
        :   { "boo", "and", "foo" }
        o   { "b", "", ":and:f" }

    Parameters:
        regex - the delimiting regular expression 
    Returns:
        the array of strings computed by splitting this string around matches
        of the given regular expression 
    Throws:
        PatternSyntaxException - if the regular expression's syntax is invalid
    Since:
        1.4
    See Also:
        Pattern
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132