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.
Related
I'm working on a Java Hands-On Exercise, And I ran into this problem where for some reason, It gives me this error whenever I try to call the ItemTaxCalculator class on my Java Test File
Here is the ItemTaxCalculator class
package com.mycompany.taxcalculator;
public class ItemTaxCalculator {
public double CalculateItemTax(double itemPrice, double taxPercent){
double decrease = taxPercent/10.0;
return itemPrice * decrease;
}
}
And this is the ItemTaxCalculatorTest.java
package taxcalculator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ItemTaxCalculatorTest {
public ItemTaxCalculatorTest() {
}
#Test
public void testCalculateItemTax(){
System.out.println("CalculateItemTax method");
ItemTaxCalculator instance = new ItemTaxCalculator();
double itemPrice = 50.0;
double taxPercent = 1.0;
double expectedResult = 5.0;
double result = instance.CalculateItemTax(itemPrice, taxPercent);
assertEquals(expectedResult, result, 0.0);
}
}
The error occurs on the
ItemTaxCalculator instance - new ItemTaxCalculator(); line.
An error message comes up saying:
cannot find symbol
symbol: class ItemTaxCalculator
location: class ItemTaxCalculatorTest
I can't seem to find an answer for this problem, And this is the only thing that hinders me from running the code. I hope I can find some help, cheers!
Your problem is not netbeans related.
Your original class is in package:
package com.mycompany.taxcalculator;
while your test is in package:
package taxcalculator;
The test would be able to compile and execute as is, but only if both classes were in the same package.
So, there are two ways you can fix your issue.
Put your classes in the same package
Just change one of your package statements so that it matches the other one.
Add an import statement in your test class
import com.mycompany.taxcalculator.ItemTaxCalculator;
Once you do this, the class will be found and can be instantiated.
$ 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()".
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.
I am new to JUnit.
I just started working on JUnit and i am getting following error.
The method assertEquals(String, String) is undefined for the type TestJunit
and my Javacode:
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class TestJunit {
String message = "Hello World";
MessageUtil messageutil = new MessageUtil(message);
public void testPrintMessage()
{
assertEquals(message,messageutil.printMessage());
}
}
please help me resolve this issue.
You imported
import static org.junit.Assert.assertArrayEquals;
but not
import static org.junit.Assert.assertEquals;
You could also import every static member of Assert with
import static org.junit.Assert.*;
Without these, Java thinks you are calling the method assertEquals defined in the class the code is declared in. Such a method does not exist.
if importing doesn't work try saving before you run the code
I've two classes under the same package Class names are "TestPlugin" and "Pokemon". The error I get is in the class TestPlugin at line 7 where there's written "New Pokemon". The error is "Cannot be resolved to a variable". I want the TestPlugin to acces the code in Pokemon so it can be used. What should I do to fix this problem? New to bukkit plugin creation so don't make the answer too advanced please. "I don't own this code/plugin. I've it for educational purposes only!". If you wonder what bukkit libary I'm using, it's the recommended build "craftbukkit-1.6.4-R2.0".
TestPlugin's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin{
public void onEnable(){
getServer().getPluginManager().registerEvents(new Pokemon, this);
}
}
Pokemon's code:
package com.hotmail.marrunsilkeborg.plugins.testplugin;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class Pokemon implements Listener{
#EventHandler
public void onBlockPlace(BlockPlaceEvent event){
Player p = event.getPlayer();
Block bp = event.getBlockPlaced();
p.sendMessage("You've placed a " + bp.getType().toString());
}
}
Change line 7 to
this.getServer().getPluginManager().registerEvents(new Pokemon(this), this);
also think about adding a on disable
You wanted to call Pokemon's constructor, so use
new Pokemon() with the parentheses.
As #Welsar55 mentioned, use new Pokemon(this) if you are referencing your plugin in the Pokemon constructor (common-practice for Java plugins), i.e. where your Pokemon constructor is:
public Pokemon(TestPlugin myPlugin) {
this.plugin = myPlugin;
}