-1

I want to split the String String line = "20E WED 01PM 0E"; so that I have variables for each "part," where the parts would look something like:

int fDegree = 20;
String fDegreeEW = "E";
String day = "WED";
int time = 01;   \\ '1' is fine too
String amPm = "PM";
int sDegree = 0;
String sDegreeEW = "E";

I was thinking of first splitting the String line at spaces and then doing regex on each part to get the data, but I couldn't get that to work. What would be the nicest way to split a String like this into a bunch of different variables? Note: This is not the only String I want to check. The first "20" could also be "1" or "165", for example, so the words and numbers do not necessarily have a set length. But the pattern of number, word, space, word, space, number, word, space, number, word will always be true.

Edit: I tried:

String line = "20E WED 01PM 0E";
String[] splitArray = line.split(" ");
Scanner scanner = new Scanner(splitArray[0]);
int fDegree = scanner.nextInt();

but that threw an InputMismatchException. Can I use scanner here or no?

kneedhelp
  • 555
  • 4
  • 18
  • 1
    Split using a space delimiter first. Then use a regex to separate each substring into number and letters. Don't use a scanner, try this: http://stackoverflow.com/questions/8270784/how-to-split-a-string-between-letters-and-digits-or-between-digits-and-letters No one will write code for you. If you have a code which is not working, you can post it here so people can point out why its not working. – rgamber Apr 07 '16 at 01:41
  • @rgamber Why `split()` (which uses a regex), then regex each part, when a single regex can do the entire job? – Andreas Apr 07 '16 at 01:52
  • Agreed! my comment was hastily written! I am unable to edit my comment, seems like its too late. Thanks for correcting! – rgamber Apr 07 '16 at 01:54

3 Answers3

4

This regex will do it:

(\d+)(\w+)\s+(\w+)\s+(\d+)(\w+)\s+(\d+)(\w+)

See regex101 for demo (look at "match information" pane on the right).

Here's how to code:

Pattern p = Pattern.compile("(\\d+)(\\w+)\\s+(\\w+)\\s+(\\d+)(\\w+)\\s+(\\d+)(\\w+)");

String line = "20E WED 01PM 0E";
Matcher m = p.matcher(line);
if (! m.matches())
    throw new IllegalArgumentException("Bad input: " + line);
int    fDegree   = Integer.parseInt(m.group(1));
String fDegreeEW = m.group(2);
String day       = m.group(3);
int    time      = Integer.parseInt(m.group(4));
String amPm      = m.group(5);
int    sDegree   = Integer.parseInt(m.group(6));
String sDegreeEW = m.group(7);

The pattern variable p can be reused for every line.

Andreas
  • 154,647
  • 11
  • 152
  • 247
3

Johnny is right about the Scanner, but you should also consider how the data will be entered. It would be a lot easier to split this information if you gave the pattern a set length.

For example, if you have the line entered as "020 E WED 13 000 E", then all you need to do is split based on the token " " (space). You would then be given a three digit degree, your cardinal direction, the day, 1:00pm on a 24 hour clock (13), another three digit degree (I'm assuming that you'll never go above 360), and another cardinal direction.

If you need to save this information in a data structure (like an array) for later, you can either use line.split() or StringTokenizer.

1

You don't use scanner like that. Scanner is usually for letting user input a value to the system. If you want to print out all value i suggest this:

String line = "20E WED 01PM 0E";
String[] splitted = line.split(" ");
for(int i = 0; i < splitted.length(); i++){
System.out.println(splitted[i]);
}
Johnny Cheuk
  • 237
  • 2
  • 15