important typeCasting - Programmers Heaven

Howdy, Stranger!

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

Categories

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.