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:
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.
}
}
}
}
}
}
Re: Help with file I/O scanning etc
Quote:
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.
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?
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.
Quote:
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
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[]
Code :
if(nextLine.contains(genName)) {
String[] numbers = nextLine.split(genName);
int length = numbers.length;
System.out.println(numbers.substring(length));
}
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:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
The Integer class has a method to convert Strings to int values.
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
Re: Help with file I/O scanning etc
Quote:
where are arrays coming into this?
See the API doc for the splt() method:
Quote:
String[] split(String regex)
Splits this string around matches of the given regular expression.
Quote:
it just starts endlessing print random stuff
Post a small section of the output and the code that is generating it.
Re: Help with file I/O scanning etc
Would something like this work.
Code :
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
Code :
if(nextLine.contains(genName)) {
String[] numbers = nextLine.split(genName);
System.out.println(java.util.Arrays.toString(numbers));
}
Output:
Code :
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]
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
It looks like using genname in the split() returned two Strings, an empty one and the number.
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
Re: Help with file I/O scanning etc
Quote:
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.
Re: Help with file I/O scanning etc
when i print the string there, the output gives me this [Mary, F, 1234]
Code :
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
Re: Help with file I/O scanning etc
Quote:
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)
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.