user2877139
user2877139

Reputation: 21

Uses unchecked or unsafe operations, recompile with -Xlint

I have been dealing with this problem for a week and while I've found a few answers, it seems that none of them solve my program's issues.

I keep getting this message:

Note: Triangle.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Here is the code:

import java.util.*;
public class Triangle
{
   public double a;
   public double b;
   private double c;
   private ArrayList btsarr;

   private static int numMade = 0;

   public Triangle()
   {
      this.a = 3;
      this.b = 4;
      this.c = 5;
      this.btsarr = new ArrayList();

      this.btsarr.add(this.c + "");   //adding it as a String object
      Triangle.numMade++;
   }

   public Triangle(double x1, double x2, double x3)
   {
     this.a = x1;   //specs say no data validation necessary on parameters
     this.b = x2;
     this.c = x3;
     this.btsarr = new ArrayList();

     this.btsarr.add(this.c + "");
     Triangle.numMade++;
   }

   public void setC(double x)
   {
      if (x <= 0)
      {
         System.out.println("Illegal, can't set c to 0 or negative");
      }
      else
      {
        this.c = x;
        this.btsarr.add(this.c + "");
      }
    }

    public double getC()
    {   
      return this.c;
    }   

    public void showAllCs()
    {
      System.out.println(this.btsarr);
    }

    public boolean isRightTriangle()
    {
      if (this.a*this.a + this.b*this.b == this.c*this.c)
        return true;
      else
        return false;
    }

    public static int getNumMade()
    {
      return Triangle.numMade;
    }   
}

Any amount of help is appreciated, Thank you!

Upvotes: 2

Views: 25723

Answers (2)

Nishabu
Nishabu

Reputation: 144

Those are just warnings buddy. The compilation is successful. You can run your program without any problem.

If you want to suppress such warning just use the below annotation.

@SuppressWarnings("unchecked") //above the method definition.

Upvotes: 2

William Gaul
William Gaul

Reputation: 3181

The problem is that you do not specify a type for your ArrayList, so there's no compile-time checking for whether you're inserting, for example, integers into a String ArrayList. Since this would be an "unsafe operation" you get the warning.

To fix, replace private ArrayList btsarr; with private ArrayList<String> btsarr; and this.btsarr = new ArrayList(); with this.btsarr = new ArrayList<String>();.

In general you should always specify what type of objects you want things like ArrayList and HashMap (or any other Java collection) to hold when you instantiate them.

Upvotes: 4

Related Questions