As I understood, you have combined information in your file "books.txt" (name -
string and price - double). To split information, that was read from your file you should use patterns (in example there is pattern to delimit using comma and different number of whitespaces " *" ). You also can use control symbols, such as "\n".
//we have cycle until we'll reach the symbol,
//what hasn't next element
while(src.hasNext()) {
//if next value is double
//(it is just checking next value, not goes to it)
if(src.hasNextDouble()) {
//we take this value and add to sum
sum += src.nextDouble();
}
//if the next value is not double
//in your case you should pass through this block
else {
//we take this element as string
String str = src.next();
//if this string equals("done"), we are
//coercive breaking the cycle
if(str.equals("done")) break;
else {
//if this is not "done" we have troubles :)
System.out.println("File format error.");
return;
}
}
}
Did I answered on your question?