I have to input variables using BufferedReader and readline(). there are two var
ID: 3786676 • Letter: I
Question
I have to input variables using BufferedReader and readline(). there are two variables ( a and b) that i need to input for an equation. when I try to use only one variable ( a ) I get no errors. But when I put the other variable an error shows up. I'm new to BufferedReader. How can I use more than one variable.
package homeworkcsc201;
import java.io.*;
public class Frequency {
public static void main(String[] args) throws java.io.IOException
{
// these are the variables that make up the equation (y)
double x , y, i, z, w, a, b, c;
final double t;
BufferedReader br = new BufferedReader ( new InputStreamReader(System.in));
System.out.println(" Enter the value of a and b to solve for y ");
String s1 ;
s1=br.readLine();
// variables of the equation
a =Double.parseDouble(s1);
b= Double.parseDouble(s1);
t= 0.002 * Math.pow(10,-2);
// I separated the equation into different parts
i= 2*3.14* Math.pow(10, 6) * t ;
x = Math.sin(i);
z= 2 * 3.14 *5 * Math.pow(10, 5);
w= Math.cos(z);
c= Math.exp(-a*t);
// equation of y
y= Math.abs(x+w)/c+ 50*b ;
System.out.println(" The output of y is " + y);
}
}
Explanation / Answer
Hi,
I have updated the code and highlighted the code changes below.
Frequency.java
import java.io.*;
public class Frequency {
public static void main(String[] args) throws java.io.IOException
{
// these are the variables that make up the equation (y)
double x , y, i, z, w, a, b, c;
final double t;
BufferedReader br = new BufferedReader ( new InputStreamReader(System.in));
System.out.println(" Enter the value of a and b to solve for y ");
String s1 ;
s1=br.readLine();
String s2 ;
s2=br.readLine();
// variables of the equation
a =Double.parseDouble(s1);
b= Double.parseDouble(s2);
t= 0.002 * Math.pow(10,-2);
// I separated the equation into different parts
i= 2*3.14* Math.pow(10, 6) * t ;
x = Math.sin(i);
z= 2 * 3.14 *5 * Math.pow(10, 5);
w= Math.cos(z);
c= Math.exp(-a*t);
// equation of y
y= Math.abs(x+w)/c+ 50*b ;
System.out.println(" The output of y is " + y);
}
}
Output:
Enter the value of a and b to solve for y
2
3
The output of y is 151.05475595654278