2

Okay, so I want to read things in from a file. They look like this:

70          Fresno Credit Bureau             Fresno           CA          93714

And I want it to be in pieces, with the 70 going to an int, Fresno Credit Bureau and Fresno to go into separate strings, the state initials into separate chars, and finally feeding that last zip code into another int.

I've attempted to read it piecemeal using Scanner, and I've attempted to read it in wholesale as a string buffer and no dice either way.

while(gre.hasNextLine()){
            imidate = gre.nextInt();
            gre.skip(" ");
            han = gre.next();
            gre.skip(" ");
            luke = gre.next();
            gre.skip(" ");
            bok = gre.next().charAt(0);
            mander = gre.next().charAt(1);
            gre.skip(" ");
            imate = gre.nextInt();
            theTree.addNode(imate, han, luke, bok, mander, imidate);
        }

Which is probably screwing up on the parsing somewhere, from the looks of it, but I can't tell where. Any suggestions? Comments? Concerns?

3
  • Are those tabs, or is this a file with specific number of characters for fields? Does Java have a string split that will split on the tabs and make an array, like C# does? Commented Dec 3, 2014 at 22:23
  • @Nikki9696 yes Nikki, there is a java string split. You just need to pass in some sort of string to look for to split on. Commented Dec 3, 2014 at 22:26
  • Don't use Scanner. Read a line at a time and separate into columns based on character position or tabs or whatever. Commented Dec 3, 2014 at 22:32

3 Answers 3

1

It looks like you want to allow spaces inside your strings, treating 2+ spaces as separators. For that to work you need to set the appropriate pattern for Scanner's delimiter:

Scanner gre = ...
gre.useDelimiter("\\s{2,}");

Now the call to next will ignore spaces inside Fresno Credit Bureau, breaking only on two or more spaces.

This will let you get rid of the skip calls, too, because Scanner skips over delimiters.

Demo.

Finally, this piece of code looks suspicious:

bok = gre.next().charAt(0);
mander = gre.next().charAt(1);

If you are trying to grab the first and second characters of the same token, store it in a String before calling charAt(). Otherwise you would be accessing characters of two different tokens:

String tok = gre.next();
bok = tok.charAt(0);
mander = tok.charAt(1);
Sign up to request clarification or add additional context in comments.

Comments

0

Split the line into a String[] , then you can put the elements of the String array to your variables.

Like:

String[] line = gre.split("\\s+");
imidate = Integer.parseInt(line[0]);
han = line[1];
etc ...

Comments

0

Since each line consists of objects of different types, you should probably have a class to handle this. For example,

class MyObject {
    int    number;
    String name;
    String city;
    char[] state;
    int    zip;

    public static MyObject fromLine(String line) {
       String[] columns = line.split("\\s{2,}");
       number = Integer.parseInt(columns[0]);
       name   = columns[1];
       city   = columns[2];
       state  = columns[3].toCharArray();
       zip    = Integer.parseInt(columns[4]);
    }
}

If each line in your file looks like the one in your question, you could read the file line by line using a Scanner or a BufferedReader, and then treat the processing of each line separately. Using a BufferedReader, you could do the following:

try (BufferedReader reader = new BufferedReader(new FileReader(vocabulary))) {
        List<MyObject> myObjects = reader.lines()
                                         .map(MyObject::fromLine)
                                         .collect(Collectors.toList());
} catch (IOException e) { }

If you are using tabs, then use "\\t+" instead of "\\s{2,}".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.