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

Thread: Help with file I/O scanning etc

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Help with file I/O scanning etc

    I have a program where a user enters a name and a gender, then the program is suppose to read through all these yob####.txt files to see if it contains that name,gender. if it does, i need to be able to get the number from it. in the yob####.txt files every single line is formatted like this "Mary,F,30045" the number is the number of times that name has been given to a baby for that year. I dont know how to get the number, i think i know how to find if it contains the Mary,F part but how do i get that number? heres my code:

    package mat2670;
     
    import java.util.*;
    import java.io.*;
     
    public class BabyNames {
     
        public static void main(String[] args) throws FileNotFoundException {
     
            //Set up the scanner to get a file
            Scanner console = new Scanner(System.in);
            System.out.print("Please enter a name:  ");
            String name = console.nextLine();
            System.out.print("Please enter a gender (M or F): ");
            String gender = console.nextLine();
            String genName = name + "," + gender;
     
            // Directory path here
            String path = "C:\\Users\\Connor Moore\\Documents\\yobdata";
     
            String files;
            File folder = new File(path);
            File[] Files = folder.listFiles();
     
            //loop to go through each file in the directory i have them in
            for (int i = 0; i < Files.length; i++) {
     
                Scanner nextFile = new Scanner(Files[i]);
     
                if (Files[i].isFile()) {
     
                    while (nextFile.hasNextLine()) {
     
                        String nextLine = nextFile.nextLine();
     
                        if(nextLine.contains(genName)) {
     
                            //this is where im stuck, im not too sure how to get the number. from the string that contains the number i need.
     
                        }
                    }
                }
            }
        }
    }


  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: Help with file I/O scanning etc

    I dont know how to get the number
    If the lines in the file are like you posted, you could use the String class's split() method with a comma on a line to get an array with 3 Strings. read the line into a String and split() it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 13th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    well how i have it set up is what im searching the file for would be like this "Mary,F" and i already have the line that contains that as a string. How does this split method work?

  5. #4
    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: Help with file I/O scanning etc

    The first place to look for how a method works is the API doc.
    Also a search on the forum will give you some code samples.

    im searching the file for would be like this "Mary,F"
    then you could use the length of the search String in the String class's substring method to get the characters of the number that is after the comma following the F
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    connorlm3 (February 13th, 2013)

  7. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    So in the java docs i found this

    String[] split(String regex)
    Splits this string around matches of the given regular expression.

    Trying to understand what it means, and how to implement it. So it might be easier to add the second comma to my genName so it has a string like "Mary,F," then if i do String[] number = split(genName); then would number have the numbers i need? and if so if there a method to convert those numbers from a string to an int, i have to work with the numbers as an int, keeping a running total, etc. thanks for the help btw

    --- Update ---

    I have this now, but the substring part is underlined, saying method : substring(int), variable numbers is of type String[]

                        if(nextLine.contains(genName)) {
     
                            String[] numbers = nextLine.split(genName);
                            int length = numbers.length;
                            System.out.println(numbers.substring(length));
                        }

  8. #6
    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: Help with file I/O scanning etc

    The regex can be VERY complicated. In this case "," will work as the regex to separate the Strings that will be written to the array that is returned. Write a small test program that experiments with different Strings to see what split() returns. Use the Arrays class's toString() method to format the array to see the results:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    The Integer class has a method to convert Strings to int values.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    arrays? where are arrays coming into this? you lost me now. and ok i found the method for the string to int conversion. still confused where arrays are coming into play at.. and if i try running my program now, it just starts endlessing print random stuff

  10. #8
    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: Help with file I/O scanning etc

    where are arrays coming into this?
    See the API doc for the splt() method:
    String[] split(String regex)
    Splits this string around matches of the given regular expression.
    it just starts endlessing print random stuff
    Post a small section of the output and the code that is generating it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    Would something like this work.
                        if(nextLine.contains(genName)) {
     
                            int length = nextLine.length();
                            String numbers = nextLine.substring(length);
                            System.out.println(numbers);
                        }


    --- Update ---

    I mean it obviously doesnt work, because my output is just a lot of blank lines.. but could anything like that work? to me it looks like it would print the numbers after the "Mary,F," if the String its on is "Mary,F,12345"

    --- Update ---

    nvm, wow i just realized all im doing is getting the total length of the string that contains genName. ahhhh

    --- Update ---

    Ok, im making progress but im confused why im getting the brackets and the , in the output, heres the statement now, and the output
                        if(nextLine.contains(genName)) {
     
                            String[] numbers = nextLine.split(genName);
                            System.out.println(java.util.Arrays.toString(numbers));
                        }
    Output:
    Please enter a name:  Mary
    Please enter a gender (M or F): F
    Mary,F,  //this is what genName contains
    [, 7065]
    [, 6919]
    [, 8149]
    [, 8012]
    [, 9217]
    [, 9128]
    [, 9891]

  12. #10
    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: Help with file I/O scanning etc

    Why are you using the contents of genname to splt() the String? Did you try writing a small test program with some sample Strings as I suggested to see how split() worked? See post#6
    [, 9891]
    It looks like using genname in the split() returned two Strings, an empty one and the number.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    Ok i see how the split method works now, if i make a string like String tosplit = new String(","); and i use it in split, it just puts spaces in the string after the commas? I dont see how that could help me though? + it still gives me the [ ]'s

  14. #12
    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: Help with file I/O scanning etc

    it still gives me the [ ]'s
    Are you talking about the output from the Arrays class's toString() method?
    That print out is only for debugging.
    The data you want is in the array.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    when i print the string there, the output gives me this [Mary, F, 1234]
                        if (nextLine.contains(genName)) {
     
                            String[] numbers = nextLine.split(tosplit);
                            String numbers2 = Arrays.toString(numbers);
                            System.out.println(numbers2);
                        }

    Oh ok, so this is irrelevant, So still how am i extracting the data i need from the array that contains either [Mary, F, 1234] or how i had it as [, 1234]?

    --- Update ---

    Just wanted to add, that the length of the number can vary. its not always the same length

  16. #14
    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: Help with file I/O scanning etc

    he array that contains either [Mary, F, 1234]
    To get an element in an array use an index with the array name:
    theArray[theIndex]
    gets the element at theIndex.
    Your array has 3 elements, you want the third one (index=2)
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Help with file I/O scanning etc

    Ok, thank you very much for your help, I can now get the number with the strategy you have helped me with, I've got it converting to an int also that i can add to a running total.

Similar Threads

  1. Scanning large FTP directories with apache.commons.net.ftp
    By daniel_el in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2012, 06:40 AM
  2. Port Scanning ERROR
    By dbz_sid in forum Java Networking
    Replies: 4
    Last Post: September 27th, 2011, 03:40 AM
  3. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  4. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  5. Scanning Document Issue
    By redvenice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2010, 08:18 AM