Results 1 to 15 of 15
Thread: Hash Set in Java
- 08-07-2010, 03:44 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
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
- 08-07-2010, 03:58 PM #2
Can you copy and post the errors here?help me to identify the syntax errors
You don't need a library to pass a reference to an object to another class.to pass the set to another class, does anybody know what utility in the Java library I need to use?
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?
- 08-07-2010, 04:05 PM #3
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:
The only thing I changed wasJava 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(); } }
and the class name from 'requirements' to "Requirements."Java Code:Set<String> requirements = new HashSet<String>();
"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)
- 08-09-2010, 11:58 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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.
- 08-09-2010, 01:08 PM #5
please copy and paste here the full text of the error messages.I am getting these errors
- 08-09-2010, 01:33 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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
- 08-09-2010, 01:38 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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.
- 08-09-2010, 01:45 PM #8
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; ^
- 08-09-2010, 01:48 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
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
- 08-09-2010, 02:02 PM #10
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?
- 08-09-2010, 02:43 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
thats line 5 in my code the line that you said had errors in it.
- 08-09-2010, 03:25 PM #12
I thought that the 'line 5' from your post would be to the error.Location
line 5
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.
- 08-09-2010, 04:03 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,382
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-09-2010, 04:06 PM #14
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.
- 08-09-2010, 04:11 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,382
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
how to sort in this hash map
By akira_mz86@yahoo.com in forum New To JavaReplies: 6Last Post: 03-17-2010, 02:19 AM -
Hash
By sandy1028 in forum New To JavaReplies: 4Last Post: 04-17-2009, 10:36 AM -
Hash Map
By rekha in forum New To JavaReplies: 1Last Post: 03-21-2009, 01:00 PM -
Hash Table Help
By michael_mke in forum New To JavaReplies: 3Last Post: 11-27-2008, 05:12 PM -
Hash Table help
By rhm54 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks