I'm having a little bit of a play around with the Java Robot function.
However as i think it's a major pain in the backside i was wondering if i could abstract the function and create a faster way.
Now Firstly i have done the following:
public abstract class Cyborg {
public static void Cyborg(KeyEvent args[]) throws AWTException{
try {
Robot robot = new Robot();
for(KeyEvent k:args){
robot.keyPress(KeyEvent.k);
}
} catch(AWTException e){
e.printStackTrace();
}
}
}
Which i am currently a little baffled with i am currently getting an error that k cannot be resolved. However in the KeyEvent args[] section should this be classed as a KeyEvent or should this just be a string? As i am from a PHP background i am starting to get to grips with java. Can anyone advise if this is the best way to go about this? And also as to why KeyEvent.k won't resolve whether it is a KeyEvent or if it is a String? Any other advise on if this will work/won't work or issues to try and avoid would be great.
Thank you
robot.keyPress(KeyEvent.k);
This won't work, because this will attempt to find a static member of the class KeyEvent called k. You've named your incremental object k, so use k.
robot.keyPress(k.getKeyCode());
More thorough Explanation
When you say the following:
String[] strs = {"hello", "there", "my", "friend"};
for(String str : strs)
{
// Do something.
}
What you're making the computer do has the same effect as:
for(int x = 0; x < strs.length; x++)
{
String str = strs[x];
}
So you're defining a String object named str that will contain the current String object that is contained within the strs array.
This line
robot.keyPress(KeyEvent.k);
Should be like this
robot.keyPress(k.getKeyCode());
Related
I want to create a special list data structure that works like an array, in that it's like a list with values x[0], x[1], ... Any advice would be much appreciated.
I know all of my code isn't perfect I just want to figure out how to fix the one problem I've outlined below. This is some of the code I have:
public class SpecialList {
int[] specialList;
int lengthList;
public SpecialList(int x[]) {
this.lengthList = x.length;
this.specialList = new int[lengthList];
this.specialList = x;
for (int i=0; i<lengthList; i++) {
this.specialList[i] = x[i];
}
}
public SpecialList(SpecialList w) {
this.specialList = w.specialList;
}
public SpecialList doSomething(SpecialList y) {
int len = y.lengthList;
//The line below is an example to show the error I get
System.out.println(y[0]);
//Do some other stuff to the list y
return y;
}
//I test the code with this
public static void main(String[] args) {
SpecialList y = new SpecialList(new int[] {14, 17, 30});
SpecialList z = x.doSomething(y);
}
However I get the error 'array required, but SpecialList found' when I try to do stuff with y[i] like with System.out.println(y[0]); line of code.
'lengthList' works but getting the individual values of y[i] , the list doesn't. I cant work out whats wrong with my constructor for it to not work how I want it to.
You can't redefine what [n] means based on what object it's applied to; that's an array-specific notation. So y[0] where y is a SpecialList instance just won't work. If it could work, List (or at least ArrayList or other implementions where direct addressing is cheap) would probably have that feature.
Although you can do that in some other languages, you can't in Java. It's just not a feature Java offers. (Which is a good thing or a bad thing depending on your point of view...) Instead, you'll have to provide get and set methods or similar, just like List does.
I have an assignment for a class that has to do with implementing arraylists to work with sounds in java. I have tried to write the method but keep getting this error:
java.lang.ArrayIndexOutOfBoundsException:-254 (in java.util.ArrayList)
I am new to programming so I may have missed something quite simple. Anyway, here is my current method:
public void amplify (double amt) {
for(Integer i : myData){
myData.set(i, (int)(myData.get(i) * amt));
}
}
Thank you. Any help would be appreciated.
Try to use this you just create a local variable j and when you want to set the ArrayList use it like a index like below:
public void amplify (double amt) {
int j =0;
for(Integer i : myData){
myData.set(j, (int)(myData.get(i) * amt));
j++;
}
}
I am trying to run this program but I cannot, the compiler is sending me a ".class" error.
Can somebody help me with my problem and if it is possible a general tip about ".class" error?
Here is the program:
import java.io.*;
class Bus
{
private int kostos;
private int plithos;
private int typepiv;
Bus(int x,int y,int z)
{
kostos=x;
plithos=y;
typepiv=z;
}
public void KB(int[] x)
{
try{
for(int i=1;i<5;i++)
{
if(typepiv==2)
{
plithos=plithos+plithos/2;
kostos=kostos-kostos/2;
}
if(typepiv==3)
{
plithos=plithos-plithos/5;
kostos=kostos-kostos*25/100;
}
if(typepiv==1)
{
plithos=plithos;
kostos=kostos;
}
x[i]=plithos*kostos;
}
} catch(Exception ex){
ex.printStackTrace();
}
}
}
class testBus
{
public static void main(String args[])
{
String leof[]=new String[4];
int leof1[][]=new int[4][3];
for(int i=1;i<5;i++)
{
System.out.println("dwste onoma leoforiou");
leof[i]=UserInput.getString();
System.out.println("dwste kostos thesis enilika");
leof1[i][1]=UserInput.getInteger();
System.out.println("dwste plithos thesewn");
leof1[i][2]=UserInput.getInteger();
System.out.println("dwste tupos epibath gia enilikes=1,gia
paidia=2,gia suntaksiouxous=3");
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
----->leof2.KB(KostEnoik);
System.out.print("onoleo");
System.out.print(" ");
System.out.print("plithos");
System.out.print(" ");
System.out.print("kost(EURO)");
System.out.print("typepiv");
System.out.print(" ");
System.out.print("apotelesma kostEnoik");
for(int g=1;g<5;g++)
{
System.out.print(leof[g]);
System.out.print(leof1[g][2]);
System.out.print(leof1[g][1]);
System.out.print(leof1[g][3]);
System.out.print(KostEnoik[g]);
}
}
}
the compiler message says :
testBus.java:56:error:cannot find symbol
leof2.KB(KostEnoik);
symbol:bariable leof2
location:class testBus
1 error
Remove the array brackets [] when invoking KB
leof2.KB(KostEnoik);
and remove the preceding enclosing brace }.
Aside: Java naming conventions indicate that variables start with a lowercase letter e.g. kostEnoik. Also consider giving the method KB a meaningful name, e.g. calculateCost
Read Java naming conventions
concern is with your access
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it is defined i.e. outside for loop and scope is upto for loop and that's why the compiler will not be able to find that varialble .
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it's defined (in this particular case, the for loop) and that's not allowed.
method KB takes an int array as argument, but you don't have to add the [] when passing the argument. The correct line is
leof2.KB(KostEnoik);
However, there's something pretty odd with you logic: you're repeatedly (for loop) setting leof2, but only the last iteration of the loop will have any effect. I'm almost certain that that's not what you actually want, but the correct answer to where Bus leof2 should actually be defined depends on the correction of that issue.
leof2.KB(KostEnoik); this is the main culprit. whether you have imported UserInput.
Also try to go through the Java Basics
any method can be invoked using object when it is non static or class name when it is static. Please consider this link
Get leof2 object out side the For Loop.
Don't type [] when you pass the array as argument "leof2.KB(KostEnoik[]);".
I am new at programming and currently in our classes we are learning java. I am trying to create a routine in which I need to use String variables only. Below it is the code in which I am working with:
public static void main(String[] args) throws java.io.IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
PrintStream out = System.out;
String hair.equals("damagedHair");
cutHair(marvin);
cleanHair(michelle);
for (int i = 0; i < 2; i++) {
static void cutHair(String marvin) {
String cabello;
marvin.equals(hair);
if (marvin.equals("damagedHair")) {
cabello.equals("newHaircut");
result(hair);
}
static void cleanHair(String michelle) {
String hair;
michelle.equals(hair);
if (michelle.equals(newHaircut)) {
hair.equals("putShampooAndConditioner");
result(hair);
}
static void result(String pHair) {;
PrintStream out = System.out;
out.println("=============");
out.println(pHair);
out.println("=============");
}
}
Jcreator is giving me an error that says Illegal start of expression and also java 50 error ';' expected.
I am not sure why is this coming up and I am a little confused as to whether I am doing something I am not supposed to and how to correct it. Sorry about the double posting, this is the right message. Need some help from you guys to figure this out.
Thanks in advanced!
This is in your main:
for(int i=0; i<2; i++)
{
static void cutHair(String marvin)
{
String cabello;
marvin.equals(hair);
if(marvin.equals("damagedHair"))
{
cabello.equals("newHaircut");
result(hair);
}
}
You cannot define methods inside of main. Also, hair is not in scope here, ie it's in your main, not your method. Additionally, you're constantly only declaring variables, and then using them without them ever having been initialized. For example, in the above method, you have:
cabello.equals("newHairCut")
but cabello was never initialized, this should give you a might not have been initialized warning. Or earlier in your code, you have:
String hair.equals("damagedHair");
Again, this doesn't make any sense. You just declared hair here, you cannot call methods on it until you initialize it. I suggest that you review some tutorials.
I just read that it is not possible to define a pointcut on a single array element (bug link). Considering I really need to detect an array element modification, I would like to know if there is any workaround for this kind of problem (a pattern or something).
Something like what is described in this article
public class FieldPointcuts {
static int ar[];
public static void main(String[] args) {
ar = new int[] {100}; //set
ar[0] = 200; //get
}
}
and advice
before(int i, Object s, Object[] a):
arrayset() && args(i, s) && target(a)
{
System.out.println (" Arrayset:["+i+"/"+(a.length-1)+"] = "+s) ;
}
Thanks in advance.
Unfortunately, there is nothing I can think of that will work. The best that I can think of would be to use Lists instead of arrays, but if you are weaving into third party code, this would not be possible.