2

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!

sushain97
  • 2,752
  • 1
  • 25
  • 36
user2877139
  • 21
  • 1
  • 1
  • 2
  • Read the Java tutorial on [Generics](http://docs.oracle.com/javase/tutorial/index.html) for some basic help. – camickr Oct 13 '13 at 22:59

2 Answers2

4

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.

William Gaul
  • 3,181
  • 2
  • 15
  • 21
2

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.
zari
  • 1,709
  • 1
  • 12
  • 18
Nishabu
  • 134
  • 1
  • 15
  • 1
    Aren't the warnings suppose to indicate that something might go wrong at a certain point? If there are no consequences, then why are the warnings logged? – Cristik May 04 '15 at 21:16