0

Assuming I had a text file sample.txt that has info like this:

John-Mike "male" "a computer scientist" 6 9

I want to read in this file into an array where

array[0] = John-Mike 
array[1] = male
array[2] = a computer scientist
array[3] = 6
array[4] = 9

i have tried

String[] tokens = file.nextLine().split(" ");

it gives me something like this instead

array[0] = John-Mike
array[1] = male
array[2] = a
array[3] = computer
array[4] = science
array[5] = student
.
.
.

but that splits all the whites paces including the ones in the apostrophe and stores them separately. How do I use split to manipulate this with scanner? I have searched all through the web for a good amount of time for a credible solution but I have found none yet. Any reference or info would be great

EDIT:

You cannot add anything to the text file just to make changes to the delimeter

ekeith
  • 644
  • 1
  • 10
  • 33
  • So you want to split on white space except when there's a `"` until there's another `"`... just create a loop that does that or something similar... use your imagination ;) – DigitalNinja Mar 22 '16 at 23:27
  • If you don't agree that your question is duplicate post comment in which you will explain why it is not duplicate, [don't repost same question again](http://stackoverflow.com/q/36166924/1393766). Don't forget to add in your comment `@nickOfPerson` who closed your question as duplicate so he would be informed about your comment. This way (if you ware right) your question can be reopened. – Pshemo Mar 22 '16 at 23:54

3 Answers3

1

You will need to write a regular expression in order to split it that way. Check out this post: Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token)

Community
  • 1
  • 1
Denis
  • 1,219
  • 1
  • 10
  • 15
  • then I would need to change the way the file is by adding "\" but I don't want to add anything to the text file – ekeith Mar 22 '16 at 23:31
  • I am trying to get the correct RE for you. meanwhile you can use the link. – Denis Mar 22 '16 at 23:32
1

One way to go about it is to extract them as groups with regex. For example:

String s = "John-Mike \"male\" \"a computer scientist\" 6 9";
Pattern p = Pattern.compile("[\\w\\d-]+|\"[\\w\\d -]+\"");
Matcher m = p.matcher(s);
while(m.find()) {
    System.out.println(m.group());
}
/* Result: 
John-Mike
"male"
"a computer scientist"
6
9
*/
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38
0

You may need to split string with " first and then, split it further if it contains white space and is not enclosed in quotes, e.g. something like this (not tested):

public static void main(String[] args) throws Exception{
    String s = "John-Mike \"male\" \"a computer scientist\" 6 9";
    String[] tokens = s.split("\"");
    List<String> newTokens = new ArrayList<>();
    for(String token : tokens){
        token = token.trim();
        if(token.isEmpty()){
            continue;
        }
        if(token.contains(" ") && !s.contains("\""+token+"\"")){
            //This means it is a separate string with spaces, in this case, split further
            for(String subToken : token.split(" ")){
                newTokens.add(subToken);
            }
        }else{
            newTokens.add(token);
        }
    }
    System.out.println(newTokens);
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Assuming you are not allowed to add or change anything in the txt file. How would this be read? – ekeith Mar 22 '16 at 23:34
  • You can read the file line by line and split it as described above. – Darshan Mehta Mar 22 '16 at 23:35
  • you are adding \ to the txt file. reading it without adding "\" to split is what i am asking – ekeith Mar 22 '16 at 23:37
  • I am not adding anything into file. I am just enclosing string with quotes and checking whether it exists into the original string. – Darshan Mehta Mar 22 '16 at 23:38
  • 1
    `\\` is not being added to the file, it is to escape the quote in quotes in the string. If you were to print out the string it would not be there. – Matthew Wright Mar 22 '16 at 23:38
  • `String s = "John-Mike \"male\" \"a computer scientist\" 6 9";` should be `String s ="John-Mike "male" "a computer scientist" 6 9"` thats what I meant – ekeith Mar 22 '16 at 23:40
  • That would not work if you try it. You have to escape the quotes within the quotes when you make a string. – Matthew Wright Mar 22 '16 at 23:41
  • That is just a java representation to escape the quotes. You can directly use the string returned by e.g. `br.readLine()` there. – Darshan Mehta Mar 22 '16 at 23:41
  • @elle you are confusing what ``\`` is inside String. `"` is special character in Java which represents start and end of the string so you can't write something like `System.out.println("foo"bar");` because the middle `"` would be understood as end of string, which means only `"foo"` would be seen as proper string and `bar"` part would cause error. If you want to print something like `foo"bar` then you need to let Java see `"` as simple character, not control character. To so so we are escaping it by adding ``\`` before like `System.out.println("foo\"bar");`. – Pshemo Mar 22 '16 at 23:47
  • @elle In other words string literal `"foo\"bar"` represents string `foo"bar`. You don't need to worry about adding ``\`` to your text file because there each character is not treated as possible part of Java code, but as simple text so no escaping is needed. – Pshemo Mar 22 '16 at 23:48