-2

Why do I get mssg with "cannot find symbol", when I use .split on the String Object?

public static void main(String[] args) {


String[] inputFile = StdIn.readAllLines().split("\\n");

//create Congress caracteristics
int states = Integer.parseInt(inputFile[0]);        // read states value and save in the first 
//position in the array

int totalSeats = Integer.parseInt(inputFile[1]);   //read number of seats and save in the second
//position in the array
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
Na Dia
  • 195
  • 1
  • 4
  • 11
  • 1
    `split` is a method of String not String[](the return type of readAllLines)..please read the docs. – Rahul Shah Mar 05 '15 at 22:14
  • Minor note: `StdIn` is a class created by Princeton for their Intro to Computer Science course: http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdIn.html – rayryeng Mar 17 '15 at 19:39

4 Answers4

3

This is because you are trying to call the split method on an array. There is no such method on an array. readAllLines() already returns an array of the strings. Just remove the .split() call.

From the JavaDoc:

static String[] readAllLines()

Reads all remaining lines from standard input and returns them as an array of strings.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
0

StdIn.readAllLines() does not return a String but a String[] (array of strings). You can't use split() on an array of strings.

Actually, you don't need to do that, because calling readAllLines() already gives you the array you are looking for.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
0

You are most likely calling #split() in an Array/Collection of Strings, not a String. You should iterate though them and split one after the other.

Pau Carre
  • 471
  • 2
  • 5
0
   We have fileName Object we itrate it a split it simple  in this program i split my object with (.) so thats why i used this =split("\\.");



    String[] fileName="we have a object here to we want to splite it ";


    for(String obj:FileName)
            {
        String[] part1=obj.split("\\.");
            }
    System.out.println(part1);



    check it its working fine