Results 1 to 20 of 20
  1. #1
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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:
    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" );
    	}
    }
    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.
    Last edited by Epidilius; 06-10-2011 at 01:57 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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

  3. #3
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 01:49 AM.

  5. #5
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can you recompile with -Xlint:unchecked?

  7. #7
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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?

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    javac -Xlint:unchecked Car.class
    Your list declaration should also be using generics, so
    Java Code:
    List plateList =...;
    Becomes

    Java Code:
    List<String> plateList = ...;
    Also, any reason why you used such a long workaround for creating a list?
    Java Code:
    List<String> plateList = new ArrayList<String>();
    Seems much simpler and more straightforward.

  9. #9
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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?

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm assuming you forgot to import the package.

    Java Code:
    import java.util.ArrayList;
    As your first line of code.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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>();

  12. #12
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  15. #15
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    I added import java.util.ArrayList;, and I still get the same error. Where would I put the generics <>?

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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 exists
    Last edited by sunde887; 06-10-2011 at 02:55 AM.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  18. #18
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    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" );
    	}
    }

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Change
    Java Code:
    List plateList = ...;
    To

    Java Code:
    static List<String> plateList = new ArrayList<String>();
    You should really be using access providers too(private, public, protected), rather than packae private access.

  20. #20
    Epidilius is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

Similar Threads

  1. No-arg constructor??
    By collwill in forum New To Java
    Replies: 6
    Last Post: 03-09-2011, 03:50 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 09:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Tags for this Thread

Posting Permissions

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