What I need to do is check a string and this is what I have so far
System.out.print("Please enter a string of 1's and 0s :");
numLine = input.next();
if (numLine.matches("^101$")) {
System.out.print("A is true");
} else if (numLine.matches("^\\d[01]+101$ ")){
System.out.print("B is true");
} else {
System.out.println("no");
}
The first part of the code the ^101$ works and prints out A is true
for B what I am trying to do is only accept 1s and 0s and return if it ends in 101
and I would like a C to do 101 at the begining and accept as many 1s and 0s
and a D that does 101 anywhere in it even if its 111000101000 or something else
the killer for me is the (“^101$”) syntax and I could use help with that
You don’t need
^and$withmatches(as I said in my comment). Here’s the way I see it:B:
"[01]+101"C:
"101[01]+"D:
"[01]*101[01]*"Recall that
X+meansXone or more times and thatX*meansXzero or more times.Also (for A), if you want to ‘match’ only
"101", you might want to consider simply usingequalsinstead, i.e.numLine.equals("101").