| Author |
Translating c++ into java
|
James Stanford
Greenhorn
Joined: Mar 01, 2003
Posts: 4
|
|
I'm not very good at writting in java so I wrote this code in c++. but now I need to convert it into java. Can someone help me do this? [code] #include <iostream.h> #include <fstream.h> #include <stdlib.h> #include <string.h> #include <stdio.h> int main(int argc, char *argv[]){ if (argc !=2){ cerr << endl << "Error: syntax for command should be: " << argv[0] << " filename\n\n"; exit(1); } int i = 0, elements = 0; char *ch; char *buffer[13]; // Open file specified by argument one ifstream fin(argv[1], ios::in); if (!fin || fin.bad() || fin.eof() ){ cerr << endl << "Error: file not found\n\n" ; exit(1); } // Get file size cout << "Attempting to read file length... "; fin.seekg(0,ios::end); i = fin.tellg(); fin.seekg(0); // Allocate memory for file content ch = new char [i]; // read contents of infile into buffer while(!fin.eof()){ fin.getline(ch, i); buffer[elements] = ch; cout<< "ch [" << elements << "]:" << ch << endl; cout<< "buffer [" << elements << "]:" << buffer[elements] << endl; elements++; } cout << "\nNumber of elements in file: " << elements << "\n"; // display file contents for (i = 0; i < elements; i++) cout << "Element [" << i << "]: " << buffer[i] << endl; delete[] ch; cout << "\nData read succesfully.\n\n" ; fin.close(); return 0; } [\code]
|
I knew I was a vim addict when I started editing my emacs with vim
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
Hi James, Well, I could do this for you, but neither of us would feel good about it in the morning. If you really need to translate this into Java, then your really need to learn Java. If you are proficient in C++, it ain't that hard. To get you started, I'll translate one line of code for you: C++ JAVA Note that in Java argv[0] is not the name of the main entry file as in C++. You will need to change <main_class_name> to the name of your Java app.
|
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
|
 |
James Stanford
Greenhorn
Joined: Mar 01, 2003
Posts: 4
|
|
I kinda know java, in fact I knew the line you translated, my problem is that I don't have much experiance with java and I keep getting stuck at every little syntax error and don't know what to do. I guess I'll try mostly translating it and then get help w/ it. All right here we go. here is an example of what I'm trying to read: GA D A Goal L A2 A1 A4 Z ZA Ezea How would you read in these words into an array specifided by the command line in java? [ April 07, 2003: Message edited by: James Stanford ] [ April 07, 2003: Message edited by: James Stanford ]
|
 |
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
|
|
Hi James, BTW, welcome to JavaRanch.
How would you read in these words into an array specifided by the command line in java?
Do you want to have all that on the command line or do you want to read it in from a file? You're still thinking as a procedural programmer instead of an OO programmer. Here are a few points in your code: A String[] array is more suited for this than char[] array. Strings are not null terminated char arrays in Java. They are immutable objects.You need a class constructor that takes an int as a parameter., at least according to your code.The hashArray() method must have a return type, presubably void in your case.The hashArray() method should probably take a String (or possibly File) as a parameter so you can pass in argv[0] from main. Take a look at that, and see if you can at least get the code to compile. Then we'll move on to the next step.
|
 |
 |
|
|
subject: Translating c++ into java
|
|
|