This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why won't it work?

 
sigfrid sai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I want to ask what's the problem with my program..
My program is about stacks, there's no error in it, but my problem is, our requirement is do all operations in stacks and then, even if you close the program, the next time you will open it, the previous data should still be displayed..
I used the filewriter and filereader, but every time I close it, then open it again, the previous data doesn't appear..
Any advice?
thanks.. here's my program.
 
Stephan van Hulst
Bartender
Pie
Posts: 6081
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, refactor your code to use a separate ActionListener for each button.

Don't let your class implement ActionListener. Instead do something like this:
It will be much easier for you to find your problem.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Sigfrid!

Champ, I didn't test your program, but, at a first glance, the first thing that comes to my mind is, on line 41, when you are saving the content, try invoking out.flush() before invoking out.close() and let's see if it works.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:First of all, refactor your code to use a separate ActionListener for each button.

Don't let your class implement ActionListener. Instead do something like this:
It will be much easier for you to find your problem.


Agreed.
 
Stephan van Hulst
Bartender
Pie
Posts: 6081
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and welcome to CodeRanch!
 
sigfrid sai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Oh and welcome to CodeRanch!


thanks man,.
 
sigfrid sai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do I have to call the saveMe() and readMe() in every button?
if so, It still won't work.. sorry, I keep asking lots of question..
here's the code

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11905
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do you think your readMe method is called? Perhaps you could verify that (some simple debug statements (System.out.println will do) will verify if it is called at the right time (or ever)).
 
sigfrid sai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Monkhouse wrote:When do you think your readMe method is called? Perhaps you could verify that (some simple debug statements (System.out.println will do) will verify if it is called at the right time (or ever)).


I did what you said,.. the readMe method is called whenever i click a button..
then the saveMe method is called after I supplied what is needed.. I put a message dialog box to inform whenever these two methods are called, so it's a sure that they were called,. cause when i run the program, just like I said, they appeared before and after...
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11905
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, wrong problem.

Looking at the "readMe" method - how many lines are in the file written to disk? How many lines are you trying to read?

What is the format of the file? Is the first number zero-indexed or ???

What happens if there are more numbers in the file than can be stored in the stack?
 
sigfrid sai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Monkhouse wrote:My apologies, wrong problem.

Looking at the "readMe" method - how many lines are in the file written to disk? How many lines are you trying to read?

What is the format of the file? Is the first number zero-indexed or ???

What happens if there are more numbers in the file than can be stored in the stack?


I'm sorry, I don't understand your question..
in the notepad, there's only one line..
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11905
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sigfrid sai wrote:
Andrew Monkhouse wrote:Looking at the "readMe" method - how many lines are in the file written to disk? How many lines are you trying to read?

in the notepad, there's only one line..

Each pair of questions I asked is related to a different problem. But just on this one question, go back and look at your readMe method. Where is your call to "nextLine"? For that matter, where is the "id" variable defined? Where is your loop? Again - you might want to put debug statements in your method and in your loop and see what is being called when, and what the results are.

I left out some more important questions too - what are the constructors for the Scanner class? Is there a better constructor for you to use?

I know that this seems like we are only inching our way to a solution, but trust me - if you can get answers to these questions, you will be a long way to solving this (and future) problems yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic