3

This is obviously a "Homework style" task, but I have issues with grasping how I can exclude a certain set of numbers. I understand how I can write out the odd numbers from 0-100, but I fail to comprehend any answers I've found, or tried to find, about how to exclude pre-set numbers of choice.

Basically, what I want to happen is the following: The program writes all odd numbers from 1-100. Lets say I don't want the numbers 5,9 or 11 to be displayed, but I want my program to continue, how do I make that happen?

I have two different sets of code (I don't know which is easier to use in this context, so I'm including both of them):

for (int i=0; i<100; i++)

            if (i % 2 !=0)
        System.out.println(i)

and

for (int i=1; i<100; i=i+2)
System.out.println(i)
Graham
  • 6,484
  • 2
  • 35
  • 39
Littlev
  • 33
  • 2
  • 2
  • 4
  • Try with something obvious to begin with (if i != 5 ...). You can refine it once you have that working. – Fruitbat Dec 01 '13 at 17:11
  • or create an array with the numbers to skip and check if the number is in the array – Bas van Dijk Dec 01 '13 at 17:11
  • Take a look at how to create and check a "set" of values in javascript: http://stackoverflow.com/questions/7958292/mimicking-sets-in-javascript/7958422#7958422 – jfriend00 Dec 01 '13 at 17:13

4 Answers4

2

(with JDK 7)

Set<Integer> exclusions = new HashSet<>(Arrays.asList(5, 9, 11)); //syntactic sugar from JDK 7
for (int i=0; i<100; i++) {
    if(i % 2 == 0 || exclusions.contains(i))  // if pair OR in exclusions list, isn't interesting
       continue; // continue allows to say: "isn't interesting, take me to the next value
    System.out.println(i); // Oh yeah, this one is interesting! It's an odd! print it!
}

(with JDK 5/6)

Set<Integer> exclusions = new HashSet<Integer>(Arrays.asList(5, 9, 11));
for (int i=0; i<100; i++) {
    if(i % 2 == 0 || exclusions.contains(i))
       continue;
    System.out.println(i);
}

of course, there is other ways to achieve the tasks, but I enjoy this one.

Besides, the use of Arrays.asList is a good tips to [initialize the Setinline][1].

It replaces this traditional boilerplate code:

Set<Integer> exclusions = new HashSet<Integer>();
   exclusions.add(5);
   exclusions.add(9);
   exclusions.add(11);
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • This works as well, but because I'm more of a novice when it comes to programming, I like simple code, that I could achieve myself without too much help from the internet. Nonetheless, thank you for your answer. – Littlev Dec 01 '13 at 17:24
  • @Littlev I put some explanation along the lines. – Mik378 Dec 01 '13 at 17:25
1
for (int i=0; i<100; i++) {
    if ( (i % 2 !=0) && i!=5 && i!=9 && i!=11) {
        System.out.println(i)
    }
}

It's bad practice to write it without the brackets since it's too easy to make an error when adding more lines later on. Also, you're probably writing Java and not JavaScript.

mb21
  • 34,845
  • 8
  • 116
  • 142
  • Thanks for the answer; Not only did you help me with this task, but also the next one, being prime numbers where I had to use % and / in the same if-statement. – Littlev Dec 01 '13 at 17:27
0

If you have constant numbers to exclude it is just enough to do it with ifs

for (int i=1; i<100; i=i+2) {
  if ( i != 1 && i !=5 && i != 10 ) {
    System.out.println(i);
  }
}

But if you are having some list of numbers, which are read from input or something, ifs will just fail to do it. What you need is set of numbers to exclude and Java has a lot of implementations of set which are also efficient in checking does some set contains given element. So you would do something like this:

// you can use hashset
Set<Integer> setOfElementsToExclude = new HashSet<Integer>();
// add as many elements as you want from input or manually
setOfElementsToExclude.add(1);
setOfElementsToExclude.add(5);
setOfElementsToExclude.add(10);
for (int i=1; i<100; i=i+2) {
  if ( !setOfElementsToExclude.contains(i) ) {
    // so print only the numbers not present in exclusion set
    System.out.println(i);
  }
}

If you want you can read about set here: http://docs.oracle.com/javase/7/docs/api/java/util/Set.html

Dejan Pekter
  • 705
  • 3
  • 18
0
StringBuilder sb = new StringBuilder();

Set<Integer> set = new HashSet<>();
set.add(5);
set.add(9);
set.add(11);

for(int i = 0 ; i <= 100 ; i++) {
    if(!set.contains(i) && i%2 != 0) {
          sb.append(i).append(" ");
    }
}
System.out.println(sb.toString());
Fr0z3n7
  • 2,548
  • 2
  • 14
  • 15