Hello everyone my name is Fyree, and I'm having problems with a school assignment where I need to create a method that takes the values from the user, and puts then through the computeRate()
method to print out a line that shows the computed Rate. Since the program is taking the user input values as Strings, I am unable to use that in the compute rate formula since they are not ints.
My problem is being able to convert the Strings into ints, and having the computeRate()
be able to correctly take two of the six values input by the user (the intev5 / inbv part...). The rest of the values are only to be used for a bar graph that I need to make after this which is a problem for another question. For some reason, it is unable to find those two variables listed above, even though before it was able to find the String versions of the user input value (but of course could not correctly compute the rate because they are not ints). So here is my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rate_Graph extends JApplet implements ActionListener
{
JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
public void init()
{
setLayout(new FlowLayout());
bv = new JLabel("Enter beginning value:");
bv1 = new JTextField(5);
ev1 = new JLabel("Enter year 1 value:");
ev_1 = new JTextField(5);
ev2 = new JLabel("Enter year 2 value:");
ev_2 = new JTextField(5);
ev3 = new JLabel("Enter year 3 value:");
ev_3 = new JTextField(5);
ev4 = new JLabel("Enter year 4 value:");
ev_4 = new JTextField(5);
ev5 = new JLabel("Enter year 5 value:");
ev_5 = new JTextField(5);
int intbv = Integer.parseInt(bv1.getText());
int intev1 = Integer.parseInt(ev_1.getText());
int intev2 = Integer.parseInt(ev_2.getText());
int intev3 = Integer.parseInt(ev_3.getText());
int intev4 = Integer.parseInt(ev_4.getText());
int intev5 = Integer.parseInt(ev_5.getText());
go = new JButton("Add!");
go.addActionListener(this);
add(bv); add(bv1);
add(ev1); add(ev_1);
add(ev2); add(ev_2);
add(ev3); add(ev_3);
add(ev4); add(ev_4);
add(ev5); add(ev_5);
add(go);
}
public void actionPerformed(ActionEvent event)
{
Object src = event.getSource();
if(src==go){
String strbv = bv1.getText();
String strev1 = ev_1.getText();
String strev2 = ev_2.getText();
String strev3 = ev_3.getText();
String strev4 = ev_4.getText();
String strev5 = ev_5.getText();
}
}
public double computeRate()
{
double rate = (Math.pow(intev5 / intbv, 1.0 / 5.0) - 1);
return rate;
System.out.println(rate);
}
}
Any help would be greatly appreciated.
When you declare a variable in a block of code or in a method, then that variable is only
visible
in that block of code.E.g.
intbv
is only visible ininit
likewise
strbv
in only visible in thisif
statement.If you require variables to be used in other methods then make them fields (class variables) like you have done with your
JLabel
and otherJ*
variables.Note As other have mentioned, putting code after a return statement will result in an error.
You have
System.out.println(rate)
after the return statement... this is an unreachable statement, so you must remove it or put it before the return.intev5
andinbv
are not visible because they are declared inside of theinit()
method, so they are only visible within the scope of that method.If you want to use those variables within
computeRate()
, one way would be to declare them outside ofinit()
: