Hello people.
My aim is to assing an array with words read from system.in in a single entry.
So if the users enters "Hello there"
i will have
0,Hello
1,Therre
I have managed to achieve this with the following piece of code.
import java.util.Scanner;
public class mein
{
public static void main(String[] args)
{
String[] searchArray = {"yes","no"};
System.out.println("Enter Key");
Scanner getKey = new Scanner(System.in);
while (getKey.hasNext())
{
for (int i=0; i<2; i++)
{
searchArray[i] = getKey.next();
System.out.println(searchArray[i]);
}
}
}
}
All fine till here.
What if i want to extract 3 words ? even worse what if i want to extract unknown number of words ?
I Tried the following code as well but faced errors.
import java.util.Scanner;
public class mein
{
public static void main(String[] args)
{
String[] searchArray = null;
System.out.println("Enter Key");
Scanner getKey = new Scanner(System.in);
while (getKey.hasNext())
{
for (int i=0; i<searchArray.length; i++)
{
searchArray[i] = getKey.next();
System.out.println(searchArray[i]);
}
}
}
}
Of course since the array is null there'is no length!
What could i do ?
Any ideas ?