How can i possibly do this without knowing the size of the array beforehand? I'm totally confused. I've been trying to do this by splitting the array and what not but it's supposed to do this by using only loops. Here's different ways I've tried to do it but it doesn't stop taking user input even when it encounters a *.
1st version:
import java.util.*;
public class Number11 {
public static void main(String[] args) {
String line="";
Scanner input = new Scanner(System.in);
System.out.println("You are asked to enter a character one by one and terminate the list with *");
System.out.print("Please enter a character or end it with * : ");
line = input.next();
while(!(line.equals("*")))
{
System.out.print("Please enter a character or end it with * : ");
line = input.next();
if(line.equals("*"))
{
System.exit(0);
int length = line.length();
String [] sequence = new String[length-1];
for(int i=0; i<length; i++)
{
sequence[i] = line;
}
break;
}
}
}
}
2nd:
import java.util.*;
public class Number11 {
public static void main(String[] args) {
String [] character = new String[20];
Scanner input = new Scanner(System.in);
for(int i=0; i < character.length; i++)
{
System.out.print("Enter a character or press * to stop: ");
character[i] = input.next();
while(!(character[i].equals("*")))
{
System.out.print("Enter another character or press * to stop: ");
character[i] = input.next();
}
}
}
}
Your help will be much appreciated. Thanks.