Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

important typeCasting

tollitolli Posts: 44Member
hello:

in this code am adding a student to a file. but the student record has id, name, and major....i want to prevent user from adding two names of same id because id is unique :


String S;
String file;

String S2="
"+jTextField1.getText().trim();
String L=jTextField1.getText().trim();

if (S2.equals("
"))
{
JOptionPane.showMessageDialog(this,"Insert Student's Information!");
return;
}
file="students.txt";

try{
BufferedReader in = new BufferedReader ( new FileReader(file));
while ((S=in.readLine())!=null)
{
if (S.equals(L))
{
JOptionPane.showMessageDialog(this,"This Student Already Exists: Cannot Add twice!");
return;
}
}
in.close();
BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
out.append(S2);
out.close();

}

catch (IOException e) {
JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
}


how do i typecast S into Type Student in order to use the class information and compare....????

help me plzzz

Comments

  • zibadianzibadian Posts: 6,349Member
    : hello:
    :
    : in this code am adding a student to a file. but the student record has id, name, and major....i want to prevent user from adding two names of same id because id is unique :
    :
    :
    : String S;
    : String file;
    :
    : String S2="
    "+jTextField1.getText().trim();
    : String L=jTextField1.getText().trim();
    :
    : if (S2.equals("
    "))
    : {
    : JOptionPane.showMessageDialog(this,"Insert Student's Information!");
    : return;
    : }
    : file="students.txt";
    :
    : try{
    : BufferedReader in = new BufferedReader ( new FileReader(file));
    : while ((S=in.readLine())!=null)
    : {
    : if (S.equals(L))
    : {
    : JOptionPane.showMessageDialog(this,"This Student Already Exists: Cannot Add twice!");
    : return;
    : }
    : }
    : in.close();
    : BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
    : out.append(S2);
    : out.close();
    :
    : }
    :
    : catch (IOException e) {
    : JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
    : }
    :
    :
    : how do i typecast S into Type Student in order to use the class information and compare....????
    :
    : help me plzzz
    :
    :
    Because you are trying only to check a single field you don't need to typecast the Strudent into a String, but an Integer to a String. See the Integer object for that.
Sign In or Register to comment.