简体   繁体   中英

(Java) Substrings & Reading data from two files using hashmap

If I had a .txt file called animals that had fishfroggoat etc. in it, and another file called owners that had something like:

fish:jane
frog:mark
goat:joe

how could I go about pairing the pets to their owners? I'm fairly sure a HashMap would be good here, but I'm stuck. I put the animal text into a string, but I don't know how to break it up into 4 characters properly.

Any help would be great.

Sorry I didn't add any code, but thanks to you guys' help (especially Ted Hopps) I worked it out and, more importantly, understood it. :-)

There are various approaches. The most direct is to split it using the substring method:

String animals = "fishfroggoat";
String fish = animals.substring(0, 4);
String frog = animals.substring(4, 8);
String goat = animals.substring(8); // or (8, 12)

If you have an arbitrarily long list of 4-character animals, you can do this:

String animals = "fishfroggoatbear";
int n = animals.length() / 4;
String[] animalArray = new String[n];
for (int i = 0; i < n; ++i) {
    animalArray[i] = animals.substring(4*i, 4*i + 4);
}

You can split the pet/owner strings using split :

String rawData = "fish:jane";
String[] data = rawData.split(":");
String pet = data[0];
String owner = data[1];

Use String split as given below.

String msg=fish:jane; msg.split(":")

Then it will make array separate by ":".

This is how you split a string into 4-character chunks in just one line:

String[] animals = input.split("(?<=\\G....)");

This may seem like black magic, so I'll try to demystify it. Welcome to the dark art of regular expressions...

The String.split() method splits the string on every match to the specified regex. So let's look at the regex:

(?<=\\G....)
  • The construct (?<=regex) is a "positive look behind" for the regex, meaning that the characters preceding the point in the input between characters (because a look behind is zero-width) must natch the regex.
  • The regex \\G (coded as \\\\G as a java String constant) means "start of previous match" but also initially matches start of input.
  • The regex .... matches any 4 characters.

Thus, when expressed in English, the regex (?<=\\\\G....) means "after every characters".

IF anyone is interested, removing \\G and splitting on (?<=\\....) causes it to split on every character after the 4th = it just means "preceded by 4 characters" - you need the \\G to find 4 new characters.


Here's some test code:

public static void main(String[] args) throws Exception {
    String input = "fishfroggoatbear";
    String[] animals = input.split("(?<=\\G....)");
    System.out.println(Arrays.toString(animals));
}

Output:

[fish, frog, goat, bear]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM