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: Continue statement?

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Continue statement?

    I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted. Help please.
    import java.util.Scanner;
     
    public class occurrence {
     
    	public static void main(String[] args) {
     
    	//scanner/values
    		Scanner input = new Scanner(System.in);
    		int number = 101;
     
    	//array
    		int[] allNum = new int[number];
     
    	//for loop, user input
    		for(int i = 1; number != 0; i++) {
    			System.out.print("Please enter integers from 1 to 100, enter 0 when finished: ");
    				number = input.nextInt();
    			allNum[number]++;
    			}
    		//printout
    		for(int j = 1; j <= 100; j++) {
    			if (allNum[number] == 1) {
    				System.out.println(j + " shows " + allNum[j] + " time.");
    			}
    			else if (allNum[number] > 1) {
    				System.out.println(j + " shows " + allNum[j] + " times.");
    			}
    			if (allNum[number] == 0) {
    				continue;
    			}
     
    		}
    	}
    }
    Last edited by Elyril; March 22nd, 2014 at 11:30 AM.


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

    Default Re: Continue statement?

    The continue is used to skip the execution of the rest of the code inside of a loop.
    Putting the continue at the end of a loop won't skip any code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Continue statement?

    Sorry, thought I changed it back to the original. This is what I had before.
    for(int j = 1; j <= 100; j++) {
    	                if (allNum[number] == 0) {
    				continue;
    			}
    			if (allNum[number] == 1) {
    				System.out.println(j + " shows " + allNum[j] + " time.");
    			}
    			else if (allNum[number] > 1) {
    				System.out.println(j + " shows " + allNum[j] + " times.");
    			}			
    		}
    This is also the part where I have to change 'time' to 'times'
    1 shows 1 time.
    2 shows 2 time.
    3 shows 0 time.
    4 shows 4 time.
    5 shows 3 time.
    6 shows 1 time.
    7 shows 1 time.
    8 shows 1 time.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Continue statement?

    Check the use of array indexes: j vs number
    If you don't understand my answer, don't ignore it, ask a question.

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

    Elyril (March 22nd, 2014)

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Continue statement?

    Thanks for that, but how would I change the word 'time' like in box#3 to 'times' if more than 1?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Continue statement?

    print the String up to the end of "...time"
    then test the number and print one of the following to end the line:
    if == 1 print "."
    if > 1 print "s."
    If you don't understand my answer, don't ignore it, ask a question.

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

    Elyril (March 22nd, 2014)

Similar Threads

  1. [SOLVED] Continue and break statements
    By ///M Dood in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 22nd, 2013, 03:46 PM
  2. problems about methods....cant apply the continue y or n..
    By cruiserone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2013, 05:49 AM
  3. i'm stack, have no idea how to continue this. Help
    By chingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 01:55 AM
  4. Help with interrupting a loop without break or continue
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 08:51 PM
  5. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM