Is there anyway that i could reuse an existing pattern object and just change the pattern without having to create a whole new pattern object by compiling it? Plus i am getting an error variable p might not have been initialized![]()
Code:import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class X{ public static void main(String[] args){ String flavors = new String("marshmellowipeanutbutter i rockyroad"); Pattern p = p.compile("\\s*i\\s*"); // want to keep the same pattern object just change the pattern Scanner s = new Scanner(flavors); //.useDelimiter("\\s*i\\s*"); Matcher m = p.matcher(flavors); if(m.matches()){ while(s.hasNext(p)){ System.out.println(s.next()); } } } }




Reply With Quote