Error in Do While Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maas
    New Member
    • Aug 2007
    • 6

    #1

    Error in Do While Loop

    hello

    i have a problem in the do while loop
    where the program will ask the user to enter yes and do an iteration, nut in my example it did not do??

    here is the code

    [PHP]import java.sql.*;
    import java.io.*;
    public class Project3A {

    public static void main(String[] args) {
    String mname="";
    String mail="";
    String tel="";
    String ans="";
    try {

    System.out.prin tln("Beginning Connection");
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    String accessFileName = "jdbc:odbc:Proj ect";
    String connURL = "jdbc:odbc:;DRI VER=Microsoft Access Driver (*.mdb);DBQ="+a ccessFileName+" .mdb;PWD=";
    Connection con = DriverManager.g etConnection( accessFileName) ;
    Statement stmt = con.createState ment();
    System.out.prin tln("Connection done successfully");
    stmt.execute("C reate table Member(Name String,EMAIL String,Telephon e String)");
    System.out.prin tln("Table Created");
    do{


    System.out.prin tln("Please, Enter the Member Name : ");
    InputStreamRead er istream = new InputStreamRead er (System.in);
    BufferedReader br = new BufferedReader (istream);

    try{
    mname= (br.readLine()) ;

    System.out.prin tln("Please, Enter the E-Mail Address : ");
    InputStreamRead er isr = new InputStreamRead er (System.in);
    BufferedReader bfr = new BufferedReader (isr);

    try
    {
    mail= (bfr.readLine() );
    try{

    System.out.prin tln("Please, Enter the Telephone number : ");
    InputStreamRead er ism = new InputStreamRead er (System.in);
    BufferedReader bb = new BufferedReader (ism);

    tel=String.valu eOf(bb.readLine ());
    stmt.execute("i nsert into Member (Name,EMAIL,Tel ephone) values ('"+mname+"','" +mail+"','"+tel +"')");
    System.out.prin tln("Insertion has been completed");
    stmt.execute(" select * from Member");
    ResultSet rs=stmt.getResu ltSet();

    if (rs != null)
    while (rs.next()){
    System.out.prin tln("Name: "+rs.getString( "Name")+ " E-MAIL: " + rs.getString("E MAIL")+" Telephone: "+rs.getString( "Telephone" ));
    }
    try{

    System.out.prin tln("Do you want to insert more records? ");
    InputStreamRead er is = new InputStreamRead er (System.in);
    BufferedReader bbb = new BufferedReader (is);
    ans=String.valu eOf(bbb.readLin e());

    }
    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    stmt.close();
    con.close();


    }

    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    }

    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }

    }
    catch (IOException err)
    {

    System.out.prin tln("Error in reading the numbers");
    }
    }while(ans=="ye s");
    }


    catch (Exception err) {err.printStack Trace();}

    }
    }[/PHP]
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Line 94 in your code:

    [code=java]
    }while(ans=="ye s");
    [/code]

    Don't compare Strings like that; you should use the 'equals()' method. Check out
    the API docs for the String class.

    kind regards,

    Jos

    Comment

    Working...