Reading Lines and Splitting Text

Reading a line
The file bakery.txt holds a day's sales. Each line is one item, with semicolons separating the item, the number sold, and the price of one.
rye;12;3.50
bagel;30;1.25
scone;8;2.75
The Scanner method nextLine() returns the rest of the current line as a String, spaces included. With sc connected to this file, the first call returns "rye;12;3.50" and the second returns "bagel;30;1.25". If the first line had read rye loaf;12;3.50, nextLine() would still return all of it, while next() would stop at the space and return "rye".
Splitting a line
The String method split cuts a string at every match of its argument and returns the pieces in an array.
String[] parts = "rye;12;3.50".split(";");
The array holds "rye", "12", and "3.50", so parts.length is 3. Two semicolons make three pieces, since each cut adds one. The semicolons themselves are gone.
Converting the pieces
Every element of parts is a String, including the ones that spell numbers. So parts[2] + 1 is "3.501", with the 1 joined to the end of the text. Arithmetic needs the text converted first.
int sold = Integer.parseInt(parts[1]);
double price = Double.parseDouble(parts[2]);
Now sold * price is 42.0. Both methods are called on their classes and never on a string. The method parseInt needs text that spells a whole number, and parseDouble accepts a whole number or a decimal. Integer.parseInt("3.50") compiles, but it throws a NumberFormatException when it runs.
The whole file
A while loop with hasNext() as its condition reads the file line by line.
Scanner sc = new Scanner(new File("bakery.txt"));
double sales = 0.0;
while (sc.hasNext())
{
String[] parts = sc.nextLine().split(";");
int sold = Integer.parseInt(parts[1]);
double price = Double.parseDouble(parts[2]);
sales += sold * price;
}
sc.close();
The three lines bring in 42.0, 37.5, and 22.0, so sales ends at 101.5. The first statement of the body calls split on the string nextLine() returns, without naming it first. Storing the line in a variable and splitting that does the same work.
The loop depends on the file's format. A field index of 1 or 2 is right only if every line puts its fields in that order. A blank line would split into an array with no element at index 1.