HELP! Recompile with -Xlint:unchecked for details?????
When I try to compile my code I get this message. What does this mean about my code?
Note: C:\Users\myname\Desktop\Week3_421.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Tool completed successfully
Thank you for any help.
Here is my code:
import javax.swing.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Week3_421 extends JFrame implements ActionListener
{
// Variables
JLabel titleLabel = new JLabel("Mortgage Calculator");
JLabel amntLabel = new JLabel("Mortgage Amount: ");
JLabel selecLabel = new JLabel("Choose your Mortgage: ");
JTextField amntText = new JTextField("");
JComboBox loanType;
JButton buttonCal = new JButton("Calculate");
JButton buttonClr = new JButton("Reset");
JButton buttonExit = new JButton("Exit");
JTextArea displayArea = new JTextArea(40, 20);
java.text.DecimalFormat fmt = new java.text.DecimalFormat("###,###,###.00");
public static void main(String[] args)
{
Week3_421 window = new Week3_421();
window.setSize(490, 490);
window.setVisible(true);
}
public Week3_421()
{
setTitle("Week3_421");
buildGUI();
}
public void buildGUI()
{
Container content = getContentPane();
content.setLayout(null);
// The array of loans
Loan[] loans = new Loan[3];
loans[0] = new Loan(5.35, 7);
loans[1] = new Loan(5.5, 15);
loans[2] = new Loan(5.75, 30);
loanType = new JComboBox(loans);
content.add(titleLabel);
content.add(amntLabel);
content.add(selecLabel);
content.add(amntText);
content.add(loanType);
content.add(buttonCal);
content.add(buttonClr);
content.add(buttonExit);
JScrollPane scroll = new JScrollPane(displayArea);
content.add(scroll);
// Orientation
titleLabel.setBounds(180, 20, 200, 30);
amntLabel.setBounds(50, 60, 150, 30);
amntText.setBounds(260, 60, 180, 30);
selecLabel.setBounds(50, 100, 150, 30);
loanType.setBounds(260, 100, 180, 30);
buttonCal.setBounds(50, 200, 100, 30);
buttonClr.setBounds(200, 200, 100, 30);
buttonExit.setBounds(350, 200, 100, 30);
scroll.setBounds(50, 250, 400, 180);
buttonCal.addActionListener(this);
buttonClr.addActionListener(this);
buttonExit.addActionListener(this);
}
// The course of Action
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==buttonClr)
{
buttonClear();
}
else
if (e.getSource()==buttonCal)
{
buttonCalculate();
}
else if (e.getSource()==buttonExit)
{
buttonExit();
}
}
// The action of Reset
public void buttonClear()
{
amntText.setText("");
displayArea.setText("");
}
// The Action of Exit
public void buttonExit()
{
System.exit(0);
}
// The Action of Calculate
public void buttonCalculate()
{
try
{
Loan loan = (Loan)loanType.getSelectedItem();
double amount = Double.parseDouble(amntText.getText());
int year = loan.getTermYears();
double rate = loan.getInterestYearly();
double payment = calcMonthlyPayment(amount, rate / 100, year);
displayMonthlyPayment(amount, rate / 100, year, payment);
} catch (Exception e)
{
displayArea.setText("Error! Please, ENTER the APPROPRIATE information.");
}
}
// Display
public void displayMonthlyPayment(double principal, double interestYearly, int termYears, double monthlyPayment)
{
double interestMonthly = interestYearly / 12.0;
displayArea.setText("Loan Amount: " + fmt.format(principal) + "\n");
displayArea.append("Years: " + termYears + "\n");
displayArea.append("Interest: " + interestYearly * 100 + "% \n");
displayArea.append("Monthly Payment: " + fmt.format(monthlyPayment) + "\n\n");
displayArea.append("Month\tPrinciple\tBalance Paid\tInterest Paid\n");
for (int count = 1; count <= termYears * 12; count++)
{
double interest = principal * interestMonthly; // Interest Paid
double loan = monthlyPayment - interest; // Load Balanced Paid
principal -= loan; // Amount left
// Display for balance and interest Paid
displayArea.append(String.format("%01d\t%s\t%s\t%s \n", count, fmt.format(principal),fmt.format(loan), fmt.format(interest)));
}
}
// Calculation of Monthly payment
public double calcMonthlyPayment(double principal, double interestYearly, int termYears)
{
double interestMonthly = interestYearly / 12.0;
double numberOfPayments = termYears * 12;
return (principal * interestMonthly) / (1.0 - (Math.pow((1.0 + interestMonthly), -numberOfPayments)));
}
}
class Loan
{
private double yearlyInterest;
private int termYears;
public Loan(double yearlyInterest, int termYears)
{
this.yearlyInterest = yearlyInterest;
this.termYears = termYears;
}
//Get the interestYearly
public double getInterestYearly()
{
return yearlyInterest;
}
//Get the termYears
public int getTermYears()
{
return termYears;
}
//Overriden toString
public String toString()
{
return String.format("%d year term at %.2f%%", termYears, yearlyInterest);
}
}
Re: HELP! Recompile with -Xlint:unchecked for details?????
You should do what it suggests. Just add the -Xlint:unchecked to your javac command line and it will tell you exactly what it's not happy about.
Re: HELP! Recompile with -Xlint:unchecked for details?????
Quote:
Originally Posted by
Sean4u
You should do what it suggests. Just add the -Xlint:unchecked to your javac command line and it will tell you exactly what it's not happy about.
So I complied it with Xlint and this is what it does not like. I am a java newbie so the warning does not make scene to me.
Thank you for your help!!!!
C:\Users\MYname\Desktop\Week3_421.java:68: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox
loanType = new JComboBox(loans);
^
where E is a type-variable:
E extends Object declared in class JComboBox
Re: HELP! Recompile with -Xlint:unchecked for details?????
That is a bit of an obscure warning! Have a look at the API doc for JComboBox (you should have a copy of the API docs on your PC and have it open in a browser tab all the time you're writing Java) - here's Oracle's page: JComboBox (Java Platform SE 6)
Hmmm - I can see nothing wrong with that line. Have you changed your code since your first post?
Re: HELP! Recompile with -Xlint:unchecked for details?????
That'll teach me to think out loud - I don't think I'm looking at the same API as you. That warning should normally only appear for 'generic' classes, and in my API JComboBox is not generic. What version of Java do you have installed? On a hunch, I changed the '6' to a '7' in my previous link to the API and I get:
JComboBox (Java Platform SE 7 )
Generic ComboBox! OK, so now the link in the API to 'How to use Combo Boxes' is now out of date - you'll have to read that page (your code is good for Java 6 and before, needs updating for Java 7) in conjunction with Java Generics Lesson: Generics (The Java™ Tutorials > Learning the Java Language)