I am trying to write a big system that inputs data from a text file, and has a parser file. So, do I have to write a main file that would call the parser, and if so, how would I call the parser file, just code it like this?
Parser parser = new Parser();
If not, what would be my options???? Thank you for your help :)
FOR the parser how would I specify the text file to be read on the command line. I have a
public static void main (String[] commandLineArgs)
how would I write a specific text file in this statement???? Would i replace the commandLineArgs? I forgot about this.
YourClass YourFile; // Need to fully qualified class name and full path for the file.
EDIT : If I understand correctly, you need something like this :
public class Parser {
public void parseFile (String file) {
// parsing code goes here.
}
public static void main (String[] commandLineArgs) {
Parser parser = new Parser();
parser.parseFile(commandLineArgs[0]); //
}
}
Related
I am writing a new editor for java using Xtext.
I want parser to parse the main class and replace the method call by actual code.
e.g
Class Test {
public static void main(String[] args ){
System.out.println("Virag");
method();
}
public static method(){
System.out.println("Purnam");
}
}
After parsing I want to return a document like mentioned below.
Class Test {
public static void main(String[] args ){
System.out.println("Virag");
System.out.println("Purnam");
}
}
I achieved this in lexer and parser by return of method body instead of method.
But later in editor, text region gets changed and any edit performed in editor goes wrong.
Character positions in documents are going wrong.
How to fix this problem?
You probably want to use the Code Generation concept of Xtext to transform the source code of your DSL into a new artefact where the methods' body are inlined.
I am currently dead in the water with a Java programming problem that seemed somewhat simple at first to do! I am trying to write text to a file from MULTIPLE methods in a class that does NOT contain a main() method, unlike other answers of this type question have used.
So... A quick outline of what my program is currently doing:
My program has one class (with the main() method obviously) that reads a text file stored on the disk, and passes sections of the text to certain methods in another class (second file in the project) to simply write the passed text to a text file. Each method in the class without the main() method needs to write the string passed to them to THE SAME file.
Why am I having trouble? I can easily write text to a file from ONE method in the class without the main() with FileWriter, but in order to have all of my other methods to write to the same file, I would need to make FileWriter global. I have tried to make it global, but then when I save text after another method saved text, it just rewrites the file to the latest text written.
My current class without the main() method:
package ADIFTOCSV;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class createADIF {
static File file;
static FileWriter fw;
static BufferedWriter writer;
static void init() throws IOException {
file = new File("/Users/Colin/Desktop/Myadif.txt");
fw = new FileWriter(file.getAbsoluteFile());
writer = new BufferedWriter(fw);
}
static void storeDate(String datez) throws IOException {
writer.write("<QSO_DATE:" + datez.length() + ">" + datez); <<----NULL POINTER EXCEPTION
}
static void storeFreq(String freqz) throws IOException {
writer.write("<FREQ:" + freqz.length() + ">" + freqz);
writer.close();
}
static void storeMode(String modez) {
}
static void storeBand(String bandz) {
}
static void storePower(String pwrz) {
}
static void storeTime(String timez) {
}
static void storeCall(String callz) {
}
static void storeRstSent(String rstsentz) {
}
static void storeRstRcvd(String rstrcvdz) {
}
static void storeComments(String commentsz) {
}
}
Each of these methods needs to write the String passed to them to the SAME file.
storeDate() is the first method to be called, therefore it writes text to the file first. However, when storeFreq() is called, it's text completely replaces the text written by storeDate(). This is obvious because I am forced to create a new FileWriter object in each method in order to write text. I cannot put FileWriter fw = new FileWriter(file.getAbsoluteFile()); outside the method to make it global; errors arise.
Am I missing something? Help is much appreciated. If any questions arise, or clarification is needed, please just ask!
You have to create the writer outside the methods.
Just defining the file outside is not enough.
If you recreate a writer to the same file in each method, of course it will overwrite.
The File instance is just a pointer to the file.
The writer is the "actual handle" that you need to reuse.
And be aware that you have to close the writer if you are finished with writing.
I would suggest that you scrap the class with the static methods and instead create a normal "File Write Handler" class which has a constructor where you can pass the File and writer to intialize the file writing classes and let that class handle all the writing to the file such that you can call a method like this:
FileWriteHandler.writer("<FREQ:" + freqz.length() + ">" + freqz);
and soforth for the rest you want printed. And finally call
FileWriteHandler.close();
Would be much cleaner and you could even make an interface for that class such that you can replace the FileWriterHandler with f.ex. a DatabaseWriteHandler or something like that.
What I'm trying to do is code a method that takes any kind of text input like
"words.text"
What I imagined it would look like would be
public static wordcount(File afile){....}
I want the method to be called such as
wordcount("words.txt");
I tried looking for the answer but couldn't find it. How do I do this?
Make the method with the following signature
public static void wordCount(String fileName){...}
Then inside the mthod use the string to make a File object.
public static void wordCount(String fileName){
File aFile = new File(fileName);
}
I have two classes, one does something like this:
public ClassOne:
package classes;
public class ClassOne {
public javax.swing.JTextArea progressListing
progressListing = new javax.swing.JTextArea();
public void files(File file){
Class method = new Class();
method.methodInOtherClass(files);
}
public void progressUpdate (String fileOutput){
progressListing.insert(fileOutput,0);
}
}
which then goes to the other class that has the following:
Other Class:
package classes;
public class OtherClass extends ClassOne{
public void methodInOtherClass(file){
String fileOutput
fileOutput = file.getName();
ClassOne input = new ClassOne();
input.progressUpdate(fileOutput);
}
}
It is not updating the progessListing field when the program runs. Is there a better way to do this or am I missing something?
What OtherClass does is it creates pdf files that need to show up in the text area(ie the file path with the file name). ClassOne is the swing interface. Even when it's extended into the other class it doesn't modify the text field when I need it to.
Correct me if I'm wrong, but looking at the code you have posted, I think that you are trying to read a file and put the data from the file into the text area. Again, please correct me if I'm wrong. I think you should use the following code:
BufferedReader br = new BufferedReader(new FileReader("fileName.txt");
String data = br.readLine();
jTextArea1.append("\n"+data);
Please tell me if it works.
Cheers.
PS. If you post your whole code I will be able to help better.
I figured it out. I used a getter method in the other class to assign the variable and returned it to the main class. I set the String inside the loop to have an assign and add function. Works like a charm.
I have written a grammar that allows the user to input a relative path. (e.g. "../../temp/out/path"
May aim is to get the absolute path based on the input from the user, and the absolute path of the current working directory so that I can also check if the input path is valid or not.
Is there libraries or built in functions that I can use to get the absolute path?
Something similar to C's _getcwd() function.
Yes, Java has a File class. You can create one by calling this constructor which takes a String. Then you can call getAbsolutePath() on it. You can call it like this:
package com.sandbox;
import java.io.File;
public class Sandbox {
public static void main(String[] args) {
File file = new File("relative path");
String absolutePathString = file.getAbsolutePath();
}
}
This will print a complete absolute path from where your application has initialized.
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Working Directory = " +System.getProperty("user.dir"));
}
}