-1

Alright, so the code is pretty straight forward. Generic class ourSet, that takes in some elements, puts it in a LinkedList, and does some functions on the two sets.

My problem is actually quite unrelated the general concept of the project, its more in the "user input interface" I've created. I want it to take in some Strings and add it to the set, then while receiving the string "EXIT" (all caps), to exit the loop, and do the same for the next set. What is happening is that the do while loop is only sending the 1st, 3rd, 5th,.. for all odd numbers.

package set.pkgclass;

import java.util.Scanner; 
import java.util.LinkedList; 


public class SetClass {


public static void main(String[] args) {

    ourSet<String> set1 = new ourSet<String>();  
    ourSet<String> set2 = new ourSet<String>(); 
    Scanner input = new Scanner(System.in); 



    System.out.println("Please enter a string to put in set 1, "
            + "type EXIT (in all caps) to end.");


    do {

        set1.add(input.nextLine());

    }
    while (!"EXIT".equals(input.nextLine()));


    System.out.println("Please enter a string to put in set 2, "
            + "type EXIT (in all caps) to end");

    do {

        set2.add(input.nextLine());
    }
    while (!"EXIT".equals(input.nextLine()));



    ourSet.intersection(set1,set2); 
    ourSet.difference(set1, set2);
    ourSet.union(set1, set2);








     }
  }

class ourSet<T>{




private LinkedList<T> mySet = new LinkedList<>();



public void add(T element){      
    mySet.add(element);
}

public void remove(T element){        
    mySet.remove(element);
}

public boolean membership(T element){        
    if(mySet.contains(element) == true) {
        return true; 
    }

    else {
    return false;
    }
}


public static <T> void union(ourSet<T> s1, ourSet<T> s2){
    System.out.print("The union is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i);
        if (!s2.mySet.contains(t)){
            s2.add(t);
            }

    }

    for (int i=0; i < s2.mySet.size(); i++){
        T t = s2.mySet.get(i);
        System.out.print(t+", ");
    }
    System.out.println();  


}
public static <T> void intersection(ourSet<T> s1, ourSet<T> s2){ 
    System.out.print("The intersection is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i); 
        if (s2.mySet.contains(t)) {
            System.out.print(t+", ");
        }
    }
    System.out.println();

}

public static <T> void difference(ourSet<T> s1, ourSet<T> s2){
    System.out.print("The difference is: ");
    for (int i=0; i < s1.mySet.size(); i++) {
        T t = s1.mySet.get(i);
        if (!s2.mySet.contains(t)) {
            System.out.print(t+", ");
        }

    }
    System.out.println();  
   }
 }
  • Well. he hadn't had a chance to find "an answer" to see if "THOSE answers" don't help. But hey, this is just a comment an can't be downvoted. Just give him the link and don't be snarky. This is a place where, it is advertised, "Developers learn, share, and build careers," not where a newbie is afraid to ask a goddang question, which MANY surely are. It's a fair question for anyone just starting out, with nowhere to turn. – DSlomer64 Jan 03 '18 at 01:11

3 Answers3

4

The reason is, you're calling input.nextLine() twice:

do {

    set1.add(input.nextLine());

}
while (!"EXIT".equals(input.nextLine()));

A much easier way than do-while is while:

while (!(String in = input.nextLine()).equals("EXIT")) {
    set1.add(in);
}
William Gaul
  • 3,123
  • 2
  • 13
  • 21
  • Awesome, Thanks so much. I didn't realize that I was calling it twice, It seems stupid now that I realize. +1 Thanks again – user2844764 Oct 04 '13 at 00:23
  • @user2844764 if this answer was the solution to your problem, don't forget to accept it. Thanks! – MDrabic Oct 04 '13 at 01:11
0

You ask for input twice

do {

    set1.add(input.nextLine()); // you enter a number

}
while (!"EXIT".equals(input.nextLine())); // what if you enter another number? 
// it'll just loop again, skipping that number

You need to ask for input once and use that either to stop or add it to your set.

Sotirios Delimanolis
  • 252,278
  • 54
  • 635
  • 683
0

You loop is doing exactly what you've told it to...

  • Read a line of text and add it to set1
  • Read a line of text and check to see if it equals "EXIT"
  • Repeat as required...

So, every second request is being used to check for the exit state. Instead, you should assign the text from the input to a variable and use it to do you checks, for example...

do {
    String text = input.nextLine();
    if (!"EXIT".equals(text)) {
        set1.add();
    }
} while (!"EXIT".equals(text));

ps- Yes, I know, you could use break ;)

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329