I know C++ at a decent level and I am trying to learn java. This will be a silly question but I cannot figure out how to import a .java file into another. I am at Eclipse IDE and in my project I have two files:
FileReader.java
Entry.java
I want to import the Entry.java in the other file but no matter what I do I get an error. Can you help me? Thx in advance.
FileReader.java :
import java.io.*;
class FileReader {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World");
Entry a(10,"a title","a description");
a.print();
}
}
Entry.java:
public class Entry{
int ID;
String title;
String description;
public Entry(int id, String t,String d){
ID=id;
title=t;
description=d;
}
public void print(){
System.out.println("ID:"+ID);
System.out.println("Title:"+title);
System.out.println("Description:"+description);
}
}
At this state I get an error that Entry cannot be resolved as a variable. So I believe that it is related to the import.
Firstly
Entry a(10,"a title","a description");
should be
Entry a = new Entry (10,"a title","a description");
If Entry is in the same package then you will not need to import it.
If Entry is in a different package, say com.example then you will need to do
Either
import com.example.Entry;
or
import com.example.*;
The second import will import all classes in the com.example package - usually not such a good thing.
You need new Entry
The new keyword creates the new object
Entry a = new Entry(10,"a title","a description")
a.print();
An Entry object is created with the a reference with the above instantiation.
For the import part of your question, if two files are in the same package, no import is needed. If you Entry class was in a different package than your FileReader class, then you would need to import mypackage.Entry
Try
Entry a = new Entry(/*args*/);
And if you need to import the class, then use the absolute name (package+class) and put it after import above the class declaration
import com.example.you.Entry;
In Eclipse you can do Ctrl+Shift+O to resolve all imports.
Related
Below, I am trying to change the value of the Path object there using the setSoundPath() method. I cannot find any documentation to say this is possible.
I am trying to create a class that will create a copy of a file at a specified path and put the copy in the specified folder. I need to be able to change the name of the path though, because I want to create the sound object with an initial placeholder file path.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.io.IOException;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleStringProperty;
class Scratch {
public static class Sound extends Object{
private Path there;
StringProperty tests = new SimpleStringProperty(this, "test", "");
public Sound(){
this.there = Paths.get("C:\\Users\\HNS1Lab.NETWORK\\Videos\\JuiceWRLD.mp3");
}
public void setSoundPath(String SoundPath) {
this.tests.setValue(SoundPath);
this.there = Paths.get(this.tests.toString());
}
}
public static void main(String[] args) {
Sound test = new Sound();
test.setSoundPath("C:\\Users\\HNS1Lab.NETWORK\\Music\\Meowing-cat-sound.mp3");
test.copySound();
System.out.println("Path: " + test.getSoundPath().toString());
}
}
They are immutable:
Implementations of this interface are immutable and safe for use by
multiple concurrent threads.
(from: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html)
You can create new Path objects that point to your path.
Hi I am a novice in JAVA. I have been getting this file not found exception inspite of the file existing in the very location I have specified in the path which is
Initially I had the issue of file not found. However, after performing a clean and re-run, now I am having an issue which says
Error: Could not find or load main class main.main
import Message.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
public class main{
public static void main(String[] args) {
Message msg=new Message("bob","alice","request","Data####");
MPasser passerObj=new MPasser("C:\\Workspace\\config.txt","process1");
}
}
Also in the MPasser Constructor the following piece of relevant code is there
public class MPasser(String file_name,String someVariable){
InputStream input;
try {
input =new RandomAccessFile(file_name,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Yaml yaml = new Yaml();
Map<String, String> Object = (Map<String, String>) yaml.load(input);
}
Sorry I have made edits from initial query so that it is more clear
On this line:
input = RandomAccessFile("C:\Workspace\conf.txt",'r');
You need to escape the \'s
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
"C:\Workspace\conf.txt"
Those are escape sequences. You probably meant:
"C:\\Workspace\\conf.txt"
You also appear to call it config.txt in one snippet and conf.txt in the other?
Make sure the java process has permissions to read the file.
You have to escape the backslash.
input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and also
input = new RandomAccessFile("C:\\Workspace\\conf.txt",'r');
and why you have two different filename conf.txt and config.txt.
I'm trying to use the wordalignment in the BerkeleyAligner.jar file from http://code.google.com/p/berkeleyaligner/ in my own java class.
I have already added the .jar file into my buildpath.
What parameters does the edu.berkeley.nlp.wordAlignment.combine.CombinedAligner take?
What does the edu.berkeley.nlp.wordAlignment.combine.CombinedAligneroutput?
What i have are 2 input files that are already sentence aligned; i.e. the sentence from line number X from the sourceFile is the same (but in a different language) as the sentence from line number X of the targetFile.
import edu.berkeley.*;
import edu.berkeley.nlp.wa.mt.Alignment;
import edu.berkeley.nlp.wa.mt.SentencePair;
public class TestAlign {
BufferedReader brSrc = new BufferedReader(new FileReader ("sourceFile"));
BufferedReader brTrg = new BufferedReader(new FileReader ("targetFile"));
String currentSrcLine;
while ((currentSrcLine = brSrc.readLine()) !=null) {
String currentTrgLine = brTrg.readline();
// Reads into BerkeleyAligner SentencePair format.
SentencePair src2trg = new SentencePair(sentCounter, params.get("source"),
Arrays.asList(srcLine.split(" ")), Arrays.asList(trgLine.split(" ")));
// How do i call the BerkeleyAligner??
// -What parameters does the CombinedAligner takes?
// -What does the function/class returns?
// I assume it returns a list of strings.
// Is there a class in BerkeleyAligner to read the output?
// Please provide some example, thank you!!
Alignment output = edu.berkeley.nlp.wordAlignment.combine.CombinedAligner
.something.something(currentSrcLine, currentTrgLine);
}
}
e.g. sourceFile:
this is the first line in the textfile.
that is the second line.
foo bar likes to eat bar foo.
e.g. targetFile:
Dies ist die erste Textzeile in der Datei.
das ist die zweite Zeile.
foo bar gerne bar foo essen.
Actual Answer
You just wanted to align text (from a target file and a source file), right?
If so, after creating a sentence pair, you did not even need to put them in a CombinedAligner.
You could get an Alignment: (SentencePair, boolean) from that. The boolean is if you want a tree alignment.
Putting it into the constructor will generate an Alignment automatically!
So simple!
This is where I got the code: http://code.google.com/p/berkeleyaligner/source/browse/trunk/src/edu/berkeley/nlp/wa/mt/Alignment.java
UPDATE
Unfortunately, I misunderstood your question, and posted an irrelevant response.
However, I downloaded the jar file, found CombinedAligner.class, and decompiled it.
Here's what I got:
package edu.berkeley.nlp.wordAlignment.combine;
import edu.berkeley.nlp.mt.Alignment;
import edu.berkeley.nlp.mt.SentencePair;
import edu.berkeley.nlp.wordAlignment.PosteriorAligner;
import edu.berkeley.nlp.wordAlignment.WordAligner;
import fig.basic.Fmt;
import fig.basic.ListUtils;
import java.util.ArrayList;
import java.util.List;
public abstract class CombinedAligner extends PosteriorAligner {
private static final long serialVersionUID = 1;
WordAligner wa1;
WordAligner wa2;
public CombinedAligner (WordAligner, WordAligner)
public String getName()
public Alignment alignSentencePair(SentencePair)
public List alignSentencePairReturnAll(SentencePair)
public void setThreshold(int)
abstract Alignment combineAlignments(Alignment, Alignment, SentencePair)
}
It seems that the Alignment class you're using is edu.berkeley.nlp.mt.Alignment.
Anyway, CombinedAligner is abstract, so you can't instantiate it. And I don't know what the .something's are, because there is no static method or field.
I think that what you want, however, is alignSentencePair(SentencePair).
To get this, you need to use a subclass of CombinedAligner, as CombinedAligner is abstract.
So, after poking around the files, I found these subclasses:
edu.berkeley.nlp.wordAlignment.combine.HardUnion
edu.berkeley.nlp.wordAlignment.combine.HardIntersect
edu.berkeley.nlp.wordAlignment.combine.SoftUnion
edu.berkeley.nlp.wordAlignment.combine.SoftIntersect
You can use these instead of CombinedAligner and insert your two sentences as a SentencePair!
After checking, I realized that WordAligner is also abstract!
package edu.berkeley.nlp.wordAlignment;
import edu.berkeley.nlp.mt.Alignment;
import edu.berkeley.nlp.mt.SentencePair;
import fig.basic.LogInfo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public abstract class WordAligner implements Serializable {
private static final long serialVersionUID = 1;
protected String modelPrefix;
public WordAligner ()
public abstract String getName()
public void setThreshold(double)
public Alignment alignSentencePair(SentencePair)
public Map alignSentencePairs(List)
public Alignment thresholdAlignment(Alignment, double)
public String getModelPrefix()
public String toString()
}
I found a subclass, though:
edu.berkeley.nlp.wordAlignment.IterWordAligner
Unfortunately, this is still abstract.
But there's a subclass of IterWordAligner that isn't:
edu.berkeley.nlp.wordAlignment.EMWordAligner
However, the constructor is really weird.
public EMWordAligner (SentencePairState$Factory, Evaluator, boolean)
It uses an INNER CLASS in the CONSTRUCTOR!? That's terrible programming practice.
WAIT...
I found a word aligner!
http://code.google.com/p/tdx-nlp/source/browse/trunk/pa2/java/src/cs224n/assignments/WordAlignmentTester.java?r=67
Maybe that helps and you can resolve your problem with it.
I have written a program that checks a data set and provides a result, i.e. if a climate condition is given for 1000 days as data set to the program it will find any deviation in the program and provide as result that major deviation.
package main;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import faster94.*;
import rules_agarwal.*;
import algo_apriori.*;
import context_apriori.*;
import itemsets.*;
public class MainTestAllAssociationRules {
public static void main(String [] arg){
ContextApriori context = new ContextApriori();
try {
context.loadFile(fileToPath("ds1.txt"));
}
catch(Exception e)
{
e.printStackTrace();
}
/*catch (IOException e) {
e.printStackTrace();
}*/
context.printContext();
double minsupp = 0.5;
AlgoApriori apriori = new AlgoApriori(context);
Itemsets patterns = apriori.runAlgorithm(minsupp);
patterns.printItemsets(context.size());
double minconf = 0.60;
AlgoAgrawalFaster94 algoAgrawal = new AlgoAgrawalFaster94(minconf);
RulesAgrawal rules = algoAgrawal.runAlgorithm(patterns);
rules.printRules(context.size());
}
public static String fileToPath(String filename) throws UnsupportedEncodingException{
URL url = MainTestAllAssociationRules.class.getResource(filename);
return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
}
}
The above is the main program. There are seven files and I have created by own package, but when I run this program as a whole I cannot run it. It complains that a package is missing. i have ready provided all the seven files.
Can any one be able to run those files?
Directory tree has to reflect package tree.
So if you have a class in a package named main you class file must be in a directory named main under the working directory. So if you execute from bin/ your class must be in bin/main.
Hope this helps
Edit
The directory tre has to look like this.
bin/
-----faster94/
--------------Classes or Subpackage
-----rules_agarwal/
-------------------Classes or Subpackage
-----algo_apriori/
------------------Classes or Subpackage
-----context_apriori/
---------------------Classes or Subpackage
-----itemsets/
--------------Classes or Subpackage
-----main/
----------MainTestAllAssociationRules and other classes or subpackages
To run this use java main.MainTestAllAssociationRules in the root (bin/) directory
Using a sample from xSocket which will run xSocketHandler as a new process, I want to customize and moving all of these code into other java file, can I copy public class xSocketDataHandler implements IDataHandler and paste into different filename say main.java?
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import org.xsocket.*;
import org.xsocket.connection.*;
public class xSocketDataHandler implements IDataHandler
{
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException
{
try
{
String data = nbc.readStringByDelimiter("\0");
//nbc.write("Reply" + data + "\0");
nbc.write("+A4\0");
if(data.equalsIgnoreCase("SHUTDOWN"))
xSocketServer.shutdownServer();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return true;
}
}
No, you can't do that without reducing the visibility of xSocketDataHandler to default. If you don't want to do that, your file name should be xSocketDataHandler.java
You must be having class xSocketDataHandler in a file of the same name already since it is public. You could move other non public classes in this file to Main.java instead.
A public class will need to be in a file named according to the class, so in this case it would be xSocketDataHandler.java.
Convention is also to name java classes starting with an upper-case letter, so it would be public class XSocketDataHandler and file XSocketDataHandler.java. This isn't required, though.