java - Scanning text to a variable until a "\t" is detected -


i'm trying write script reads in text file line line , assigns substrings of line individual variables. majority of done, can't seem figure out seemingly simplest part.

example text file:

12345 lenovo x1 carbon comment

ideal variables:

string tag = "12345";  string name = "lenovo x1 carbon";  string comment = "this comment"; 

actual variables:

string tag = "12345";  string name = "lenovo";  string comment = "x1 carbon comment"; 

i have of working, minus being able stop reading "\t" should detected. i'm using code below try , read in text until tab read (single variable example).

string myname = ""; while(!linescanner.next().contains("\t"))     myname += linescanner.next(); 

am missing simple in this?

supposing linescanner reference scanner, missing scanner's default delimiter pattern matches whitespace, , tab characters , space characters (among others) whitespace. tokens returned scanner never include delimiters.

you can set scanner use different delimiter pattern @ time via 1 of usedelimiter() methods. aware if later reset() scanner pattern restored default.

possibly want this:

pattern originaldelim = linescanner.delimiter(); pattern alternatedelim = pattern.compile("[\t\n]+");  string tag; string name; string comment;  tag = linescanner.next(); linescanner.usedelimiter(alternatedelim); name = linescanner.next(); comment = linescanner.next(); linescanner.usedelimiter(originaldelim); 

perhaps use alternative delimiter pattern time without switching; depends on details of input file format.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -