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 4 of 4

Thread: Reading Race Results from a text file in Java

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading Race Results from a text file in Java

    For my first phase, I'm trying to read race results from a text file. If every line had the exact same format, there wouldn't be a problem. The part that makes this so difficult is the school names. Here's an example of a line in the text file.

    1 262 Jackson Bertoli (12) Terre Haute South 15:29.1 4:59

    Each school may have 1,2, or 3 words in its name. So I tried to look for an integer (the time) after the school name and just stop the search for words in the school name there. But I'm having problems taking my value from the reader.next(); and plugging it into the part of the code that evaluates a string from an integer because one uses void and the other uses static. Plus it's having trouble recognizing my variables schoolnext and schoolnext2 throughout the whole code.

    It's pretty lengthy, but this is my first big boy program and I would GREATLY appreciate some help with what I have, or different methods I should try. Thanks in advance for all the time you spent looking through it!



    /* Original program done by Donald Bough started 11/01/13
     * Cross Country results experimenter!
     * Put your results from indianarunner.com in a .txt file, plug them into the program, and 
     * you should "eventually be able to" modify your own time or others in the text file
     * to see how it would've affected team places and points.
     * 
     * As of 11/04/13 I'm stuck figuring out how to tell, when scanning the text file, how many words make
     * up the school name, then printing off the rest of the results according to that number. This is the
     * beginning phase where I'm just trying to re-print off what the results already have basically.
     * Once I figure this out, I plan on taking those variables and actually doing the math with it.
     */
     
     
     
    import java.lang.*;
    import java.io.*;
    import java.util.*;
     
    public class myreadfile {
    	//Setting up my variables
    	int setschool = 0;
    	int setschool2 = 0;
    	String schoolnext;
    	String schoolnext2;
    	private Scanner reader;
     
     
    	//Opening my text file with the race results. This part works 100%
    	public void openfile(){
    		try{
    			reader = new Scanner(new File("C:\\Donalds\\Java\\Partsemistateresults.txt"));
    		}
    		catch (Exception e){
    			System.out.println("This file does not exist");
    		}
    	}
     
     
     
    	// Taking care of the first line of words we don't want to deal with. This also works 100%
    		public void firstline(){
     
    				String a = reader.next();
    				String b = reader.next();
    				String c = reader.next();
    				String d = reader.next();
    				String e = reader.next();
    				String f = reader.next();
    				String g = reader.next();
     
     
    			System.out.printf("%s %s %s %s %s %s %s" , a , b, c , d ,e , f, g);
    		}
     
     
    	//tells if a string is an integer. Everything from now on gets uncertain and sticky.
     
    		public static boolean isInteger(String schoolnext) {
    	    try { 
    	        Integer.parseInt(schoolnext); 
    	    } catch(NumberFormatException e) { 
    	        // This isn't an integer, so the number 2 is telling us we have a string
    	    	//I want to move onto the next word since it's another name in the 
    	    	//school. Now go back down to the readfile method.
    	    	int setschool=+2;
    	    	return false; 
    	    }
    	    //Is an integer. We now know schoolnext took the value of an integer. This 
    	    //integer would be the time part of the race results. Signal to move past 
    	    //the attempt in finding school names. Now go back down to the readfile method.
    	    int setschool =+1;
    	    return true;
    	}
     
     
     
    	//This is where the program goes after deciding we had another word in the school na,e/
    	public static boolean isInteger2(String schoolnext2) {
    	    try { 
    	        Integer.parseInt(schoolnext2); 
    	    } catch(NumberFormatException e) { 
    	        //schoolnext2 turns out to be a string, that's three school word names. DONE
    	    	int setschool2=+2;
    	    	return false; 
    	    }
    	    //schoolnext2 turns out to be an integer, we're done. schoolnext2 value is the time value
    	    int setschool2 =+1;
    	    return true;
    	}
     
     
     
    	// Meat of the code, goes through all the lines
    	 public void readfile(){
    		while(reader.hasNext()){
    			//These are going to always be in this order, makes it do-able and simple.
    			String place = reader.next();
    			String bib = reader.next();
    			String firstname = reader.next();
    			String lastname = reader.next();
    			String grade = reader.next();
    			String school = reader.next() ;
    			String schoolnext = reader.next();
     
     
    		/*	*** Here is where I have trouble. All the schools have a different amount of words
    			 in their school name. You will now see my attempt at telling when the next part of the 
    			 race results line is part of the school name or the time ***              */
     
     
    			// If setschool== 1, it is an integer. Making schoolnext in this case our time string
    			//from the race results. Then we print the rest off.
    						if (setschool == 1) {
    							String pace = reader.next();
    							System.out.printf("%s %s %s %s %s %s %s %s\n" , place, bib, firstname, 
    							lastname, grade, school, schoolnext, pace);
    						}
     
    			// If setschool was == 2, the next word isn't an integer, its another school name.
    			// So we continue on with trying to find if the next string is the race results time
    			//or another school name
    			if (setschool == 2){
    				String schoolnext2 = reader.next();
     
     
     
     
     
    				//The most words a school has is 3, so once we know the third string is also a word,
    				//we can can print off the 3 school strings which are representing each word of the 
    				//school.
    				if(setschool2 == 2){
    					String time = reader.next();
    					String pace = reader.next();
    					System.out.printf("%s %s %s %s %s %s %s %s %s %s\n" , place, bib, firstname, 
    					lastname, grade, school, schoolnext, schoolnext2, time, pace);
    				}
     
    				//The third word was an integer, our time from the race results. So now we print off
    				//the two other Strings and realize schoolnext2 took on the schoolnext2 value
    				if(setschool2 == 1){
    					String pace = reader.next();
    					System.out.printf("%s %s %s %s %s %s %s %s %s\n" , place, bib, firstname, lastname,
    					grade, school, schoolnext, schoolnext2, pace);
    				}
     
     
    			}
     
     
     
     
     
     
     
    		}
    	} 
     
     
    	//Closing the file for good programming
    	public void closefile(){
    		reader.close();
    	}
     
    }


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Reading Race Results from a text file in Java

    Hi Charles,

    Firstly let me thank you for a well formatted question!

    OK, now I will try to explain some changes which will help you.

    You only need one isInteger function, and within that there is no need to have the "int setschool" lines, as these only exist locally to that function, and are never used.

    then to check if the input is an integer or not, perhaps a loop as follows, or something similar will work for you. (not tested)

    String school = "";
    String temp = reader.next();
     
    while( !isInteger( temp ) ) {
        school += temp;
        temp = reader.next();
    }
     
    // once we reach here, temp holds an integer

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,803
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: Reading Race Results from a text file in Java

    Assuming I can make a conclusion based on 1 sample line of the text file and your explanation:

    If you read the whole line as a String[] array split on spaces, there would be a maximum of 10 elements for a 3-name school, 9 elements for a 2-name school, and 8 elements for a 1-name school with the name of the school always beginning at index 5. You can use that approach and that info to parse the elements of the String[] array into the right parts.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading Race Results from a text file in Java

    This is a different from the original way I was think of doing it. It seems A LOT more simple than what I was thinking, I'll try it out it when I find the time. Thanks for the help!


    Quote Originally Posted by GregBrannon View Post
    Assuming I can make a conclusion based on 1 sample line of the text file and your explanation:

    If you read the whole line as a String[] array split on spaces, there would be a maximum of 10 elements for a 3-name school, 9 elements for a 2-name school, and 8 elements for a 1-name school with the name of the school always beginning at index 5. You can use that approach and that info to parse the elements of the String[] array into the right parts.


    --- Update ---

    Alright great! Thanks for your help. This is my first time on jp forums and I received two replies within the hour, awesome community. Thanks once again

Similar Threads

  1. [SOLVED] Help - reading from a text file into an ArrayList
    By deeevo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 07:47 AM
  2. Reading into an ArrayList from a text file
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2013, 04:32 PM
  3. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  4. [SOLVED] Very strange results when reading from a test file
    By DougFane in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: September 11th, 2011, 06:30 PM
  5. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM

Tags for this Thread