import java.io.*;
import java.util.Scanner;
public class SalesOfCoffee{
// the next three variables are related to the scanner.
private static final String FILE_NAME = "coffee.csv";
private static Scanner myScanner;
// indicate if the scanner is valid or not.
private static boolean validScanner = false;
// so I can parse the input.
private static String line;
public static void main(String[] args){
int totalLines = 0;
String firstOfLine;
MakeScanner();
if (!validScanner) {
System.out.printf("Scanner not constructed\n");
return;
}
// skip the titles.
myScanner.nextLine();
while(myScanner.hasNextLine()) {
line = myScanner.nextLine();
totalLines++;
System.out.printf("Got '%s'\n", line);
int i= 0;
while (i < 8) {
firstOfLine = SplitLine();
System.out.printf("Back in main, firstOfLine is '%s'\n", firstOfLine);
++i;
}
System.out.printf("The rest of the line is is '%s'\n\n\n", line);
}
System.out.printf("The file contains %d lines.\n",totalLines);
myScanner.close();
}
private static void MakeScanner() {
try {
myScanner = new Scanner(new File(FILE_NAME));
validScanner = true;
} catch (FileNotFoundException e) {
System.out.printf("Cound not open %s", FILE_NAME);
}
}
private static String SplitLine() {
String firstPart = "";
int position = 0;
position = line.indexOf(',');
//System.out.printf("fond a , at %d\n", position);
if (position != -1) {
firstPart = line.substring(0, position);
//System.out.printf("The frist part is '%s'\n", firstPart);
line = line.substring(position+1);
//System.out.printf("The line is now '%s'\n",line);
}
return firstPart;
}
}