shivani reddy
shivani reddy

Reputation: 11

how to assign the values to a string array directly taken from input using bufferedreader?

public static void main(String[] args)
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in);
    String[] a = new String[]{br.readLine().split("\\s")};
}

I'm getting errors here. Is there any direct way to convert the line into array of strings?

Upvotes: 0

Views: 94

Answers (1)

StaticBeagle
StaticBeagle

Reputation: 5175

split returns an array of strings so you can simply initialize a as:

String[] a = br.readLine().split("\\s");

String[] myArray = new String[] {...} is what is known as an array literal, and the values need to be known before the code runs.

Upvotes: 1

Related Questions