So what I’m trying to do is to fill an array of 100 items with 30 names from a file using Scanner = new Scanner(new File("list.txt")). It needs to use a sentinel of "DONE" to end the loop found at the bottom of the file.
How would I do that? array[arraySize] = value(); gives me a type mismatch
public class List
{
public static void main(String[] args) throws FileNotFoundException
{
double array[] = new double[100];
int arraySize = 0;
String value;
String sentinel = "DONE";
Scanner inFile = new Scanner(new File("list.txt"));
value = inFile.next();
while (value != sentinel)
{
array[arraySize] = value();
arraySize++;
value = inFile.next();
}
}
}
D’oh….those mistakes are shameful lol. Thanks all got it to work =]
A few issues, you need to change these lines from:
To:
Note: Additionally, as good practice, you might want to add some defensive programming checks to augment your
whileloop terminating condition. (Consider what happens when the input file does not contain a sentinel)