943,678 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 365
  • Java RSS
Jan 21st, 2009
0

Writing to a file

Expand Post »
Quote ...
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.util.StringTokenizer;
public class Sample {
public static int count = 0;
public static String s[]=new String[2];
public static String s2;
public static String s3;

public static void main (String args[]) throws IOException{
isRead();
}
public static void isRead()throws IOException{
int count2 = 0;

BufferedReader lnr = new BufferedReader (new FileReader("subjects.txt"));
String line;
String ask;


while ((line = lnr.readLine())!=null){
StringTokenizer st = new StringTokenizer(line);

s[count2] = st.nextToken();
count2++;

}
isInput();

}

public static void isInput() throws IOException{
FileWriter writer = new FileWriter("output.txt");
int i;
String ask = JOptionPane.showInputDialog("Enter.");

if(ask.equals(s[0])){
StringTokenizer st = new StringTokenizer(ask,s[0],true);

while(st.hasMoreTokens()){
String ask2 = st.nextToken();

if(ask2.equals("1")){
i = Integer.parseInt(ask2);

}
else if(ask2.equals("M")){
s2 = "Math17";

}


}
String semester = "\n1\n";
writer.write(semester+s2);
writer.flush();
writer.close();
isInput();
}
else if(ask.equals(s[1])){
StringTokenizer st = new StringTokenizer(ask,s[1],true);

while(st.hasMoreTokens()){
String ask2 = st.nextToken();

if(ask2.equals("2")){
i = Integer.parseInt(ask2);

}
else if(ask2.equals("I")){
s3 = "IT110";

}


}
String semester = "\n1\n";
writer.write(semester+s3);
writer.flush();
writer.close();

}




}

and my subjects.txt:
(1,Math17)
(2,IT110)


Please help me on how to fix this. It only writes the last one, which is IT110, and no Math17 appears. Was the first one that i write was saved? Or everytime i write a new one, the old one will be deleted..I hope you can help me. Thank you.
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Jan 21st, 2009
0

Re: Writing to a file

You need to explain what you are trying to accomplish and how because your code doesn't make any sense
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 21st, 2009
0

Re: Writing to a file

Do you have something personal against the code tags that after our repeated attempts to get you start using them, you still don't.

As far as the programs goes, my comments could be copy-pasted from those of javaAddict.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: setUndecorated/Dragging
Next Thread in Java Forum Timeline: queue question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC