This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
How do I convert from int to String?
(20 answers)
Closed 8 years ago.
I'm working on Caesar cipher example in which I want that it get different keys from user and finally one key decrypt the original text , but I got a problem, here is my code
public static void main(String[] args) {
Scanner user_input= new Scanner(System.in);
String plainText = "University of malakand";
String key;
key = user_input.next();
Ceasercipher cc = new Ceasercipher();
String cipherText = cc.encrypt(plainText,key);
System.out.println("Your Plain Text :" + plainText);
System.out.println("Your Cipher Text :" + cipherText);
String cPlainText = cc.decrypt(cipherText,key);
System.out.println("Your Plain Text :" + cPlainText);
}
it shows an error on this line
String cipherText = cc.encrypt(plainText,key);
it shows me error on key incompatible types:
String cannot be converted into int
What can I do?
Ask following questions to your self first.
What your method want as parameter?
Why both are Incompatible?
What String cipherText = cc.encrypt(plainText,key); mean?
key is String or int?
Use methods like Integer.parseInt(String value) or Integer.valueOf(String value) for conversion.
It seems like you need to convert a String to int, not a int to String. To do that, you can use Integer.parseInt():
int someInt = Integer.parseInt(someString);
You are passing String and the method parameter seems to have int and hence the error. You might want to convert your string to int using int keyInt = Integer.parseInt(key); and similarly for plain text if necessary and then pass keyInt and/or plainTextInt as the parameters.
Try this -
String cipherText = cc.encrypt(plainText,Integer.parseInt(key));
Related
I have a hexadecimal number in a String which is too large to convert to int and long and want to add the value of another hexadecimal number.
So let's say I have this number:
String hex1 = "0xf27f2029f9c103f77be78b9591c1ab27167858d27d789cd3ea8a270c67ea5d91";
And want to add:
int hex2 = 0x1; or String hex2 = "0x1";
I know this question has been asked allready How to subtract or add two hexadecimal value in java but the answers don't work for me because they all involve conversion to int.
You can do it as follows:
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
String hex1 = "0xf27f2029f9c103f77be78b9591c1ab27167858d27d789cd3ea8a270c67ea5d91";
String hex2 = "0x1";
System.out.println(
"In decimal: " + new BigInteger(hex1.substring(2), 16).add(new BigInteger(hex2.substring(2), 16)));
System.out.println("In hexdecimal: "
+ new BigInteger(hex1.substring(2), 16).add(new BigInteger(hex2.substring(2), 16)).toString(16));
}
}
Output:
In decimal: 109684320921920394042076832992416841330182602685967688614501993994243850001810
In hexdecimal: f27f2029f9c103f77be78b9591c1ab27167858d27d789cd3ea8a270c67ea5d92
Change your code to:
String hex1="0xf27f2029f9c103f77be78b9591c1ab27167858d27d789cd3ea8a270c67ea5d91";
int hex2=0x1;
string hex2="0x1";
You need "" for entering a value for string.
This question already has answers here:
How do I convert from int to String?
(20 answers)
How to Convert an int to a String? [duplicate]
(6 answers)
Java - Convert integer to string [duplicate]
(6 answers)
Closed 4 years ago.
How can I store a int value in a String?
For example:
int number = 10;
string word = number;
You can use the static method:
String.valueOf(number)
Or
new Integer(number).toString();
int number = 10;
string word = Integer.toString(number);
this will convert the integer "number" to string to be able to store it inside a string without changing it's value
just that easy, good luck :)
There are multiple ways to convert an int to string.
String word = Integer.toString(number);
String word = String.valueOf(number);
String word = new Integer(number).toString();
String word = String.format ("%d", number);
etc..
There are more ways to do this. But I prefer the 1st one.
Just do some googling you will find more answers! :)
Normal ways would be Integer.toString(number) or String.valueOf(number).
But, You can try this:
int number = 10;
String str = String.valueOf(number);
Or
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(number);
String str = sb.toString();
This question already has answers here:
Java Integer parseInt error
(4 answers)
Closed 6 years ago.
I need to convert the string of hex to decimal in java..
My hex value is "00000156A56BE980".
My required output is 1471654128000
I have done the following in java,
public static void main (String args [])
{
String hexValue = " 00000156A56BE980 ";
Integer result = Integer.parseInt(hexValue, 16);
System.out.println(result);
}
but I am getting the following error,
Number format Exception for input string "00000156A56BE980"
I have tried by giving long also the same error coming.. For other hex value it's coming but when we give hex string of larger value it shows the error.
How can we convert this number to decimal?
Can anyone solve this issue for me?
Try it like so
import java.math.*;
class Main {
public static void main (String args [])
{
String hexValue = "00000156A56BE980";
BigInteger result = new BigInteger(hexValue, 16);
System.out.println(result);
}
}
See also this repl.it
The problem is probably because your value does not fit within the value range (-231 to 232-1) of Integer - see the docs
The number is too large for a 32-bit int
Try using a long instead.
public static void main(String[] args) {
String hexValue = "00000156A56BE980";
long result = Long.parseLong(hexValue, 16);
System.out.println(result);
}
Note: you can't have spaces in a number. You can call .trim() to remove them.
2 thing in order the code can work:
remove spaces trimming the String
the result doenst fit in an integer so use either Long or BigInteger instead
public static void main(String[] args) {
String hexValue = " 00000156A56BE980 ";
long result = Long.parseLong(hexValue.trim(), 16);
System.out.println(result);
}
The number is too big for integer (> 2^32).
(The value represented by the string is not a value of type int.)
Take a look here
I am trying to convert my string to ASCII value through my hash function, which looks like this:
public long hash(String word){
StringBuilder sb = new StringBuilder();
String ascString = null;
long asciiInt;
for(int i=0;i<word.length();i++){
sb.append((int)word.charAt(i));
}
ascString = sb.toString();
asciiInt = Long.parseLong(ascString);
return asciiInt;
}
and later on, I will call it in my insert() method to perform quadratic hashing using a hashTable, and the insert method looks like this:
public void insert(Word word){
int start = (int)(hash(word.text)%tableSize);
int key = start;
int attempt=0;
while(hashTable[key]!=null){
attempt++;
key=(start+(int)Math.pow(attempt,2))%tableSize;
}
hashTable[key]=word;
}
However, it throws the java.lang.NumberFormatException if the string I am trying to convert has more than 6 characters. Can anyone help me fix it or a better ways of coming up with the key value for my hash table?
Thanks!
The value you're attempting to gain (base 10 long) can't be achieved from your string because you've the wrong base. Say the string is "DEADBEEF". Because all digits of DEADBEEF are base 16, you can specify the radix as 16 and use
Long.parseLong(DEADBEEF, 16);
The non-radix method assumes that the string contains a base-10 long when really the number is much longer (DEADBEEF is 3735928559 in base 10). Check your string maybe?
This question already has answers here:
How to Convert an int to a String? [duplicate]
(6 answers)
Closed 8 years ago.
I'm trying to create a method which generates a 4 digit integer and stores it in a string.
The 4 digit integer must lie between 1000 and below 10000. Then the value must be stored to PINString.
Heres what I have so far. I get the error Cannot invoke toString(String) on the primitive type int. How can I fix it?
public void generatePIN()
{
//generate a 4 digit integer 1000 <10000
int randomPIN = (int)(Math.random()*9000)+1000;
//Store integer in a string
randomPIN.toString(PINString);
}
You want to use PINString = String.valueOf(randomPIN);
Make a String variable, concat the generated int value in it:
int randomPIN = (int)(Math.random()*9000)+1000;
String val = ""+randomPIN;
OR even more simple
String val = ""+((int)(Math.random()*9000)+1000);
Can't get any more simple than this ;)
randomPIN is a primitive datatype.
If you want to store the integer value in a String, use String.valueOf:
String pin = String.valueOf(randomPIN);
Use a string to store the value:
String PINString= String.valueOf(randomPIN);
Try this approach. The x is just the first digit. It is from 1 to 9.
Then you append it to another number which has at most 3 digits.
public String generatePIN()
{
int x = (int)(Math.random() * 9);
x = x + 1;
String randomPIN = (x + "") + ( ((int)(Math.random()*1000)) + "" );
return randomPIN;
}