Results 1 to 20 of 20
Thread: Constructor not a statement
- 06-09-2011, 11:18 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Constructor not a statement
When I try to compile normally, I get an unchecked/unsafe error, and when I ignore it I get an error for each insance where I try to use my Car constructor. The error is Car.java:35: not a statement, with the little arrow pointed at the 'c' in car1, car2, and car3. My code is:
I would appreciate any help. Also, is there a way to keep my code format when I post? It would make it easier to read.Java Code:class Car { String plateNumber; boolean permit; int Time; List plateList = Arrays.asList( new String[] {} ); Car( String plate ) { this.plateNumber = plate; if ( plateList.contains( plate )) { boolean permit = true; System.out.println( "Car " + plate + " has a permit" ); } else { plateList.add( plate ); boolean permit = false; System.out.println( "Car " + plate ); } } public static void main(String[] args) { Car car1, car2, car3; car1 = new Car( "123 ABC" ); car2 = new Car( "123 abc" ); car3 = new Car( "123ABC" ); } }Last edited by Epidilius; 06-10-2011 at 12:57 AM.
- 06-09-2011, 11:24 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Flank your code with the [code][/code] tags to get the formatting, and remove the semicolon at the end of the SuppressWarnings annotation line, which would cause a compile time error
- 06-09-2011, 11:49 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
When I get rid of the semicolon, I get the unchecked or unsafe error. It's why I put the semicolon there in the first place.
- 06-10-2011, 12:42 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Get rid of the suppression and show me the warning please.
Your constructor also shouldn't have a return statement.
Also, it seems weird to add the plate to the plate list if it's already in the list, either the list monitors existing plates and ensures that no duplicate plates exist, in which case you should add the plate to the plate list if it's not found, then create the car, and if it does exist already, perhaps throw an exception, or exit the system.
Or,
The list is a list of acceptable lists, if it's found in the list, create the car and remove it from te list, and if not found, throw an exception or exit.Last edited by sunde887; 06-10-2011 at 12:49 AM.
- 06-10-2011, 12:57 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Note: Car.java uses unsafe or unchecked operations.
Note: Recompile with -Xlint:unchecked for details.
And thanks for pointing out my list problems. I don't know why I did that... I'll edit the new code into my first post.
- 06-10-2011, 12:59 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Can you recompile with -Xlint:unchecked?
- 06-10-2011, 01:00 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I thought that was what I was doing when I suppressed the warnings. If it wasn't could you tell me how to do so?
- 06-10-2011, 01:05 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Edit: you may want to make the list static as well, this way, each new car instance will have access to the already existing plates.
Type
Your list declaration should also be using generics, soJava Code:javac -Xlint:unchecked Car.class
BecomesJava Code:List plateList =...;
Also, any reason why you used such a long workaround for creating a list?Java Code:List<String> plateList = ...;
Seems much simpler and more straightforward.Java Code:List<String> plateList = new ArrayList<String>();
- 06-10-2011, 01:17 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I have it that way because it's the one that works. I've tried your way before, but it gives me a compile error 'Cannot find symbol,' with the arrow pointing at char 'A' in Array.
When I compile with -Xlint:unchecked it tells me
"Car.java:24: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
plateList.add( plate );
^
1 warning"
I got rid of the line 'plateList.add( plate );' and it compiles and runs fine, but I need it to be in there. Any thoughts?
- 06-10-2011, 01:25 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'm assuming you forgot to import the package.
As your first line of code.Java Code:import java.util.ArrayList;
- 06-10-2011, 01:27 AM #11
I have another problem with your code.
Why do you have the List inside the Car class? Think about a Book. Does each Book have a List of every other Book that exists? Or would you have a List of Books in another Object such as a Library? I'd do the same with your code. Have a List of Car objects somewhere else and only create a new Car and add it to the List if it does not already exist.
Also, that is a gawdawful way to create a List.
Java Code:List<String> list = new ArrayList<String>();
- 06-10-2011, 01:34 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I know it is, but I can't get it to work any other way. And I don't know what you mean. Regardless, my biggest concern is the plateList.add( plate ); line. I can't think of another way to add the plates to the list, though.
- 06-10-2011, 01:40 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I like the idea of using a static list to contain existing plates, but you can also do as junky stated.
The reason for the warning is that you haven't used generics. Which is the <>.
The cannot find symbol error is probably because you didn't import the array list class. Add the import statement as I showed and your code should work fine.
- 06-10-2011, 01:46 AM #14
Another thing, stop editing your first post. People rarely go back and re-read it to see if things have changed. If you have new code or information then post it in a new reply.
- 06-10-2011, 01:51 AM #15
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
I added import java.util.ArrayList;, and I still get the same error. Where would I put the generics <>?
- 06-10-2011, 01:51 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also, you may want to test the constructors input before assigning it. For example, if the plate already exists, should the car be made?
You may consider throwing an exception if the Plate already existsLast edited by sunde887; 06-10-2011 at 01:55 AM.
- 06-10-2011, 02:08 AM #17
Post your latest code so we can see what changes you have made. Also, copy and paste the EXACT error message(s) and indicate which line it occurs on.
- 06-10-2011, 03:02 AM #18
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Java Code:import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Car { String plateNumber; boolean permit; int Time; List plateList = Arrays.asList( new String[] {} ); Car( String plate ) { this.plateNumber = plate; if ( plateList.contains( plate )) { boolean permit = true; System.out.println( "Car " + plate + " has a permit" ); } else { plateList.add( plate ); //<---This is the problem line. When I get rid of it, my code works fine. I can't think of another way to do this, though. boolean permit = false; System.out.println( "Car " + plate ); } } public static void main(String[] args) { Car car1, car2, car3; car1 = new Car( "123 ABC" ); car2 = new Car( "123 abc" ); car3 = new Car( "123ABC" ); } }
- 06-10-2011, 03:17 AM #19
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Change
ToJava Code:List plateList = ...;
You should really be using access providers too(private, public, protected), rather than packae private access.Java Code:static List<String> plateList = new ArrayList<String>();
- 06-10-2011, 04:01 AM #20
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
No-arg constructor??
By collwill in forum New To JavaReplies: 6Last Post: 03-09-2011, 02:50 AM -
Constructor
By Sarinam in forum AWT / SwingReplies: 1Last Post: 06-19-2008, 08:03 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks