LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2007, 01:09 PM   #16
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Question


Let me now include something that i didnt say before. I am also trying to make my records (the Store class) to be read in a kind of circular way, meaning that when someone presses the next record button the next record will be displayed and if the counter has reached the maximun should start from zero.
Therefore i have set in my App class a
Quote:
private static int CQCounter = 0;
private static final int MAXSTORE=10;
My first problem while trying to make this work was that the following class which is also included in my App class
Quote:
private int checkReset(int x)
{
return x % MAXStore;
}
I know this algo from data structures. Unfortunately that was not working and i tried to put some other code in my actionPerformed method which finally worked.
Any ideas why?

Thanks for your help
 
Old 03-11-2007, 02:06 PM   #17
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I'm not quire sure what you're asking here, but when you say a "circular pattern", is each record stored in its own file?

Also, check the syntax; by Java 5.0 Standard (I happen to have the language conventions right next to me), MAXSTORE is properly defined - all constants, as denoted by the "final" declaration, should be capitalized. However, when you're referencing it in checkReset(), you have it spelled out MAXStore. Try changing it to MAXSTORE in checkReset(), and see if that fixes anything.

Quote:
Originally Posted by manolakis
Thanks for your help
You're very welcome.
 
Old 03-11-2007, 02:09 PM   #18
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Also, try reversing the order when using the modulo (%) operator. Try it with:
Code:
return MAXSTORE % x;
 
Old 03-11-2007, 03:26 PM   #19
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Ok, back again to the problem that i can not write to file. I just want to ask how this could be possible as soon the Store class implements a Serializable interface. The following is how i save to the file. Can you identify anything that could causing a problem to write to the file?

Quote:
private void saveToFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );

int result = fileChooser.showSaveDialog( this );

if ( result == JFileChooser.CANCEL_OPTION )
return;

File fileName = fileChooser.getSelectedFile();
if ( fileName == null || fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE );
else {
try {
output = new ObjectOutputStream( new FileOutputStream( fileName ) );
output.writeObject(storage);
output.flush();
output.close();
}
catch ( IOException ioException ) {
JOptionPane.showMessageDialog( this, "Error Opening File",
"Error", JOptionPane.ERROR_MESSAGE );
}
}
}
Please notice output.writeObject(storage);That's how i write a Store object to the file
 
Old 03-11-2007, 05:22 PM   #20
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I've tidied it up a little bit. And outlined any major syntax changes in red.
Code:
private void saveToFile() {
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

  int result = fileChooser.showSaveDialog(this);
  if (result == JFileChooser.CANCEL_OPTION) return;
  else {
    File fileName = fileChooser.getSelectedFile();
    if (fileName == null || fileName.getName().equals( "" )) {
      JOptionPane.showMessageDialog(this, "Invalid File Name",
              "Invalid File Name", JOptionPane.ERROR_MESSAGE);
    } else {
      try {
        output = new ObjectOutputStream(new FileOutputStream(fileName));
        output.writeObject(storage);
        output.flush();
        output.close();
      } catch (IOException ioException) {
        JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
      }
    }
  }
}
I'm sorry I'm not able to put much into this right now (I'm working on some school projects).

Are you using an IDE of any sort, or just a basic text editor?

Last edited by indienick; 03-11-2007 at 05:24 PM.
 
Old 03-11-2007, 06:26 PM   #21
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Dear Sir indienick
Dont know how to thank u for all of your replies. Thanks a lot.
I 've got something new . I just had a look again to the file that i am trying to write. I open the file with a text editor like 'kwrite'. The fact is that i can see the information that i have stored. Nothing seems wrong. On the other hand i still cant re-open the file after a later execution of my program neither your program returns any output.
Something also that i have noticed is that when i first created the text file after execution of my program when i tried to open the file it asks me always with which program to open. I then choose a text editor like kwrite.
Once more obliged if i have your opinion

Thanks for sharing you knowledge and your time.
manolakis
 
Old 03-11-2007, 08:58 PM   #22
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
You are very welcome, manolakis.
That's what I'm here for.

Well, let's see...if you can open the file and view all the contents - and they're showing up correctly - the writing part is working just fine.

Now, the reading part is what's throwing us flak.

If your solution, and my solution don't give us what you want to see - and I do know that both of our solutions do work (I've tested them) - it could be a problem with the Java compiler that you're using (which while a very legal culprit, we'll dismiss it for now), it could also be the way you are handling the I/O of the file itself.

1. How are you creating the file?
--My suggestion would be to use the createNewFile() method, which comes from the java.io.File class.

2. Are you closing the output stream once you are done with it?
--Using the close() method, which is inherent from the java.io.PrintStream class.
--Although Java does run in a VM - and this could be a totally bogus stream of thought - if you aren't using the close() method when you're done with the file, or before you issue System.exit(n), an outbound stream may be "remembered" by the VM, and upon the next execution of the program, the VM may try to re-assign the stream to the program as an outbound stream.

Moreso, on the second point for the second question, I highly doubt you can have a single file being acted upon by both an in-stream and an out-stream - I/O conflicts may arise. Try explicity closing any directional streams on a file before performing a stream action in the opposite direction. Java may have a class to handle bi-directional streams, and I'll look it up in the meantime. But until then, make sure you explicitly open and close all I/O streams.

EDIT: I just have to ask, are you using SUN Java (preferred), or GCJ (GNU Java)?

Last edited by indienick; 03-11-2007 at 09:00 PM.
 
Old 03-12-2007, 03:49 PM   #23
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Could you please give me a short example explaining how to close the files explicity.
I am just wondering that if closing the files could cause the problem if i wont try to close them then it should be ok. Is that right?
 
Old 03-13-2007, 02:40 PM   #24
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Hey again
Well, I am glad to say that i made my gui working with other classes for reading and writing to files (BufferedWriter,BufferedReader).
Now I would like once more again your precious help on another issue.
I want to make the gui working for a Multi-Server. What I have planned to do
is to put the store class into the Server class as soon it should hold the only copy and the GUI into the Client class. The Clients should be able to send packets ('Employees' in my case)to the Server and then the Server should store that information.
I know that to do that i need to put things in a thread. I have also found enough src codes on the web of multi-servers but i find my self quite insecure on how to do that

Thanks for your time and for sharing your knowledge
 
Old 03-15-2007, 08:25 PM   #25
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I'm sorry about the lack of response lately, but I've been out of town for Spring Break.

I'm glad you got the data storage method(s) working.
What I mean by when I say "explicitly closing streams" is using the java.io.PrintStream.close() method (for output), and java.util.Scanner.close() (for input). It's a simple practice, and while it means an extra line of code here and there, it's not hurting anyone, and nearly completely safe-guarding your thread interactions.

Now, funny you should ask about transmitting data - because I'm actually doing the same thing right now, with Java. I've deduced it to this:
1. You need to use either java.net.Socket - or java.net.SSLSocket - for the connection to the server.
2. You need to make use of an outstream class (like java.io.PrintStream) for sending data over the socket.
3. You need to have a listener on that socket in case the server is transmitting data back to you. (In this case, you would need to design your program to act as a server, which could make your job much harder than it has to be.)
4. Encryption/decryption algorithms: to tighten up your data and keep it safe from prying eyes. (This is completely optional, and more than likely not needed if you're just doing this as a mini-project with no sensitive information being sent, or if you decide to utilize the SSLSocket class. Also, if you are using cryptographic algorithms that Java does not supply, make sure your remote system can the properly methods and algorithms to decrypt your data.)

You would only need to use a java.lang.Thread class if - and only if - you wished to do concurrent processing. The only time I could think to make use of a Thread class is if you are sending more than one set of data to more than one remote location.

Last edited by indienick; 03-15-2007 at 08:26 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String Input & Array Comparison Problem azucarmom Programming 2 03-13-2005 07:23 AM
Java: Threading, GUIs, etc. Matir Programming 1 03-06-2005 08:28 PM
Install Problem - GUIs not available richardd70 Linux - Newbie 9 09-05-2003 05:28 AM
libpng+python problem -> guis not starting Val3ntin Linux - Software 4 04-07-2003 05:01 PM
DB2 & Java problem/ Please HELP! Bogdan Linux - Software 0 05-06-2002 02:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration