• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IF Condition not working!?!?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
In the pain of desparation I'm pleading for the help of anyone with an opinion. This small applet should collect an integer from a textfield and siaply it in a table. This part works, but the IF statement I use (in public void checkAndRecordData) doesn't seem to restrict the integers that can be entered. Can anyone help? Please?
Dave.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class GradeChart extends Applet
implements ActionListener {

private TextField
gradeAField = new TextField(5); // For entry of number of students with grade A
private TextField
gradeBField = new TextField(5);
private TextField
gradeCField = new TextField(5);
private TextField
gradeDField = new TextField(5);
private TextField
gradeEField = new TextField(5);

private int gradeAs, gradeBs, gradeCs, gradeDs, gradeEs; // To record validated entered data
private boolean displayTable=false;
private boolean inputGood=false;

private Button
displayChartButton = new Button("Display bar chart"); // To request display of chart with current data
private Button
clearFieldsButton = new Button("Clear data fields");

private final Color // Various fixed colours
windowBackgroundColor = Color.cyan;

private final Font // Various fixed fonts
labelFont = new Font("Serif", Font.BOLD, 16),
chartFont = new Font("SansSerif", Font.BOLD, 18);

private final int // Various fixed coordinates and dimensions
chartLeftX = 20,
chartTopY = 150;

// Set up the GUI
public void init() {

setBackground(windowBackgroundColor);

Label gradeAFieldLabel = new Label("# of grade As:");
gradeAFieldLabel.setFont(labelFont);
add(gradeAFieldLabel);
add(gradeAField);

Label gradeBFieldLabel = new Label("# of grade Bs:");
gradeBFieldLabel.setFont(labelFont);
add(gradeBFieldLabel);
add(gradeBField);

Label gradeCFieldLabel = new Label("# of grade Cs:");
gradeCFieldLabel.setFont(labelFont);
add(gradeCFieldLabel);
add(gradeCField);

Label gradeDFieldLabel = new Label("# of grade Ds:");
gradeDFieldLabel.setFont(labelFont);
add(gradeDFieldLabel);
add(gradeDField);

Label gradeEFieldLabel = new Label("# of grade Es:");
gradeEFieldLabel.setFont(labelFont);
add(gradeEFieldLabel);
add(gradeEField);

add(displayChartButton);
displayChartButton.setFont(labelFont);
displayChartButton.addActionListener(this);

add(clearFieldsButton);
clearFieldsButton.setFont(labelFont);
clearFieldsButton.addActionListener(this);

} // end of init

// Draw the bar chart
public void paint(Graphics g) {

g.setFont(chartFont);
int tableX=30;
int tableY=190;
int tableWidth=400;
int tableHeight=200;

if (inputGood){

g.setColor(Color.gray);
g.fillRect(chartLeftX-6, chartTopY-6, tableWidth+12, tableHeight+12);
g.setColor(Color.cyan);
g.fillRect(chartLeftX-5, chartTopY-5, tableWidth+10, tableHeight+10);
g.setColor(Color.gray);
g.fillRect(chartLeftX-3, chartTopY-3, tableWidth+6, tableHeight+6);
g.setColor(Color.white);
g.fillRect(chartLeftX, chartTopY, tableWidth, tableHeight);
g.setColor(Color.gray.darker());

for (int counter=0; counter < 4; counter++) {
g.drawLine(tableX-10, tableY, tableX-10+tableWidth, tableY);
tableY+=40;
}
tableY=150;

for (int counterA=0; counterA < gradeAs; counterA++) {
g.drawString("A", tableX-5, chartTopY+30);
tableX+=10;
}
tableX=30;
for (int counterB=0; counterB < gradeBs; counterB++) {
g.drawString("B", tableX-5, chartTopY+70);
tableX+=10;
}
tableX=30;
for (int counterC=0; counterC < gradeCs; counterC++) {
g.drawString("C", tableX-5, chartTopY+110);
tableX+=10;
}
tableX=30;
for (int counterD=0; counterD < gradeDs; counterD++) {
g.drawString("D", tableX-5, chartTopY+150);
tableX+=10;
}
tableX=30;
for (int counterE=0; counterE < gradeEs; counterE++) {
g.drawString("E", tableX-5, chartTopY+190);
tableX+=10;
}
} // End of if (inputGood)

if (!inputGood){
g.setColor(Color.red);
g.drawString("Data is absent or invalid - cannot display chart!", 20, 200);
}

} // end of paint

// actionPerformed
public void actionPerformed(ActionEvent e) {

if (e.getSource() == displayChartButton){
checkAndRecordData();
repaint();
}

if (e.getSource() == clearFieldsButton) {
clearData();
repaint();
}

} // end of actionPerformed

// checkAndRecordData:
private void checkAndRecordData() {

int tempAs=0, tempBs=0, tempCs=0, tempDs=0, tempEs=0;
try {
tempAs = Integer.parseInt(gradeAField.getText());
}
catch (NumberFormatException ex) {
System.out.println("Text field does not contain a number: "+gradeAField.getText());
}

gradeAs = tempAs;

try {
tempBs = Integer.parseInt(gradeBField.getText());
}
catch (NumberFormatException ex) {
System.out.println("Text field does not contain a number: "+gradeBField.getText());
}

gradeBs = tempBs;

try {
tempCs = Integer.parseInt(gradeCField.getText());
}
catch (NumberFormatException ex) {
System.out.println("Text field does not contain a number: "+gradeCField.getText());
}

gradeCs = tempCs;

try {
tempDs = Integer.parseInt(gradeDField.getText());
}
catch (NumberFormatException ex) {
System.out.println("Text field does not contain a number: "+gradeDField.getText());
}

gradeDs = tempDs;

try {
tempEs = Integer.parseInt(gradeEField.getText());
}
catch (NumberFormatException ex) {
System.out.println("Text field does not contain a number: "+gradeEField.getText());
}

gradeEs = tempEs;

if (((gradeAs >= 0) && (gradeAs <= 39)) && ((gradeBs >= 0) && (gradeBs <= 39)) && ((gradeCs >= 0)
&& (gradeCs <= 39)) && ((gradeDs >= 0) && (gradeDs <= 39)) && ((gradeEs >= 0) && (gradeEs <= 39))){
inputGood=true;
}
} // end of checkAndRecordData
private void clearData() {

gradeAField.setText("");
gradeBField.setText("");
gradeCField.setText("");
gradeDField.setText("");
gradeEField.setText("");
}
} // end of GradeChart applet
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not just set the boolean value on the catch statements? Set it to false if any throw the exception.
 
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic