Hi,
I have a txt file where ive split up the text using a delimiter and printed the results. I been trying to make it so the java will read the file and where a delimiter is missing it wil output the result separtely as invalid. any help/pointers in the right direction greatly appreciated. heres my code:
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class Generator {
private File file;
public static void main(String args[]) throws FileNotFoundException {
Generator String = new Generator("results.txt"); //line to input the text file
String.processLineByLine(); //line by line reading from the text
}
public Generator(String txt) {
file = new File(txt); //open a new text file for scan after
}
public void processLineByLine() throws FileNotFoundException {
Scanner scan1 = new Scanner(file);
try { //
//Scanner to get each line
while ( scan1.hasNextLine() ){ //continue to the next line
processLine( scan1.nextLine() );
}
}
finally {
scan1.close(); //no next line = scanner close
}
}
public void processLine(String aLine){
//use a second Scanner to string the content of each line
Scanner scanner = new Scanner(aLine);
scanner.useDelimiter(",");
while ( scanner.hasNext() ){
String event = scanner.next(); // name the columns
String co1 = scanner.next();
String co2 = scanner.next();
String co3 = scanner.next();
String competitor1 = scanner.next();
String competitor2 = scanner.next();
String competitor3 = scanner.next();
System.out.println( event + ": (Gold) " + competitor1 + co1 + ", (Sliver) " + competitor2 + co2 + ", (Bronze) " + competitor3 + co3);
} //print out the result
scanner.close(); // close the scanner
}