My question is in regards to my code below. Basically, I've come to the point when I am using too many if's and else's and my program gets confused. The point of the first if is to check if the command line arguments anywhere contain just a's just b's or both simultaneiously. Assuming a and b stand for two different books, I want my program to print out whether I am starting one new book or two new books (respectively). I only want to make my first if work, I don't wish to change any other bits of the code unless absolutely nessesary.
public class TwoBookReader { public static void main(String args[]) { PageCounter pca = new PageCounter(); PageCounter pcb = new PageCounter(); boolean changed = true; String book = "a"; PageCounter currentBook = pca; int bookmark=0; for(int i = 0; i < args.length; ++i) { if (args[i].matches("[a]*") && args[i].matches("[b]*")) //CODE I WANT TO CHANGE... { System.out.println("Starting two new books"); } if(args[i].equals("a")) { book = "a"; currentBook = pca; changed = true; } else if(args[i].equals("b")) { book = "b"; currentBook = pcb; changed = true; } else if(args[i].equals("x")) { bookmark=currentBook.whatPageAmIOn(); System.out.println("Bookmarked page " + bookmark + " in " +book); } else if(args[i].equals("r")) { System.out.println("Return to page " + bookmark + " in " +book); } else { if(!changed) { System.out.println("Still reading from "+book); } else { System.out.println("Reading from "+book); } for(int j = 0;j < Integer.parseInt(args[i]);++j) { System.out.println("Read page "+currentBook.whatPageAmIOn()); currentBook.readPage(); } System.out.println("Put "+book+" down"); changed = false; } } } }
This is what the program prints out. I don't know why it completely ignores the first if!
C:\Users\Downloads>java TwoBookReader
a 3 x 3 b 2 a r 2
Reading from a
Read page 1
Read page 2
Read page 3
Put a down
Bookmarked page 4 in a
Still reading from a
Read page 4
Read page 5
Read page 6
Put a down
Reading from b
Read page 1
Read page 2
Put b down
Return to page 4 in a
Reading from a
Read page 7
Read page 8
Put a down
----------------------
fanks!