PDA

View Full Version : java


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.

MCQ
2008-02-03, 08:28
Writing looks okay.

Reading (does that part compile?):
1. What does Process() function do?
2. The mousebuttonclicked2 function was just copied and logic straight reversed from write?

Use the BufferedReader to open your input file, read a line, set text field. Basically, what you had in the readfromfile function, except add a line to do the textfield.setText(str) after getting a string from the text file. Do it once for each text field.

kuliand2
2008-02-03, 09:31
Writing looks okay.

Reading (does that part compile?):
1. What does Process() function do?
2. The mousebuttonclicked2 function was just copied and logic straight reversed from write?

Use the BufferedReader to open your input file, read a line, set text field. Basically, what you had in the readfromfile function, except add a line to do the textfield.setText(str) after getting a string from the text file. Do it once for each text field.

1. nothing i've removed it now
2. yes i did just copy it and try to reverse it.

Ok i have got rid of alot that code as it was not working and according to Netbeans had loads of error's so im think it is easier to start from scratch instead of copying it and trying to revers it. So this is what i have now below but i still don't know hot to use the textfield.settext(str) method.

}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {

}

private void BufferedReader(String s) {
try {
BufferedReader in = new BufferedReader(new FileReader("/users/data.txt"));
String str;
while ((str = in.readLine()) != null) {
in.readLine();
in.close();
}
} catch (IOException e) {
}// TODO add your handling code here:
}

adamb
2008-02-03, 09:59
Its been a while since Ive done any java, but when writing to a file I would put each string on a new line, so you dont have to set a string delimiter, and then use
while(in.hasNext())
{
str = in.nextLine();
//set whatever = str;
}
in.close;

adamb
2008-02-03, 10:11
Ok after thinking a bit more about this, you know there are going to be 5 names and amounts so I would create an array and read from the file into it, then take the elements from the array and put them into the correct display fields:


String[][] strArray = new String[5][2]

int count = 0;

s = new Scanner(new BufferedReader(new FileReader("/users/data.txt")));

while(s.hasNext())
{
strArray[count][0] = s.nextLine(); //Store the name
strArray[count][1] = s.nextLine(); //Store the amount
count++;
}


Then you should be able to take the values from the array and put them into your text fields.

Hope that helps.

kuliand2
2008-02-07, 14:56
thanks, ye think i will go down the array method.

Im still finding this course work hard i could do it in VB as the lecturer gave us a lot of code that i could recycle but i keep hearing that no one uses VB in the real world and that java is the language in vogue.

Anyway one of my class mates has done a computer science course before and said they spent about half a term just learning the methods used and basic techniques used to solve problems i think this is what i need as i have never done any programming before and we just jumped straight into VB in lectures so can anyone recommend a website or something that starts with the very basic's? a podcast would be ideal as i learn better when listening or watching.

thaanks

adamb
2008-02-07, 16:24
Big Java (http://www.amazon.com/Big-Java-Cay-S-Horstmann/dp/0471697036/) is the book that was used when I started learning. There is a newer edition but you can get that edition which is Java 5 used there for under $20.

Its a pretty good reference as well once you get the basics down.