Writing to a file

Reply

Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

Writing to a file

 
0
  #1
Jan 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Writing to a file

 
0
  #2
Jan 21st, 2009
You need to explain what you are trying to accomplish and how because your code doesn't make any sense
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Writing to a file

 
0
  #3
Jan 21st, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC