|
You should really make more use of the tutorials[^] for basic questions. Java, like most languages, supports I/O from basic byte/byte-stream right through to Object serialization.
MVP 2010 - are they mad?
|
|
|
|
|
Hello Friends
I created a project in java tht Encrypts and derypts a file.But first of all I want to know Is that Possible If i use the same code in C++ way to encrypt and decrypt the same file thru java Code.
Another Question is Can I convert Directly my java code to C++ file?
Thanks & Regards
Yogesh Sikri
|
|
|
|
|
1. Given the same key, file, and algorithm then yes.
2. Depends on the implementation of the Java code.
|
|
|
|
|
Encryption in C++ is not quite as simple as in Java but it can be done. You may find articles about it either here or via Google. See also my response here[^].
MVP 2010 - are they mad?
|
|
|
|
|
In college, I was given an assignment, to use these methods to sort a list alphabetically from the args array from a cmd prompt input from the user.
I can take in and print out the list, sorted, however, when I sort it, it doesn't print correctly, eg 1 2 3 4 5 might print as 1 2 4 3 5.
My source code can be seen here:
[MySortProgram Source Code]
|
|
|
|
|
You're forgetting to reset maxSoFar, and Bad Things happen when i == 0
|
|
|
|
|
Can you explain how to "reset" maxSoFar? I thought i just gotta swap the index into temp and then change the small to that index and then the temp to the max so far?
What will happen when i reaches 0?
|
|
|
|
|
bmnot wrote: I thought i just gotta swap the index into temp and then change the small to that index and then the temp to the max so far?
Yes, that is right.
But what you're doing now is remember the index of the global max of the entire array, instead of just the part that you're supposed to be looking at. The next iteration you will need the max of a smaller part of the array, not the global max.
The Bad Things that happen at i == 0 are also partially a result of not resetting maxSoFar, it will not search for a new max (since 0 is not smaller than 0), and then swap them anyway. If you found the correct max (maxSoFar = 0 be cause it's the only one you can still scan) then swapping 0 with 0 has no effect, but you'd still have the old max, so strange things happen.
|
|
|
|
|
Okay, I understand the maxSoFar thing now, I'm a bit of a n00b to programming and REALLY appreciate this help. I'm doing it in college and have only been at it about 6 weeks.
Also, how do I get around this i = 0 problem? Cause I've noticed that when I input the strings, they end up in an order that is most definatly not alphabetically!
|
|
|
|
|
Well you could change the ending condition of the outer loop to i > 0 , that should work
|
|
|
|
|
But then would that not stop before the 1st array (list[0])?
I've done this and the list is completly messed up! I've re-upped the code, here is the link.[Source Code]
Could you compile and test it please and see if you can find what the problem is?
Also, as far as programming goes, is this code "messy"?
Thanks,
Niall
|
|
|
|
|
Now you're resetting maxSoFar too often
Oh this is not messy.. just wait until you have to implement things like chordal graph colouring...
About the first element - yes, it would stop before it. But you wouldn't move it even if you did not stop before it. The only candidate for swapping would be itself, and that's not very useful. Also you could see it like this, all following items are already correctly sorted (because of the selection-sort invariant which says that the "last" portion of the array is already perfect), and you only have 1 item left, it must already be in the right place, since it can not go anywhere else. It's not a problem if list[0] was not the smallest element in the beginning - it will be the smallest now (so apparently it got swapped already)
|
|
|
|
|
god i'm confused now!! Where should the maxSoFar go then?
haha. Can you give me some examples maybe with some code to show me? I'd much appreciate it.
Thanks,
Niall
|
|
|
|
|
Maybe so, but then I'd pretty much give you the answer wouldn't I
Seems like cheating..
On the other hand, it's on wikipedia anyway
Here is the code that does it the other way around (starting at i = 0 and finding the smallest element every time)
http://en.wikipedia.org/wiki/Selection_sort#Code[^]
|
|
|
|
|
So what's up, did you solve the problem?
|
|
|
|
|
fixing the former could fix the latter, even when nothing needs to be done for i==0
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Can a java application call a WCF service?If Yes can any one help me how to do it?It is really urgent.
I have created a WCF service in .net .
I have simple java application.
I want to use WCF service in Java application..
|
|
|
|
|
|
You posted this question in three other forums. Please use one forum only for your queries.
MVP 2010 - are they mad?
|
|
|
|
|
Hello..
I am trying to write a code in socket programming using java that would transfer audio files from 1 destination to another.
I am able to read the audio file successfully using AudioInputStream and AudioSystem methods.
However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Please can u help me?
I was able to successfully transfer a simple text file..but it just doesn't seem to work for audio files..
Thanks n regards..
here is my source code..
Server code
public class UdpSend1
{
private DatagramSocket socket = null;
private DatagramPacket recvPacket, sendPacket;
private int hostPort;
public static void main(String args[])
{
DatagramSocket socket = null;
DatagramPacket recvPacket, sendPacket;
try
{
socket = new DatagramSocket();
InetAddress hostAddress = InetAddress.getLocalHost();
File fr = new File("C:\\Taal_Theme.wav");
AudioInputStream aio = AudioSystem.getAudioInputStream(fr);
int totalFramesRead = 0;
while (socket != null)
{
int bytesPerFrame = aio.getFormat().getFrameSize();
if (bytesPerFrame == AudioSystem.NOT_SPECIFIED)
{
}
int numBytes = 1024 * bytesPerFrame;
byte[] audioBytes = new byte[numBytes];
try
{
int numBytesRead = 0;
int numFramesRead = 0;
while ((numBytesRead = aio.read(audioBytes)) != -1)
{
numFramesRead = numBytesRead / bytesPerFrame;
totalFramesRead += numFramesRead;
sendPacket = new DatagramPacket(audioBytes, numFramesRead, hostAddress, numFramesRead);
socket.send( sendPacket );
recvPacket= new DatagramPacket(new byte[512], 512, hostAddress, 0);
socket.receive(recvPacket);
System.out.write(recvPacket.getData(), 0,recvPacket.getLength());
System.out.print("\n");
}
}
catch (SocketException se)
{
System.out.println("Error in SimpleDatagramClient: " + se);
}
}
}
catch (IOException ioe)
{
System.out.println("Error in SimpleDatagramClient: " + ioe);
}
catch (UnsupportedAudioFileException e)
{
System.out.println("Error!!! "+e);
e.printStackTrace();
}
}
}
The Client code
public class UdpReceive1
{
public static void main(String args[])
{
DatagramSocket socket = null;
DatagramPacket recvPacket, sendPacket;
try
{
InetAddress hostAddress = InetAddress.getLocalHost();
FileOutputStream fout = new FileOutputStream("C:\\f.wav");
AudioInputStream aio;
socket = new DatagramSocket(4545);
while (socket != null)
{
System.out.println("HELLO!!!");
recvPacket= new DatagramPacket(new byte[512], 512,
hostAddress, 0);
socket.receive(recvPacket);
System.out.println("HELLO!!!");
byte[] str = new byte[512];
str = recvPacket.getData();
System.out.println(str);
ByteArrayInputStream bis = new ByteArrayInputStream(str);
System.out.println(bis);
aio = new AudioInputStream(bis, null, 512);
AudioSystem.write(aio, AudioFileFormat.Type.WAVE, fout);
0,recvPacket.getLength());
System.out.print("\n");
sendPacket = new DatagramPacket(recvPacket.getData(),
recvPacket.getLength(),recvPacket.getAddress(),recvPacket.getPort());
socket.send( sendPacket );
}
}
catch (SocketException se)
{
System.out.println("Error in DatagramClient: " + se);
}
catch (IOException ioe)
{
System.out.println("Error in DatagramServer: " + ioe);
}
}
}
modified on Monday, January 25, 2010 9:58 AM
|
|
|
|
|
vidzdas wrote: However, i am unable to write back the same file to the destination. The client side does not receive the audio file properly.
Perhaps you could explain exactly what happens when you try this. There is no real difference in sending a text file or an audio file from one computer to another; a file is just a stream of bytes. It may be that you should read the file as a simple BufferedInputStream rather than using the AudioSystem object.
MVP 2010 - are they mad?
|
|
|
|
|
Hey thanks..but i already tried that.
A file is created on the other computer but only garbage is displayed. The audio comes as only bytes.
Can u please suggest something else.??
|
|
|
|
|
vidzdas wrote: Hey thanks..but i already tried that.
Uh, tried what?
vidzdas wrote: A file is created on the other computer but only garbage is displayed. The audio is comes as only bytes.
I don't understand, there is nothing to display from an audio file.
MVP 2010 - are they mad?
|
|
|
|
|
|
4277480 wrote: Try this
That's what I was suggesting. I wish I had a £ for every time I have tried to get people to understand that a file is merely a stream of bytes, and transferring from one place to another without the need to understand the content is what keeps the entire world running these days.
MVP 2010 - are they mad?
|
|
|
|