0

I am trying to read specific characters from a text file to store for further processing. The file i am trying to read from is laid out as :

45721 Chris Jones D D C P H H C D
87946 Jack Aaron H H H D D H H H
43285 Ben Adams C F C D C C C P
24679 Chuck Doherty F F F P C C C F
57652 Dean Betts F C F C D P D H
67842 Britney Dowling D D F D D F D D
22548 Kennedy Blake P F P F P F P P

The specific characters i am trying to read are the 8 character following the individuals names. I have attempted looking for solutions but i am terribly new to java and having problems understanding the logic so any help would be greatly appreciated.

AJJ
  • 899
  • 3
  • 11
  • 16

2 Answers2

2

Try out the Scanner class, it's great for this stuff

Scanner in = new Scanner(new File("./myfile.txt"));
while(in.hasNextLine()){
    //read in a whole line
    String line = in.nextLine();

    //sanity check so we don't try to substring an empty string from an empty line at the end of the file.
    if(line.length() == 0){
        continue;
    }

    //15 chars from the end it 8 chars + 7 spaces
    //We go back 16 though since the string index starts at 0, not 1.
    line = line.subString(line.length()-16, line.length()-1);

    //Now split the string based on any white spaces in between the chars spaces
    String[] letters = line.split(" ");
    //Now do something with your letters array 
}

Note that this assumes the formatting you posted is followed pretty strictly. The scanner can also read token by token if you wanted to set it up a little differently. Check out the Scanner documentation.

TheBat
  • 1,006
  • 7
  • 25
  • When i try to test this segment of code i get the error "The method size() is undefined for the type String", any thoughts? – AJJ Oct 09 '14 at 13:45
  • My bad, I forget if it's size or length for strings. Updated my answer – TheBat Oct 09 '14 at 13:50
  • So how would i go about just printing the array for now to see if it works properly. Would it just be System.out.printIn(letters); ? – AJJ Oct 09 '14 at 13:58
  • That syntax will simply print the hashcode of the array. You can either use a for loop and print out the element individually, or do `System.out.println(Arrays.tostring(letters));` – TheBat Oct 09 '14 at 14:01
  • Check this out http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array – TheBat Oct 09 '14 at 14:02
  • ah yes thank you, that worked perfectly! Would i now be able to access the rows of that array for further processing? – AJJ Oct 09 '14 at 14:09
  • Yup! you can access the array just like the other answer says, except with indices 0-7. So letter[0] would be D for the first line. – TheBat Oct 09 '14 at 14:11
  • great, you've been very helpful, much appreciated – AJJ Oct 09 '14 at 14:19
1

If the line is always of the form #### FirstName LastName A1 A2 A3 A4 A5 A6 A7 A8 , then the following code should work:

String line = "57363 Joy Ryder D D C P H H C D";
String[] cols = line.split("\\s+"); // split line by one or more whitespace chars

cols[0] would have the number and cols[3] through cols[10] would have the letters you desire, A1 through A8.

The indices would be different if there were a middle name or if there were no last name.

rajah9
  • 11,645
  • 5
  • 44
  • 57