I am developing a Java API which is consumed by an angular app. There's a socket which sends all the key events from browser. How can I get the corresponding character of the keysym table?
Something like:
int a = 97;
int b = 98;
int c = 99
int enter = 65293;
String character = Keysym.get(a); //outputs a
Solution
You simply need to cast to char :
static char getCode(int key) {
return (char) key;
}
Use as
public static void main(String[] args) {
System.out.println(getCode(97)); // a
System.out.println(getCode(98)); // b
System.out.println(getCode(99)); // c
}
Related
My goal is currently to take a string looking like
"######
# #
# # ##"
# is here my "character" and "#" is walls that should be avoided, I want my character to get out of the box and return the pathway, I realize my example is pretty bad but I hope you understand. The idea I have currently is to read in a text-file with that String, then creating an array as a 2-D grid and then work from there, the code I have currently is:
package soko;
import java.io.*;
import java.util.*;
public class Sokoban2 {
static File file;
Scanner input;
static int b;
static int c;
static String[][] array;
public Sokoban2() {
//array = new String [9][9];
}
public int readFile() throws Exception{
Scanner input = new Scanner(System.in);
file = new File("C:/Users/joaki/Desktop/sokoban/readin.txt");
input = new Scanner(file);
while (input.hasNext()) {
b = b + 1;
String line = input.nextLine();
System.out.println(line);
}
input.close();
return b;
//array = new String[5][5];
}
public void insertStuff() {
//array[1][1]="2";
}
public void printStuff() {
for (int r = 0; r<2;r++){
String line2 = "";
for (int d = 0; d <2;d++){
line2+="["+array[d][r]+"]";
}
System.out.println(line2);
}
}
public static void main(String[] args) throws Exception{
Sokoban g = new Sokoban();
g.readFile();
//g.insertStuff();
//g.printStuff();
//g.creatingGrid(c,b);
//System.out.println(b);
}
}
I really want to print my b, at least check it's value, but my program wont return any value, it's not even returning a null or 0, I've tried to print it as well, no luck there either, ideas ?
You aren't returning the value to any variable, I just ran your code and it works fine, it counts the lines in the file.
int b = g.readFile();
System.out.println(b);
If you don't return the value to a variable you won't have access to the value of b in your main because it's out of scope, more about that here:
https://www.cs.umd.edu/~clin/MoreJava/Objects/local.html
Edit:
I actually just realized that you declared b in your class as static and I decided to run it as you have it above, I still get it printing out the amount of lines.
g.readFile();
System.out.println(b);
If you are going to have your function return a value and you don't plan on storing b then get rid of the static declaration in your class and just declare it in the function that returns b.
package dspermutation;
import java.util.Scanner;
public class DSPermutation {
String s;
char[] c;
int n;
public static void main(String[] args) {
DSPermutation ds=new DSPermutation();
ds.input();
}
private void input() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
s=sc.next();
c=s.toCharArray();
n=c.length;
permutation(c,n-1,0);
}
private void permutation(char[] cc,int nn,int ii) {
if(ii==nn)
{
System.out.println(cc);
}
else
{
for(int j=ii;j<=nn;j++)
{
swap(cc[ii],cc[j]);
permutation(cc,nn,ii+1);
swap(cc[ii],cc[j]);
}
}
}
private void swap(char p, char c0) {
int x=s.indexOf(p);
int y=s.indexOf(c0);
/*1*/ char temp=c[x];
/*2*/c[x]=c[y];
/*3*/c[y]=temp;
/*c[x]=c0;
c[y]=p;*/
}
}
The above program is for printing all permutations of a given string.The result is coming true but in swap() method if i replace line 1,2,3(written in comment) by logic written in comment(after line 1,2,3) then answer comes wrong. Why could this be happening?
Your mistake is assuming c[x] == p and c[y] == c0. But the indexes x and y are derived from the immutable string s, which doesn't reflect the values in c in its shuffled state.
You are swapping values of character array using immutable string's position (i.e String always holds the same initial values). To make your commented code work you have to add this s = String.valueOf(c);at the end of swap function.
private void swap(char p, char c0) {
int x = s.indexOf(p);
int y = s.indexOf(c0);
// char temp = c[x];
// c[x] = c[y];
// c[y] = temp;
c[y] = p;
c[x] = c0;
s = String.valueOf(c);
}
I am trying to print reverse of a 32 bit binary number in decimal format:
Example:
x = 3,
00000000000000000000000000000011
=> 11000000000000000000000000000000
return 3221225472
I am getting a number format exception, can anyone please help me whats wrong in my code? I appreciate your help.
public class ReverseBinary {
public long reverse(long a) {
String s1 = String.format("%32s", Long.toBinaryString(a)).replace(' ', '0');
StringBuilder sb = new StringBuilder(s1);
String s = sb.reverse().toString();
long c = Integer.parseInt(s, 2);
return c;
}
public static void main(String args[]) {
ReverseBinary rb = new ReverseBinary();
long ans = rb.reverse(3);
System.out.println(ans);
}
}
Your variable c might be a long variable, but the value delivered by Integer.parseInt(s,2) is still an integer. This call tries to parse an integer value which causes problems, because the value is obviously out of the integer range.
Simply replace Integer.parseInt(s,2) by Long.parseLong(s, 2).
It should be
long c= Long.parseLong(s,2);
Just in case you want the signed integer that corresponds to the reversed bit pattern: there is a method to just do that.
public class Test
{
public static void main (String[] args)
{
// 000...001 -> 100...000, interpret as two's complement 32bit int
int reversed = Integer.reverse(0b0_00000000_00000000_00000000_00000001);
System.out.println(reversed == Integer.MIN_VALUE); // -> true
}
}
This is my code . I want a binary value for c ,but my output is 294977 . How to xor this?
public class Dumm {
public static void main(String []args) {
int a = 01101010;
int b = 00001111;
int c = a ^ b;
System.out.print(c);
}
}
If you want to take a and b as binary value then start with "0b".
for print binary value use "Integer.toBinaryString()" method.
Try this:
public static void main(String [] args)
{
int a = 0b1101010;
int b = 0b0001111;
int c = a ^ b;
System.out.println(Integer.toBinaryString(c));
}
If you want to treat your numbers as binary, you have to start numbers with 0b.
You have prepended o(English Alphabet) instead of 0(Zero).
int a = 0b01101010;
int b = 0b00001111;
int c = a ^ b;
System.out.print(Integer.toBinaryString(c));
This is in Java as the tag implies. I can't figure out how to get it to print out the "key" string at the end of the code while keeping my variables the way they are. I've only really worked with static main, and I have no idea what that does for programs as I'm a total novice. Can someone point me in the right direction? I'd like to know what you all think!
import java.util.Random;
class Key {
private String key = new String();
private void main(String[] args) {
Random r = new Random();
for (int i = 10; i > 0; i--) {
int randomNumber = r.nextInt(10) + 48;
int randomLetter = r.nextInt(26) + 97;
int branchSwitch = r.nextInt(2);
if (branchSwitch == 1) {
// System.out.print((char)randomNumber);
key = key + (char) randomNumber;
} else
key = key + (char) randomLetter;
// System.out.print((char)randomLetter);
}
System.out.print(key);
}
}
First, main should be public static if you want to run this as an application.
So you can fix your program as follows (note that your original main is renamed to generateAndPrint because you can't have two methods with the same signature in one class):
class Key {
private String key = new String();
private void generateAndPrint() {
Random r = new Random();
for (int i = 10; i > 0; i--) {
int randomNumber = r.nextInt(10) + 48;
int randomLetter = r.nextInt(26) + 97;
int branchSwitch = r.nextInt(2);
if (branchSwitch == 1) {
// System.out.print((char)randomNumber);
key = key + (char) randomNumber;
} else
key = key + (char) randomLetter;
// System.out.print((char)randomLetter);
}
System.out.print(key);
}
public static void main(String[] args) {
Key key = new Key();
key.generateAndPrint();
}
}
I can't understand why your main is private (and non-static).
However, here is a test-run of your program at ideone.com. It seems to work ok.
Changes I made:
Made the main-method public static
Made the variable static.