Can some one help me with this code. To give a run down I am trying to use the r
ID: 3874168 • Letter: C
Question
Can some one help me with this code. To give a run down I am trying to use the robot class to type a city name, so I have a scanner input to recieve the city name and then convert the string into a char array and then in the Keypress robot object I have a loop that types each chacter until down. the problem is I dont know how to represent the char values when invoking the key press method. here is the code bit Im talking about
I tryed putting a1 or i in the parenthesis of the method neither seems to work. Help would be much appreciated
Scanner sc = new Scanner(System.in); System.out.print("Enter City"); string city = sc.next(); char [] a1 city.toCharArray(); for(int 1-0 ; 1 al.length; i++) { System.out.print(i); Keypress (a1); try robot = new Robot(); catch (ANTException e) e.printStackTrace); public static void Keypress (char i) f for( 1-0; i 20 ; i++) { robot.keyPress (KeyEvent.VK_SHIFT); robot.keyPress (i); robot.keyRelease(i); robot.keyRelease (KeyEvent.VK_SHIFT)Explanation / Answer
when calling method Keypress you are passing character array to it but in the defination of method keypress you are passing only one character, there are multiple errors you will find those in code below and few corrections too, comment out unneccesary stuff from the code
/************************ code ****************************************/
import java.awt.AWTException;
import java.util.Scanner;
public class Robot {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter City");
String city=sc.next();
char[] a1=city.toCharArray();
for(int i=0;i<a1.length;i++)
{
System.out.println(i); // prints index of character
System.out.println(a1[i]); // prints character in city name at index i
//above line will print all characters upside down from city name, comment out line above that to avoid printing index
}
Keypress(a1); // you are passing character array a1 as parameter to keypress method
try
{
robot=new Robot(); //i hope you have declared this object in earlier code somewhere like this Robot robot; otherwise there will be an error
}
catch(AWTException e)
{
e.printStackTrace();
}
sc.close();
}
public static void Keypress(char i) //here keypress is taking only one character as parameter...you need to pass whole array instead
//public static void keypress(char[] a1) //use this if you want to pass whole array
{
for(i=0;i<20;i++) // as you have passed i in argument of method you should've used differed counter in for-loop instead of i
//for(int j=0;j<20;j++) //better to use this as i is character and you are changing its value in forloop by intialising it with 0
{
robot.keyPress(KeyEvent.VK_SHIFT); // Keypress and keyPress methods are both different, i hope you are not considering them same
robot.keyPress(i); //robot object should be global if you want to use it in this method
robot.keyRelease(i);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
//use this method this will work
public static void keypress(char[] a1) //use this if you want to pass whole array
{
for(int j=0;j<20;j++) //better to use this as i is character and you are changing its value in forloop by intialising it with 0
{
// robot.keyPress(KeyEvent.VK_SHIFT); // Keypress and keyPress methods are both different, i hope you are not considering them same
// robot.keyPress(i); //robot object should be global if you want to use it in this method
// robot.keyRelease(i);
// robot.keyRelease(KeyEvent.VK_SHIFT);
System.out.println(a1[j]); //prints characters of city name
}
}
}
/****************************** end code ***************************************/
your basic question is answered in above code...if you have meant differentely please comment on this answer with your query..next time please provide all code that will help us understand your query much better and we will be able to help better