Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: too many if's and else's!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Exclamation too many if's and else's!

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: too many if's and else's!

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: too many if's and else's!

    Quote Originally Posted by kassavetova View Post
    my program gets confused.
    I didn't realise computers were sentient yet. Has someone built Skynet and not told me about it?
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default

    You know what the point I was making is so either that's the only thing you read or you're really not here to help people but to waste your time writing useless sarcastic posts. Hope it was worth it

  5. The Following User Says Thank You to kassavetova For This Useful Post:

    AlexHail (October 29th, 2013)

  6. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,319
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: too many if's and else's!

    Lighten up. Don't take yourself so seriously.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    kassavetova (October 29th, 2013)

  8. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: too many if's and else's!

    Anybody who knows what causes the problem described in OP?

    Thanks

  9. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,319
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: too many if's and else's!

    the first if is to check if the command line arguments anywhere contain just a's just b's or both simultaneiously
    The first if only checks one args[] element at a time each time through the for loop. Finding a or b anywhere in the array args[] can't happen with the statement as it's written. Can you see that? What you've described would require a nested for loop (or perhaps one in advance of the current for loop) to check every args[] element to see if both 'a' and 'b' were present.

  10. #8
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: too many if's and else's!

    Hi,

    Spoonfeeding removed
    Last edited by jps; November 1st, 2013 at 08:28 AM. Reason: spoonfeeding
    Thanks and regards,
    Sambit Swain

  11. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: too many if's and else's!

    Quote Originally Posted by GregBrannon View Post
    The first if only checks one args[] element at a time each time through the for loop. Finding a or b anywhere in the array args[] can't happen with the statement as it's written. Can you see that? What you've described would require a nested for loop (or perhaps one in advance of the current for loop) to check every args[] element to see if both 'a' and 'b' were present.
    Ok so I understand what you are trying to say but I still cannot see the problem myself so I tried this with a nested loop on top of the rest of the code anyway:

    for(int k = 0; k < args.length; ++k)
         	 {
    		 for(int m = 0; m < Integer.parseInt(args[k]); ++m)
         	 	 {
    			if (args[k].matches("[a]*")  && args[k].matches("[b]*")) 
    			{
            			System.out.println("Starting two new books");
    		    	} 
    		 }
    	}

    I'm getting a runtime error now, I think because it expects only integers but I'm not very sure:

    javaTwoBookReader a 3 x 3 b 2 a r 2
    Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at TwoBookReader.main(TwoBookReader.java:15)

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,319
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: too many if's and else's!

    Oh, and you missed the solution posted by Sambit. Too bad.

    As you've correctly pointed out, this statement:

    if ( args[i].matches("[a]*") && args[i].matches("[b]*") )

    is the problem. Since 'i' will always be the same in the above comparison the result will always be false. The index must vary from 'i' to compare the first to THE REST of the elements in the args[] array, something like:

    if ( args[i].matches("[a]*") && args[j].matches("[b]*") )

    where j varies from i to args.length. That second index variable, j, would typically be controlled by a nested loop.

    OR if you know that the second book, b, will always be a specific element number and 'a' is always at index 0, you could simply compare the contents of those 2 indices:

    if ( args[0].matches("[a]*") && args[4].matches("[b]*") )

    but I don't know if that's always the case.

    Good luck!

Tags for this Thread