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 3 of 3

Thread: How can I make this shorter

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How can I make this shorter

    I want to make the following code shortened into a loop
    can anyone help me out
            while (usernumber >= 1 && usernumber <= 7)
    		 {
    		    usernumber = scanner.nextInt();
     
    		    if (usernumber == vehicle[0]);
    	        {
    		    counter1 = counter1 + 1;
    	        }
    	       if (usernumber == vehicle[1])
    	        {
    	        counter2 = counter2 + 1;
    	        }
    	       if (usernumber == vehicle[2])
    	        {
    	        counter3 = counter3 + 1;
    	        }
    	       if (usernumber == vehicle[3])
    	        {
    	        counter4 = counter4 + 1;
    	        }
    	       if (usernumber == vehicle[4])
    	        {
    	        counter5 = counter5 + 1;
    	        }
    	       if (usernumber == vehicle[5])
    	        {
    	        counter6 = counter6 + 1;
    	        }
    	       if (usernumber == vehicle[6])
    	        {
    	        counter7 = counter7 + 1;
    	        }
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I make this shorter

    Compress the counterX variables into an array.

    If only one of the if statements can be true, then
    To get the code you've posted to execute faster, use if/else if vs all ifs. The first true if means that none of the rest of the ifs can be true.
    Last edited by Norm; August 6th, 2011 at 07:53 PM. Reason: removed

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I make this shorter

    Look into using loops, switch statements, and different data types - together with Norm's advice would boil your code down to a few lines.

Similar Threads

  1. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM
  2. How to make a graph like this
    By sugarhigh in forum AWT / Java Swing
    Replies: 1
    Last Post: August 27th, 2010, 09:05 AM
  3. Trying to make a bot
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 11th, 2010, 02:07 PM
  4. make forum with EJB??!!
    By element_sole in forum Enterprise JavaBeans
    Replies: 3
    Last Post: February 12th, 2010, 02:18 PM
  5. How to make it???
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 29th, 2009, 02:49 PM