Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: need help with validating results

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with validating results

    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

Similar Threads

  1. Help with radio buttons and results.
    By Bewitched1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 3rd, 2011, 04:01 PM
  2. How to Accumulate results
    By DreamNaut in forum Java Theory & Questions
    Replies: 2
    Last Post: October 29th, 2010, 01:10 AM
  3. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 03:37 PM
  4. Football Results
    By RSYR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2009, 07:24 PM
  5. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM