kuliand2
2008-02-03, 04:23
i've got to make a simple Java program but i'd like to use a swing interface to store 5 names and the amount of money they donated to a fictionally charity for my course work. In lectures we only covered basic Java for making command line apps and we only did 3 lectures on that so my knowledge is pretty limited however i have been using Google to help me and i have managed to save the data to a text file with the names and money separated by a coma but i haven't been able to read the data back and place it the text fields and formatted text fields.
So here is the code i have for saving the data to the txt. file
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
StringBuffer sb = new StringBuffer();
sb.append(""+jTextField1.getText()+",");
sb.append(""+jFormattedTextField1.getText()+"\n");
sb.append(""+jTextField2.getText()+",");
sb.append(""+jFormattedTextField2.getText()+"\n");
sb.append(""+jTextField3.getText()+",");
sb.append(""+jFormattedTextField3.getText()+"\n");
sb.append(""+jTextField4.getText()+",");
sb.append(""+jFormattedTextField4.getText()+"\n");
sb.append(""+jTextField5.getText()+",");
sb.append(""+jFormattedTextField5.getText()+"\n");
writeToFile(sb.toString());
}
private void writeToFile(String s) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("/users/data.txt",true));
out.write(s);
out.close();
} catch (IOException e) {
e.toString();// TODO add your handling code here:
}
But im having problems working out the code to read the data back and place it in the jtextfields that were used to enter it.
so here is the code i have made up to try and read back.
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
BufferedReader sb = new BufferedReader();
sb.append(""+jTextField1.setText()+",");
sb.append(""+jFormattedTextField1.setText()+"\n");
sb.append(""+jTextField2.setText()+",");
sb.append(""+jFormattedTextField2.setText()+"\n");
sb.append(""+jTextField3.setText()+",");
sb.append(""+jFormattedTextField3.setText()+"\n");
sb.append(""+jTextField4.setText()+",");
sb.append(""+jFormattedTextField4.setText()+"\n");
sb.append(""+jTextField5.setText()+",");
sb.append(""+jFormattedTextField5.setText()+"\n");
ReadFromFile(sb.toString());
}
private void ReadFromFile(String s) {
try {
BufferedReader in = new BufferedReader(new FileReader("/users/data.txt"));
String str;
while ((str = in.readLine()) != null) {
Process(str);
}
in.close();
} catch (IOException e) {
}// TODO add your handling code here:
}
As you can see it is clearly wrong but i have no idea how to fix it? Also does anyone know of a website that has basic help and tutorials for java beginners.
I can post the code for the whole app if you need but there is alot of it as Netbeans generates loads of code that i don't need but haven't removed yet.
So here is the code i have for saving the data to the txt. file
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
StringBuffer sb = new StringBuffer();
sb.append(""+jTextField1.getText()+",");
sb.append(""+jFormattedTextField1.getText()+"\n");
sb.append(""+jTextField2.getText()+",");
sb.append(""+jFormattedTextField2.getText()+"\n");
sb.append(""+jTextField3.getText()+",");
sb.append(""+jFormattedTextField3.getText()+"\n");
sb.append(""+jTextField4.getText()+",");
sb.append(""+jFormattedTextField4.getText()+"\n");
sb.append(""+jTextField5.getText()+",");
sb.append(""+jFormattedTextField5.getText()+"\n");
writeToFile(sb.toString());
}
private void writeToFile(String s) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("/users/data.txt",true));
out.write(s);
out.close();
} catch (IOException e) {
e.toString();// TODO add your handling code here:
}
But im having problems working out the code to read the data back and place it in the jtextfields that were used to enter it.
so here is the code i have made up to try and read back.
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
BufferedReader sb = new BufferedReader();
sb.append(""+jTextField1.setText()+",");
sb.append(""+jFormattedTextField1.setText()+"\n");
sb.append(""+jTextField2.setText()+",");
sb.append(""+jFormattedTextField2.setText()+"\n");
sb.append(""+jTextField3.setText()+",");
sb.append(""+jFormattedTextField3.setText()+"\n");
sb.append(""+jTextField4.setText()+",");
sb.append(""+jFormattedTextField4.setText()+"\n");
sb.append(""+jTextField5.setText()+",");
sb.append(""+jFormattedTextField5.setText()+"\n");
ReadFromFile(sb.toString());
}
private void ReadFromFile(String s) {
try {
BufferedReader in = new BufferedReader(new FileReader("/users/data.txt"));
String str;
while ((str = in.readLine()) != null) {
Process(str);
}
in.close();
} catch (IOException e) {
}// TODO add your handling code here:
}
As you can see it is clearly wrong but i have no idea how to fix it? Also does anyone know of a website that has basic help and tutorials for java beginners.
I can post the code for the whole app if you need but there is alot of it as Netbeans generates loads of code that i don't need but haven't removed yet.