Question:
Can someone please help me with my java code for my homework? Please?
2010-04-25 22:48:03 UTC
I'm getting this error message after I compile it using textpad: "Variable strMortgage might not have been initialized"

mortgage = Double.parseDouble(strMortgage);

Here's my code...

import javax.swing.*;
import java.io.*;
import java.lang.*;

public class DebtRatio
{

public static void main(String[] args)
{

String strMonthlyIncome;
String strMortgage;
String strAutoLoan;
String strOtherDebt;

Double monthlyIncome;
Double mortgage;
Double autoLoan;
Double otherDebt;
Double ratio;

//Input Section
System.out.println("Debt Ratio Calculator");
strMonthlyIncome= JOptionPane.showInputDialog(null,"Enter your monthly payment");

strMonthlyIncome= JOptionPane.showInputDialog(null,"Enter your mortgage");

strAutoLoan= JOptionPane.showInputDialog(null,"Enter your autoLoan");

strOtherDebt= JOptionPane.showInputDialog(null,"Enter other debt");

//Conversion
monthlyIncome = Double.parseDouble(strMonthlyIncome);
mortgage = Double.parseDouble(strMortgage);
autoLoan = Double.parseDouble(strAutoLoan);
otherDebt = Double.parseDouble(strOtherDebt);

//Calculation
ratio = ((mortgage + autoLoan + otherDebt) / monthlyIncome);

//Output
System.out.println("Your Debt Ratio is" + ratio);

}
}
Four answers:
Scott
2010-04-25 22:52:56 UTC
You didn't ever set strMortgage to be anything because you set strMonthlyIncome twice.



strMonthlyIncome= JOptionPane.showInputDialog(null,"Enter your monthly payment");



strMonthlyIncome= JOptionPane.showInputDialog(null,"Enter your mortgage");
?
2016-06-02 04:59:15 UTC
Not quite. The assignment says that the method outlier() should generate a random number. Your method outlier() does not generate a random number; it only checks a number that is passed in from the outside. You need to move the generation of the random number from your main() method to your outlier() method. Then you won't need to pass in any parameters at all. Also, you're making the boolean return value a lot more complicated than it needs to be. The simplest way to do it is: return (p < 10 || p > 20); The parentheses here are not necessary, but they make it a little easier to read.
2010-04-25 22:55:43 UTC
strMonthlyIncome= JOptionPane.showInputDialog(null,"Enter your mortgage");



This should be strMortgage instead of strMonthlyIncome
Saula
2010-04-26 18:56:44 UTC
You used strMonthlyIncome twice.



If you have anymore problems IM Me: urjavacoder


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...