CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011

    Invalid and Valid Data

    Hello all this is my first post so go easy on me :P ive been browsing these forums for a while and have finally decided to register and hoepfully contribute. Im having trouble getting my programme to reconise and sort invalid data. Any help would be great

    My spec is to:

    Is there a line in the file, if yes add 1 to count.
    Read the line in, split it into column s (7)
    If valid then output the result and increment a valid count.
    If invalid then break and display invalid count.

    //imports the scanner
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class splitter
    int count = 0;
    int valid = 0;

    public static void main(String[] args) throws FileNotFoundException {

    Scanner s = new Scanner(new File("results.txt")); // create a scanner which scans from a file

    String line = ""; // stores the each line of text read from the file

    while ( s.hasNext() == true ) {

    line = s.nextLine(); // read the next line of text from the file

    String [] splitupText = line.split(","); // split the text into multiple elements

    if (splitupText.length ==7)

    for ( int i = 0; i < splitupText.length; i=i+1 ) { // loop over each element

    splitupText [i] = splitupText [i].trim ();

    if (splitupText [i].compareTo ("")==0);

    String nextBit = splitupText[i]; // get the next element (indexed by i)

    nextBit = nextBit.trim(); // use the trim method to remove leading and trailing spaces


    //displaying data
    System.out.println (splitupText [0] + " : " + "Gold " + "(" + splitupText [4] + ")" + ", " + "Silver "+ "(" + splitupText [5] + ")" + ", " + "Bronze " + "(" + splitupText[6] + ")") ;


    Ive got to adding the invalid data into the text file and have some a bit unstuck, the programme reads and outputs valid data correctly but so far not luck with the invalid, if anyone could help that would be greatly appriciated, Thanks.

  2. #2
    Join Date
    May 2006

    Re: Invalid and Valid Data

    Sorry it's taken a while to answer your question - I appear to have overlooked it.

    Please use code tags when posting code.

    the programme reads and outputs valid data correctly but so far not luck with the invalid,
    You haven't said what constitutes valid data so I can only give a generalised answer.

    Anything that isn't valid (ie isn't output) must by definition be invalid so where ever you have a test for validity add an else clause that will be executed if the data is invalid.

    If you test the data in more than one place then you should to put the invalid data code in a separate method so you can call the same code multiple times rather than writing it out in several places.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)