Monday 15 July 2013

How to ignore newlines when using method skip(String) of the java.util.Scanner object? -


I would like to save some time and do not want to test each token manually in the parsed file, but it seems Use that code correctly using the skip (string) method of java.util.Scanner .

To re-submit the question: How can I do both to pass the test given below?

  Import java.io.StringReader; Import java.util.Scanner; Import junit.framework.Assert; Import org.junit.Test; Public class scannerest {lasting private last string text = "function block \ n \ nproject \ n \ tVAR_INPUT \ n \ t \ tUnited_by1: true; \ n \ tEND_VAR \ nEND_FUNCTION_BLOCK"; @Test Public Zero Scanner Skeptest () {scanner sc = new scanner (new string reader (text)); Sc.skip ("(i) FUNCTION_BLOCK?"); String blockname = sc.next (); Claim of Sc.hasNext ("(? I) VAR_INPUT"); // Added test sc.skip ("(? I) VAR_INPUT"); // line failure string variable = sc.next (); Sc.skip (":"); String type = sc.next (); Sc.skip ("(i) END_VAR?"); Sc.skip ("(i) END_FUNCTION_BLOCK?"); Assign "anonymous_project" .equals (blockName); Assign "anonymous_view1". Equal (variable); "Real". Extraordinary (type); } @Test Public Zero Scanner without Skeptest () {scanner sc = new scanner (new string reader (text)); String left = sc.next (); "FUNCTION_BLOCK" is insisted on. String blockname = sc.next (); Left = sc.next (); Assign "VAR_INPUT". String variant = sc.next (); Left = sc.next (); Emphasize ":". Unseen CAS (left); String type = sc.next (); Left = sc.next (); Claim "END_VAR" .equalsIgnoreCase (omitted); Left = sc.next (); Claim "END_FUNCTION_BLOCK" .equalsIgnoreCase (omitted); Assign "anonymous_project" .equals (blockName); Assign "anonymous_view1". Equal (variable); "Real". Extraordinary (type); }}   

Any suggestions and suggestions are appreciated

Tutu, thanks for editing my answer after trying to do this, please re-explain the explanation at the top.

Actually, when you want to leave a token with skip () , you must first leave the whitespace. On the contrary, next () method does this automatically.

From javadoc:

Next () and hasNext () methods and their primitive-type partner methods (like nextInt () and hasNextInt ()) before any input , Which match the delimiter pattern, and then try to return the next token.

Therefore, by using sc.skip (WHITESPACE + "abc") you can skip to any white space and then token abc The result looks like this:

  public class scannerest {static private last string text = "function block \ n \ n \ tVAR_INPUT \ n \ t \ tUnited_world1: real; \ N \ tEND_VAR \ nEND_FUNCTION_BLOCK "; Public static final string WHITESPACE = "[\\ n \\ t] +"; @Test Public Zero Scanner Skeptest () {scanner sc = new scanner (new string reader (text)); Sc.useDelimiter (white location); Sc.skip ("FUNCTION_BLOCK"); String blockname = sc.next (); Sc.skip (WHITESPACE + "VAR_INPUT"); String variant = sc.next (); Sc.skip (WHITESPACE + ":"); Sophikalon with string type = sc.next (); Sc.skip (WHITESPACE + "END_VAR"); Sc.skip (WHITESPACE + "END_FUNCTION_BLOCK"); Assign "anonymous_project" .equals (blockName); Assign "anonymous_view1". Equal (variable); "Real"; "Equal (typeWithSemiColon);}}   

Note that the scanner will not still divide the real by because the semicolon is actually white There is no place - I leave that to find out as a practice for you :)

No comments:

Post a Comment