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

Thread: My code is not working

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

    Default My code is not working

    Hi
    I was wondering if someone can help me out with a bit of code.
    I cant get my loop to work properly, in particular the incrementing of vehicle [0]
    Everytime I run the loop it skips vehicle[0] but after i punch in 1 for a second time it starts to increment. the other counters are working fine except for this [0]
    Can anyone tell me how to fix this problem?

    Thanks
      int usernumber;
    		int vehicle[]={1,2,3,4,5,6,7}; 
    		int counter[]={0,0,0,0,0,0,0};
     
    		Scanner scanner = new Scanner(System.in);
    		usernumber = scanner.nextInt();
     
     
    		while (usernumber <= 7 && usernumber >= 1)
    		{
    			usernumber = scanner.nextInt();
     
     
    		   if (usernumber == vehicle[0])
    		   {
    		    counter[0] = counter[0] + 1;
    	        }
    	       if (usernumber == vehicle[1])   
    	       {
    	        counter[1] = counter[1] + 1;
    	        }
    	       if (usernumber == vehicle[2])   
    	       {
    	        counter[2] = counter[2] + 1;
    	        }
    	       if (usernumber == vehicle[3]) 
    	       {
    	        counter[3] = counter[3] + 1;
    	        }
    	       if (usernumber == vehicle[4])
    	       {
    	        counter[4] = counter[4] + 1;
    	        }
    	       if (usernumber == vehicle[5])
    	       {
    	        counter[5] = counter[5] + 1;
    	        }
    	       if (usernumber == vehicle[6])
    	       {
    	        counter[6] = counter[6] + 1;
    	        }
    		}


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: My code is not working

    In my first 'proper' programming job, we had to 'check out' source to edit by going into the library, finding the folder on the hanging racks that had the continuous-sheet print out of the last change, physically taking it back to our desks and then debugging the problem section by hand. To do that, there was a template (a plastic sheet with holes in) that you put over the paper, drew a few lines to make a simple table and wrote some standard headings on it. Then you'd have to write the current line number in the left column and write the values of your variable-of-interest in the rest of the row and at the end of each row a note of what changed on that line. You'd repeat that until you could demonstrate the bug in pencil marks. Only then could you edit the code and pass the lot onto the testers.

    What we wouldn't have given for a web forum! I was looking at your code a day or two ago - you were asking for a 'short version', right? There are some obvious ways of making this code smaller. Your logic is in an awkward order. After you instantiate the Scanner, you read a number. The next line (the while) you validate that number, and then you read a number which you don't validate. And then you use that number to increment your counts. I'll stop there, I guess you can see the problem now!

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    mike2452 (August 9th, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My code is not working

    There are some obvious ways of making this code smaller.
    Namely a loop.

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

    mike2452 (August 9th, 2011)

  6. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: My code is not working

    Namely a loop.
    I was thinking along the lines of 'looking up' the vehicle references rather than hard-coding them. The code:
    if (usernumber == vehicle[0])
    {
    counter[0] = counter[0] + 1;
    }
    if (usernumber == vehicle[1])
    {
    counter[1] = counter[1] + 1;
    }
    if (usernumber == vehicle[2])
    {
    counter[2] = counter[2] + 1;
    }

    uses the contents of the vehicle array which appear to me to be static (the vehicle ids don't change and there are no gaps - which sounds plausible, if not very flexible). The vehicle array is basically an index map from vehicle numbers to counter index. So the code is like this:
    if (usernumber == 1)
      counter[0 /* usernumber - 1 */] = counter[0 /* usernumber - 1 */] + 1;
    else if (usernumber == 2) // else because only one expression at a time can be true
      counter[1 /* usernumber - 1 */] = counter[1 /* usernumber - 1 */] + 1;
    else if (usernumber == 3) // no brackets needed for only one statement
      counter[2 /* usernumber - 1 */] = counter[2 /* usernumber - 1 */] + 1;
    ... since the counter index is always 1 less than the entered usernumber, the cascade of if statements can be reduced to this:
    if (usernumber > 0 && usernumber <= counter.length)
      counter[usernumber - 1]++; // myInt++ is the same as myInt = myInt + 1
    OP can even put an else clause on that input-validating-if to break out of the loop (there isn't always a nice concise loop-terminating condition for user-input loops, so I'd use a 'while (true)' here) if the input is not valid. Am I spoonfeeding yet?

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

    mike2452 (August 9th, 2011)

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

    Default Re: My code is not working

    Hey Sean
    Is there a way of incrementing that first initial input without re doing the whole thing. I have tried everything I could but still no worky

    Thanks so much for your help

  9. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: My code is not working

    Your first getInt from Scanner is never used - the value of usernumber is immediately re-assigned on entry into the loop. You could just delete the Scanner.getInt line before the loop. If you're worried about deleting code, just 'comment it out' - put a // at the start of the line or wrap the whole line in /* and */

    When you do that, your while loop won't run because Java initialises int data members to zero if you don't initialise them to something else. You could kludge your code by initialising usernumber to 1-to-7, but that would be nasty. What your loop should really look like is (pseudocode)
    loop
        get input
        if input is not valid
            exit the loop
        do something with the input
    end loop
    because validation has to follow input, it's hard to put the validation tests in the condition for the while loop. There is a way of doing it that I sometimes use and I suspect a lot of Java coders use (put the input assignment inside the condition), but it's ugly. My preference for cases like this is to 'loop forever' - use 'while(true)' - get the input and break on failed validation.

Similar Threads

  1. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM
  2. Getting code working with gui
    By Nostromo in forum AWT / Java Swing
    Replies: 2
    Last Post: March 21st, 2011, 09:34 PM
  3. Code working, but not the way I thought...
    By JLogan3o13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2010, 01:34 PM
  4. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  5. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM