Hey, I'm new the the world of programming, and my class uses Java (no pun)... Anyway, I've finished the assignment, but I'm trying to delve a little deeper than the requirements... I have been running my program just fine, but I need some if statements that will report an error and show a pop-up if a letter is typed rather than a number. Here is what I have so far for that part...
// Get the text from the fields
String lStr = loanField.getText();
String iStr = interestField.getText();
String mStr = startField.getText();
String eStr = endField.getText();
// Now let's check the strings to make sure someone typed something!
int checkpoint = 0;
if (lStr == null || lStr.equals(""))
{
JOptionPane.showMessageDialog(this, "You must input a loan amount.");
checkpoint ++;
}
if (iStr == null || iStr.equals(""))
{
JOptionPane.showMessageDialog(this, "You must input an interest rate.");
checkpoint ++;
}
if (mStr == null || mStr.equals(""))
{
JOptionPane.showMessageDialog(this, "You must input a starting value.");
checkpoint ++;
}
if (eStr == null || eStr.equals(""))
{
JOptionPane.showMessageDialog(this, "You must input an ending value.");
checkpoint ++;
}
if (checkpoint > 0)
return;
// Parse text
double L = Double.parseDouble(lStr);
double i = Double.parseDouble(iStr);
double m = Double.parseDouble(mStr);
double e = Double.parseDouble(eStr);
// Check each value
if (L < 0 || L > 1000000)
{
JOptionPane.showMessageDialog(this,"Loan amount must be between 0 and 1,000,000.");
checkpoint ++;
}
if (i < 0 || i > 100)
{
JOptionPane.showMessageDialog(this,"Interest rate must be greater than 0 and less than 100.");
checkpoint ++;
}
if (m <= 0)
{
JOptionPane.showMessageDialog(this,"Starting months must be greater than 0. ");
checkpoint ++;
}
if (m > e)
{
JOptionPane.showMessageDialog(this,"Starting months must not be greater than ending months.");
checkpoint ++;
}
if (e <= 0)
{
JOptionPane.showMessageDialog(this,"Ending months must be greater than 0. ");
checkpoint ++;
}
if (checkpoint > 0)
return;
Question
art Techo
Hey, I'm new the the world of programming, and my class uses Java (no pun)... Anyway, I've finished the assignment, but I'm trying to delve a little deeper than the requirements... I have been running my program just fine, but I need some if statements that will report an error and show a pop-up if a letter is typed rather than a number. Here is what I have so far for that part...
// Get the text from the fields String lStr = loanField.getText(); String iStr = interestField.getText(); String mStr = startField.getText(); String eStr = endField.getText(); // Now let's check the strings to make sure someone typed something! int checkpoint = 0; if (lStr == null || lStr.equals("")) { JOptionPane.showMessageDialog(this, "You must input a loan amount."); checkpoint ++; } if (iStr == null || iStr.equals("")) { JOptionPane.showMessageDialog(this, "You must input an interest rate."); checkpoint ++; } if (mStr == null || mStr.equals("")) { JOptionPane.showMessageDialog(this, "You must input a starting value."); checkpoint ++; } if (eStr == null || eStr.equals("")) { JOptionPane.showMessageDialog(this, "You must input an ending value."); checkpoint ++; } if (checkpoint > 0) return; // Parse text double L = Double.parseDouble(lStr); double i = Double.parseDouble(iStr); double m = Double.parseDouble(mStr); double e = Double.parseDouble(eStr); // Check each value if (L < 0 || L > 1000000) { JOptionPane.showMessageDialog(this,"Loan amount must be between 0 and 1,000,000."); checkpoint ++; } if (i < 0 || i > 100) { JOptionPane.showMessageDialog(this,"Interest rate must be greater than 0 and less than 100."); checkpoint ++; } if (m <= 0) { JOptionPane.showMessageDialog(this,"Starting months must be greater than 0. "); checkpoint ++; } if (m > e) { JOptionPane.showMessageDialog(this,"Starting months must not be greater than ending months."); checkpoint ++; } if (e <= 0) { JOptionPane.showMessageDialog(this,"Ending months must be greater than 0. "); checkpoint ++; } if (checkpoint > 0) return;Link to comment
Share on other sites
8 answers to this question
Recommended Posts