Why is my watson translate service method not working? - java

I did imports of classes I'm using but it gives this message error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method translate(String, String, String) in the type LanguageTranslation is not applicable for the arguments (String, Language, Language)
at ibm.Cognitive.Translate(Cognitive.java:20)
at Teste.Watson.main(Watson.java:16)
My class:
package ibm;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import Teste.Watson;
import java.util.Map;
public class Cognitive implements Serializable{
static public String Translate(ArrayList lista){
LanguageTranslation service = new LanguageTranslation();
service.setUsernameAndPassword(lista.get(0).toString(), lista.get(1).toString());
TranslationResult translationResult = service.translate(lista.get(2).toString(), Language.ENGLISH, Language.SPANISH).execute();
return lista.get(0).toString();
}
}
How can I solve this?

Disclaimer: I have not used the API for Watson. However, judging by the error you are getting, it appears the translate service wants (string, string, string) arguments.
Fixes I would try, if I were coding this:
Use Language.ENGLISH.toString() and Language.SPANISH.toString() as args.
Simply type "ENGLISH", "SPANISH" as the args for language, thus sending string literals in to the function.

Related

Why do I get "the receiver expression should be replaced with the type qualifier"?

$ javac Main.java
Main.java:27: error: illegal static interface method call
ProcessHandle.current().allProcesses().mapToLong(w->w.pid()).forEach(System.out::println);
^
the receiver expression should be replaced with the type qualifier 'ProcessHandle'
1 error
Why do I get "the receiver expression should be replaced with the type qualifier 'ProcessHandle'"?
What shall I do instead? Thanks.
import java.lang.System;
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]){
System.out.println("visible processes pids are: ");
ProcessHandle.current().allProcesses().mapToLong(w->w.pid()).forEach(System.out::println);
}
}
What are You trying to achieve here? It doesn't seem to make much sense to ask for the current ProcessHandle and then use that to query allProcesses. Both of these are static methods. You probably want to just omit the ".current()".

scala.collection.Seq doesn't work on Java

Using:
Apache Spark 2.0.1
Java 7
On the Apache Spark Java API documentation for the class DataSet appears an example to use the method join using a scala.collection.Seq parameter to specify the columns names. But I'm not able to use it.
On the documentation they provide the following example:
df1.join(df2, Seq("user_id", "user_name"))
Error: Can not find Symbol Method Seq(String)
My Code:
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import scala.collection.Seq;
public class UserProfiles {
public static void calcTopShopLookup() {
Dataset<Row> udp = Spark.getDataFrameFromMySQL("my_schema","table_1");
Dataset<Row> result = Spark.getSparkSession().table("table_2").join(udp,Seq("col_1","col_2"));
}
Seq(x, y, ...) is a Scala way to create sequence. Seq has it's companion object, which has apply method, which allows to not write new each time.
It should be possible to write:
import scala.collection.JavaConversions;
import scala.collection.Seq;
import static java.util.Arrays.asList;
Dataset<Row> result = Spark.getSparkSession().table("table_2").join(udp, JavaConversions.asScalaBuffer(asList("col_1","col_2")));`
Or you can create own small method:
public static <T> Seq<T> asSeq(T... values) {
return JavaConversions.asScalaBuffer(asList(values));
}

TextTransfer type resolve issue

I am trying out this code from javapractices dot com and Im seeing a type resolve issue :
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
TextTransfer textTransfer = new TextTransfer(); <- ERROR
// Do other stuff
}
I see the following error. How do I resolve this? Im using Eclipse with Java 1.7.0_45
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
TextTransfer cannot be resolved to a type
TextTransfer cannot be resolved to a type
at JP_Clipboard.main(JP_Clipboard.java:20)
In the example the class name is called TextTransfer. You have renamed it to JP_Clipboard. Either use
JP_Clipboard textTransfer = new JP_Clipboard();
or rename the class back to TextTransfer (preferrable - follows Java naming conventions)
I checked your reference.. You have renamed the TextTransfer class by JP_Clipboard in your case
So your code should be:
public final class JP_Clipboard implements ClipboardOwner {
/**
* #param args
*/
public static void main(String... aArgs) {
JP_Clipboard textTransfer = new JP_Clipboard();
}
The issue is with your example Class name JP_Clipboard - this should be renamed to TextTransfer; referring to link you have provided in your post.
You need import for TextTransfer or it must be in same package.

Java - How to use a existing Enum inside class

Can someone give me an example how to use this enum. I'm trying to find out what I need to import and how I can use the methods of the following Enum:
http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.Status.html
You would need the correct package containing the enum definition. In this instance javax.ws.rs. Visit this post to know where to find it.
Can't find javax.ws.rs package in jdk
After you have added the .jar to your CLASSPATH you can simple import it
import javax.ws.rs.core.Response.Status;
Here goes one example in JSON:
public Response retrieveSomething(String uuid) {
Entity entity = service.getById(uuid);
if(entity == null) {
return Response.status(Response.Status.NOT_FOUND).entity("Entity not found for UUID: " + uuid).build();
}
String json = //convert entity to json
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}
In many respects an enum is just like z regular class; the answer is practically the for how to use an enum as how to use a class:
Step 1: import the enum to your program:
import javax.ws.rs.core.Response.Status;
Step 2: Het a reference to an instance (unlike regular classes, you can't create an instance - that is done for you by the JVM), either from the enum:
Status status = Status.OK;
or as the returned value of a method:
Status status = response.getStatus();
Step 3: Invoke a method:
int code = status.getStatusCode();
Here is a very simple example using the Status enum:
First import Response:
import javax.ws.rs.core.Response;
Then your code...
public Response create() {
return Response.status(Response.Status.CONFLICT).build();
}

Hamcrest generics hell #2 : iterableWithSize gives errror "is not applicable for the arguments"

In hamcrest (1.3.RC2, with no JUnit dependencies)
I am failing using iterableWithSize().
I have an (extension of) an Iterator parametrized with Content like this
EndResult<Content> contents = contentRepository.findAllByPropertyValue("title", "*content*");
where EndResult is
package org.springframework.data.neo4j.conversion;
public interface EndResult<R> extends Iterable<R> {...}
and Content is a my Pojo.
Now, I would think that this would work
assertThat(contents, iterableWithSize(1));
but it gives me the error :
The method assertThat(T, Matcher)
in the type Assert is not applicable
for the arguments
(EndResult< Content>, Matcher< Iterable< Object>>)
I also tried these failures :
assertThat(contents, iterableWithSize(equalTo(1));
assertThat(contents, IsIterableWithSize.<EndResult<Content>>.iterableWithSize(1));
These are my imports :
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.hamcrest.collection.IsIterableWithSize;
The hasSize for collections works as expected, but for iterators I cant even find a working example...
It should just be
assertThat(contents, IsIterableWithSize.<Content>iterableWithSize(1));
iterableWithSize is typed on the component type of your Iterable, not the concrete type of iterable itself.

Categories

Resources