943,929 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1702
  • Java RSS
Oct 21st, 2005
0

I/O : simple but not yet solved

Expand Post »
hi.
my problem is plain simple but still i couldn't make it out.
it's all very basic stringwork (keyboard input, println, ...) but when I get to the point of writing the output file, nothing happens: the text file is created but remain completely blank.
this is my code:

Java Syntax (Toggle Plain Text)
  1. import java.io.*; //Serve per l'input e l'output su file
  2.  
  3.  
  4.  
  5. public class ContoC{
  6.  
  7.  
  8.  
  9. public static void main(String args[]) throws IOException {
  10.  
  11.  
  12.  
  13. String choice, temp;
  14. FileWriter fw;
  15.  
  16. System.out.println(" CONTO CORRENTE ");
  17.  
  18. System.out.println();
  19.  
  20.  
  21.  
  22. System.out.println("Insercisci il numero del I conto corrente:");
  23.  
  24. int a=0;
  25.  
  26. BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
  27.  
  28. try {
  29.  
  30. a=Integer.parseInt(in.readLine());
  31.  
  32. }catch (IOException e){} //Input I conto
  33.  
  34.  
  35.  
  36. System.out.println("Insercisci il numero del II conto corrente:");
  37.  
  38. int b=0;
  39.  
  40. BufferedReader in1 = new BufferedReader (new InputStreamReader (System.in));
  41.  
  42. try {
  43.  
  44. b=Integer.parseInt(in1.readLine());
  45.  
  46. }catch (IOException e){} //Input II conto
  47.  
  48.  
  49.  
  50. System.out.println("Insercisci il numero del III conto corrente:");
  51.  
  52. int c=0;
  53.  
  54. BufferedReader in2 = new BufferedReader (new InputStreamReader (System.in));
  55.  
  56. try {
  57.  
  58. c=Integer.parseInt(in2.readLine());
  59.  
  60. }catch (IOException e){} //input III conto
  61.  
  62.  
  63.  
  64. System.out.println();
  65.  
  66.  
  67.  
  68. ContoCorrente c1, c2, c3;
  69.  
  70.  
  71.  
  72. c1=new ContoCorrente(a);
  73.  
  74. c2=new ContoCorrente(b);
  75.  
  76. c3=new ContoCorrente(c);
  77.  
  78.  
  79.  
  80. c1.versamento(10000);
  81.  
  82. c2.versamento(600);
  83.  
  84. c3.versamento(5000);
  85.  
  86.  
  87.  
  88. c1.prelievo(5000);
  89.  
  90. c2.prelievo(100);
  91.  
  92. c3.prelievo(2000);
  93.  
  94.  
  95.  
  96. System.out.println("Saldo CC n." + c1.n_conto() +" --> " + c1.saldo());
  97.  
  98. System.out.println("Saldo CC n." + c2.n_conto() +" --> " + c2.saldo());
  99.  
  100. System.out.println("Slado CC n." + c3.n_conto() +" --> " + c3.saldo());
  101.  
  102.  
  103.  
  104. System.out.println();
  105.  
  106. c1.stampa(a);
  107.  
  108. System.out.println();
  109.  
  110. c2.stampa(b);
  111.  
  112. System.out.println();
  113.  
  114. c3.stampa(c);
  115.  
  116. System.out.println();
  117.  
  118. // Qui inizia la parte di gestione output carattere (-> conti.txt)
  119.  
  120. System.out.println("\n\nVuoi scrivere i dati sul file «conti.txt» ?\n");
  121.  
  122. BufferedReader in3 = new BufferedReader (new InputStreamReader (System.in));
  123.  
  124. try { fw = new FileWriter("conti.txt"); }
  125.  
  126. catch(IOException exc) {
  127. System.out.println("Errore: il file non si apre!");
  128. return ;
  129. }
  130. do {
  131. System.out.print(": ");
  132. choice = in3.readLine();
  133. if(choice.compareTo("N") == 0) break;
  134.  
  135. temp = "Conto: " + c1.n_conto() + " - Saldo: " + c1.saldo() + "\r\n";
  136. fw.write(temp);
  137. temp = "Conto: " + c2.n_conto() + " - Saldo: " + c2.saldo() + "\r\n";
  138. fw.write(temp);
  139. temp = "Conto: " + c3.n_conto() + " - Saldo: " + c3.saldo() + "\r\n";
  140. fw.write(temp);
  141.  
  142. } while(choice.compareTo("N") !=0);
  143.  
  144. fw.close();
  145.  
  146. }
  147.  
  148. }

for debugging i added 3 System.out.println to check the temp string and it was ok.
the only problem seems to be writing on the file
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 21st, 2005
0

Re: I/O : simple but not yet solved

hi
if you can attach all the related files i can compile and see what happens.
Regards
Srinivas
Reputation Points: 10
Solved Threads: 0
Light Poster
cheenu78 is offline Offline
45 posts
since Jun 2005

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: File I/O
Next Thread in Java Forum Timeline: BST - need help with methods





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


Follow us on Twitter


© 2011 DaniWeb® LLC