0

so I have had trouble lately with using multiple delimiters in a FileScanner. I would like to do so as the first Strings in my file are separated by commas, but before it goes to the next line there is no comma, only a space. This creates problems with assigning the values to my arrays.

    public class FileScan
    {
         public static void main(String[] args) throws IOException
         {
              String[] firstName = new String[50];
              String[] lastName = new String[50];
              String[] number = new String[50];

              Scanner fileScan = new Scanner(new File("phonebook.txt");
              fileScan.useDelimiter(",");

              for(int i = 0; fileScan.hasNext(); i++)
              {
                   firstName[i] = new firstName[fileScan.next];
                   lastName[i] = new lastName[fileScan.next];
                   number[i] = new number[fileScan.next];

                   System.out.println(firstName[i] +"\t"+ lastName[i] +"\t"+ number[i]);
              }

Because the File looks something like this:

    John, AppleSeed, 800-555-9000
    Jack, Torrance, 450-555-3000
    Elizabeth, MacDonald, 304-555-3042

I want to be able to switch the delimiter during runtime in order to not cause errors. However If I do this:

         fileScan.useDelimiter(",");
         firstName[i] = new firstName[fileScan.next];
         lastName[i] = new lastName[fileScan.next];
         fileScan.useDelimiter(" ");
         number[i] = new number[fileScan.next];

I am still getting errors.The only other possibility I could think of was to just use the default and try to take out the commas after. Is there a better way to do this?

David MacNeil
  • 126
  • 11

2 Answers2

2

Have you considered reading a line in , and then using string.split to pull out the Fname and Lname. How to split a string in Java

Community
  • 1
  • 1
DaBaer
  • 204
  • 1
  • 7
  • This could work too, but is a little more complicated than what I was expecting. – David MacNeil Dec 06 '13 at 18:59
  • The code isnt that bad for this. Merely read the line in, run a while look that makes sure its not EOF, and then parse the string into arrays of what you need. – DaBaer Dec 06 '13 at 19:01
  • hmm Yes I suppose, I was just hoping there would be another way using just deLimiters. I don't see any issues with it working. – David MacNeil Dec 06 '13 at 19:05
2

The Scanner.useDelimiter method takes a Pattern or the String representation of a Pattern as argument.

Have you tried simply using fileScan.useDelimiter(",\\s?");?

That should delimit you items based on , optionally followed by one whitespace.

For instance:

You have a file named foo.txt, containing the text in your example, in path: my/path/foo.txt

Here's the code to parse it with the Scanner

try {
    Scanner s = new Scanner(new File("my/path/foo.txt"));
    s.useDelimiter(",\\s?");
    while (s.hasNext()) {
        System.out.println(s.next());
    }
}
catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

Output:

John
AppleSeed
800-555-9000
Jack
Torrance
450-555-3000
Elizabeth
MacDonald
304-555-3042
Mena
  • 47,782
  • 11
  • 87
  • 106
  • The parameters (",\\s"); will let the fileScanner know to split on spaces after two commas? If so that is exactly what I was looking for! – David MacNeil Dec 06 '13 at 18:57
  • @FriarBob I'm actually starting to wonder whether I have misinterpreted your question. Please try the `Pattern` I've indicated and tell me whether it displays the proper result. Otherwise, please edit your answer with an exact input vs output. – Mena Dec 06 '13 at 18:59
  • The problem lies in going from the last string on the first line to the first string on the second line, Since it is delimiting based on the comma only, the number and the firstName are being put into the same array, and the last array at the end of the File is not being processed. – David MacNeil Dec 06 '13 at 19:03
  • @FriarBob so the `Pattern` I've indicated in my answer should work fine in your case. In a few seconds I'll append an example to my answer. – Mena Dec 06 '13 at 19:06