I\'m working on a program similar to the following except it needs to loop 10 ti
ID: 3890373 • Letter: I
Question
I'm working on a program similar to the following except it needs to loop 10 times asking for numbers and the operator each time. I'm able to run it once, store in the database and retrieve the info but am failing to create a successful loop process for the remaining 9 iterations.
Here are the original reqs:
1. Create a program and Class called "calcs" to save calculations
2. Program must create a table called calcs
3. must accept 2 double type variable as input from the user
4. Option to *, / ,-, + the 2 numbers
5. must store inputted numbers as double type variables into Calcs Table
6. Display values from user in the table
7. Populate the database with 10 rows of values (ie, run this 10 times and store in the database).
My code is similar to the following code which was posted by another expert. I have a slightly different process but it closely follows this code as a reference.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Calcs {
private double op1;
private double op2;
private String optr;
public Calcs(double op1, double op2, String optr) {
super();
this.op1 = op1;
this.op2 = op2;
this.optr = optr;
}
public double calculate(){
if(optr.equals("+")){
return op1+op2;
}
else if(optr.equals("-")){
return op1-op2;
}
else if(optr.equals("*")){
return op1*op2;
}
else if(optr.equals("/")){
return op1/op2;
}
else{
System.out.println("invalid operator: "+optr);
}
return 0.0;
}
public static void main(String[] args) {
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","orcl");
Scanner scn=new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
double op1=scn.nextDouble();
double op2=scn.nextDouble();
System.out.println("Enter Operator Option to *, / ,-, + ");
String optr=scn.next();
Calcs cal=new Calcs(op1, op2, optr);
double result=cal.calculate();
PreparedStatement pstmt=con.prepareStatement("insert into calculations values(?,?,?,?)");
pstmt.setDouble(1,op1); //1 specifies the first parameter in the query
pstmt.setString(2,optr);
pstmt.setDouble(3,op2);
pstmt.setDouble(4,result);
int i=pstmt.executeUpdate();
System.out.println(i+" records inserted successfully");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from calculations");
System.out.println("operand1"+" "+"operator"+" "+"operand 2"+" = "+"result");
while(rs.next())
System.out.println(" "+rs.getDouble(1)+" "+rs.getString(2)+" "+rs.getDouble(3)+" = "+rs.getDouble(4));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
Explanation / Answer
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Calcs {
private double op1;
private double op2;
private String optr;
public Calcs(double op1, double op2, String optr) {
super();
this.op1 = op1;
this.op2 = op2;
this.optr = optr;
}
public double calculate(){
if(optr.equals("+")){
return op1+op2;
}
else if(optr.equals("-")){
return op1-op2;
}
else if(optr.equals("*")){
return op1*op2;
}
else if(optr.equals("/")){
return op1/op2;
}
else{
System.out.println("invalid operator: "+optr);
}
return 0.0;
}
public static void main(String[] args) {
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","orcl");
Scanner scn=new Scanner(System.in);
for(int k=0;k<10 ; ++k) {
System.out.println("Enter 2 numbers: ");
double op1=scn.nextDouble();
double op2=scn.nextDouble();
System.out.println("Enter Operator Option to *, / ,-, + ");
String optr=scn.next();
Calcs cal=new Calcs(op1, op2, optr);
double result=cal.calculate();
PreparedStatement pstmt=con.prepareStatement("insert into calculations values(?,?,?,?)");
pstmt.setDouble(1,op1); //1 specifies the first parameter in the query
pstmt.setString(2,optr);
pstmt.setDouble(3,op2);
pstmt.setDouble(4,result);
int i=pstmt.executeUpdate();
System.out.println(i+" records inserted successfully");
}
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from calculations");
System.out.println("operand1"+" "+"operator"+" "+"operand 2"+" = "+"result");
while(rs.next())
System.out.println(" "+rs.getDouble(1)+" "+rs.getString(2)+" "+rs.getDouble(3)+" = "+rs.getDouble(4));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
===============================================================
This code will create 10 records and once you have inserted all the records it will show you all the data, Thanks, let me know if there is any concern.
Thanks