Results 1 to 15 of 15
  1. #1
    muj
    muj is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default Hash Set in Java

    I'm working on a project. I am trying to use a HashSet to insert objects in to a set. I then need to pas this set to another class so that I can perform operations on the set. I am using this code


    import java.util.HashSet;
    import java.util.Set;


    public class requirements {


    public void HashSet() {

    Set requirements = new HashSet();

    //Declare some string items
    String item_1 = "r1";
    String item_2 = "r2";
    String item_3 = "r3";

    boolean result;

    //Add the items to the Set
    result = requirements.add(item_1);
    System.out.println(item_1 + ": " + result);

    result = requirements.add(item_2);
    System.out.println(item_2 + ": " + result);

    result = requirements.add(item_3);
    System.out.println(item_3 + ": " + result);

    //Now we try to add item_1 again
    result = requirements.add(item_1);
    System.out.println(item_1 + ": " + result);

    //Adding null
    result = requirements.add(null);
    System.out.println("null: " + result);

    //Adding null again
    result = requirements.add(null);
    System.out.println("null: " + result);
    }


    public static void main(String[] args) {
    new requirements().HashSet();
    }
    }


    }

    But keep getting lots of syntax errors can somebody help me to identify the syntax errors?

    Also I want to pass the set to another class, does anybody know what utility in the Java library I need to use?

    any help would be very uch appreciated, I am new to Java

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    help me to identify the syntax errors
    Can you copy and post the errors here?

    to pass the set to another class, does anybody know what utility in the Java library I need to use?
    You don't need a library to pass a reference to an object to another class.
    Can you explain why calling a method in the other class with a reference to the set as an arg wouldn't solve your problem?

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Those are not syntax errors. They are suggestions. In your case, they are type safety. First off, please use the [code] tags when posting code. It retains the indentation. Second, you should read about generics. This is how it should be:

    Java Code:
    import java.util.HashSet;
    import java.util.Set;
    
    
    public class Requirements {
    
    	public void HashSet() {
    
    		Set<String> requirements = new HashSet<String>();
    
    		//Declare some string items
    		String item_1 = "r1";
    		String item_2 = "r2";
    		String item_3 = "r3";
    
    		boolean result;
    
    		//Add the items to the Set
    		result = requirements.add(item_1);
    		System.out.println(item_1 + ": " + result);
    
    		result = requirements.add(item_2);
    		System.out.println(item_2 + ": " + result);
    
    		result = requirements.add(item_3);
    		System.out.println(item_3 + ": " + result);
    
    		//Now we try to add item_1 again
    		result = requirements.add(item_1);
    		System.out.println(item_1 + ": " + result);
    
    		//Adding null
    		result = requirements.add(null);
    		System.out.println("null: " + result);
    
    		//Adding null again
    		result = requirements.add(null);
    		System.out.println("null: " + result);
    	}
    
    
    	public static void main(String[] args) {
    		new Requirements().HashSet();
    	}
    }
    The only thing I changed was
    Java Code:
    Set<String> requirements = new HashSet<String>();
    and the class name from 'requirements' to "Requirements."
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    muj1 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default the errors

    I am getting these errors

    Return type of the method is missing
    Syntax error on token class, delete this token
    Syntax error on token misplaced construct
    Syntax error insert Enum body to complete EnumheaderName
    Syntax Error insert Enum body to complete enum decleration
    The public type requirements must be defined in its own file.

    I have changed the coding to the code provided above.

    I thought I would have to use a library to move the Hashset to another class but if there is a better way please do let me know.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    I am getting these errors
    please copy and paste here the full text of the error messages.

  6. #6
    muj1 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default errors

    Description
    Return type for the method is missing test

    resource
    requirements.java

    Path
    project/src/org/test/requirementsproject

    resource
    requirements.java

    Location
    line 11


    Desription
    Syntax error on token "class", delete this token test project/src

    Resource
    Specification.java

    Path
    test project/src

    Location
    line 5


    Description
    Syntax error on token(s), misplaced construct(s)

    Resource
    test project/src

    Path
    Specification.java

    Location
    line 2

  7. #7
    muj1 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    Description
    Syntax error, insert "enum Identifier" to complete EnumHeaderName

    Resource
    test project/src

    Path
    Specification.java

    Location
    line 5


    Description
    Syntax error, insert "EnumBody" to complete EnumDeclaration

    Resource
    test project/src


    Path
    Specification.java

    Location
    line 5


    Description
    The public type requirements must be defined in its own file

    Resource
    test project/src

    Path
    Specification.java

    Location
    line 5


    I have another class called specification which is currently empty. I am using Ecplise.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    What is line 5? all the error messages point to it as the problem.
    Your posting of compile error messages is different. On my system I get:
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

  9. #9
    muj1 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default error

    this is line 5

    Set<String> requirements = new HashSet<String>();

    i get those errors in my ecplise problems bar at the nottom of my screen

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    Sorry, I don't know about your IDE. I use the javac program to compile my programs.
    The error message texts you posted don't include all the same information that the javac program generates.

    Is the line of code you show where the error messages occurred?

  11. #11
    muj1 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    thats line 5 in my code the line that you said had errors in it.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    Location
    line 5
    I thought that the 'line 5' from your post would be to the error.
    I don't know where the lines you posted come from. Your IDE doesn't give good error messages.

    Can you compile your program with javac and post the errors it generates. See my post#8 for the format of the javac errors.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    The original program compiles fine with me; there are just a few warnings about using a raw type Set but that's all; I did this in Eclipse; I think the OP is not showing us the code that s/he is trying to compile ...

    kind regards,

    Jos

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,616
    Rep Power
    23

    Default

    We need a crystal ball to be able to help. Or a webcam poised over the shoulder of the OP so we can see what they are doing.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    We need a crystal ball to be able to help. Or a webcam poised over the shoulder of the OP so we can see what they are doing.
    Or a remote controlled chainsaw it we have to slightly correct them ...

    kind regards,

    Jos ;-)

Similar Threads

  1. how to sort in this hash map
    By akira_mz86@yahoo.com in forum New To Java
    Replies: 6
    Last Post: 03-17-2010, 02:19 AM
  2. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  3. Hash Map
    By rekha in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 01:00 PM
  4. Hash Table Help
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 11-27-2008, 05:12 PM
  5. Hash Table help
    By rhm54 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •