2

I have a string[] = [5 5, 1 2 N, LMLMLMLMM, 3 3 E, MMRMMRMRRM]

When I split the 2nd and 4th elements. I get

[, L, M, L, M, L, M, L, M, M]

[, M, M, R, M, M, R, M, R, R, M]

import java.io.*;

public class Instruction {
public String[] instructionList;
public String filePath;

public Instruction(String fileName) {
    this.filePath = fileName;
}

public String[] readFile() throws IOException {
    FileInputStream in = new FileInputStream(this.filePath);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    int n = 5;
    instructionList = new String[n];

    for (int j = 0; j < instructionList.length; j++) {
        instructionList[j] = br.readLine();
    }
    in.close(); 
    return instructionList;
}}

import java.util.Arrays;
public class RoverCommand {

public static void main(String[] args) throws Exception {

//Create new Instruction object with directions.txt.                
    Instruction directions = new Instruction("directions.txt");
    String[] instructions = directions.readFile();              
    String roverInstructions = Arrays.toString(instructions[2].split(""));
    System.out.println(roverInstructions);

}

I've tried replacing the empty space, to no avail. How can I split() without returning this empty first element?

yesyouken
  • 350
  • 1
  • 4
  • 14
  • 1
    Maybe you would care to show us your split statement? – MarsAtomic Sep 29 '14 at 22:08
  • 2
    What is the argument to `split`? Please show all relevant code. – rgettman Sep 29 '14 at 22:08
  • 1
    What do you mean split the second and fourth elements ? –  Sep 29 '14 at 22:14
  • @sin When I read the file 'directions.txt', I am returned while a String[] of [5 5, 1 2 N, LMLMLMLMM, 3 3 E, MMRMMRMRRM] – yesyouken Sep 29 '14 at 22:16
  • Why not just use `toCharArray()` like this: `System.out.println(Arrays.toString("LMLMLMLMM".toCharArray()));` -> `[L, M, L, M, L, M, L, M, M]` – Jason Sperske Sep 29 '14 at 22:16
  • Yeah, just make a character array out the string. –  Sep 29 '14 at 22:18
  • @JasonSperske , that worked! I am brand new to Java, don't quite know the ropes yet. Thanks for your help. However, is this the BEST way to do this? Not sure what best practices are in Java – yesyouken Sep 29 '14 at 22:19

1 Answers1

4

String.split() takes a regular expression so it may not be operating the way you are expecting it, however if you wanted to use it you could do this:

System.out.println(Arrays.toString("LMLMLMLMM".split("(?!^)")));

Which outputs this:

[L, M, L, M, L, M, L, M, M]

Here is a explanation of the regular expression:

(?!^) Negative Lookahead - Assert that it is impossible to match the regex below
   ^ assert position at start of the string

This will give you the same output though:

System.out.println(Arrays.toString("LMLMLMLMM".toCharArray()));

In this case I would advocate for toCharArray() both will work with double byte chars, so in the end it comes down to readability.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • what output do you get with OP's split line? :-) `System.out.println(Arrays.toString("LMLMLMLMM".split("")));` – Kent Sep 29 '14 at 22:22
  • `[, L, M, L, M, L, M, L, M, M]` – Jason Sperske Sep 29 '14 at 22:23
  • here I got the expected output...without the first empty element.. with the same line of code. – Kent Sep 29 '14 at 22:24
  • 1
    [We can also avoid `split("(?!^)")` magic and start using `split("")` with Java 8](http://stackoverflow.com/q/22718744/1393766) – Pshemo Sep 29 '14 at 22:26
  • I can't explain why you would have that experience. What version of Java are you using? – Jason Sperske Sep 29 '14 at 22:26
  • @JasonSperske `java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26) Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)` in Intellj IDE, language level was set as java7. – Kent Sep 29 '14 at 22:28
  • To that I submit: http://minus.com/i/dgUGrqkj1qKD I like your screen shot better though – Jason Sperske Sep 29 '14 at 22:43