Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    unchecked or Unsafe operations

    Hey, This is a small java program for sorting user input into a vector it, sorts and outputs. Im a newbie but it works although it flash a warning
    program.java uses unsafe or unchecked operations.
    recompile with -XLint: unchecked for details.

    Its not the end of the world but this is homework so i can see me getting slated for it.

    Code:
    import java.util.Vector;
    import java.util.Collections;
    import javax.swing.JOptionPane;
    public class VectorSort {
    
    public static void main(String[] args) {
    
    
    Vector v = new Vector();
    String userInput;
    for (int i= 0; i<= 5; i++)
    { 
    userInput = JOptionPane.showInputDialog(null, "Enter words to be sorted now:");
    
    v.add(userInput);
    }
    
    
    Collections.sort(v);
    JOptionPane.showMessageDialog(null, "Vector elements after sorting : ");
    for(int j=0; j<v.size(); j++)
    JOptionPane.showMessageDialog(null, v.get(j));
    
    }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If you have not yet been taught generics, then you can safely ignore the errors.
    The problem is that Vector/Collections are generic types, so you should specify what type of object is allowed within them: Vector<String> v = new Vector<String>();.
    If generics have not been covered, I believe the supressWarnings options work at a compiler level. Add this immediately above the method signature:
    Code:
    @SuppressWarnings({ "rawtypes", "unchecked" })


  •  

    Posting Permissions

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