need help with validating results
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
}
Re: need help with validating results
Quote:
read the file and where a delimiter is missing it wil output the result separtely as invalid
The String class has methods for looking at the contents of a String. You could use one of them to detect if a line has the delimiter and output a message if the delimiter is missing.
What do you mean by "output the result separately"? Does that mean you want to save the lines with errors and print them out AFTER you print out something else? The ArrayList class would be useful to save the contents of the lines to be printed later.
Please Edit your post and wrap your code with
[code=java]<YOUR CODE HERE>[/code] to get highlighting