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();
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 2 years ago.
I want to get a user input using a JOptionPane input box and split the user's input into 2 sections. (I'm a student so I have to use JOptionPane.)
For example, I want to get the start time, 20:54, and split it to
startHour = 20;
startMin = 54;
you can use the split function of String class which return an array of string values then index 0 contain the hour, index 1 contain the minutes:
note: you need to cast the string value to int
String value = "20:54";
String [] parts = value.split(":");
int startHour = Integer.parseInt(parts[0]);
int startMin = Integer.parseInt(parts[1]);
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 7 years ago.
If I ask the user for input, how do I put it as the length of an array?
Example:
String teamnum = Input.nextLine();
int teams[]=new int[teamnum];
You need to cast the input as a integer also that's not the way you declare arrays. Try this:
String teamnum = Input.nextLine();
int newnum = Integer.parseInt(teamnum);
int[] teams = new int[newnum];
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));
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;
}
This question already has answers here:
Print an integer in binary format in Java
(24 answers)
Closed 9 years ago.
How to get all 64 bits of a long as a String in Java?
So I want to do something like this -
long value = 10;
String bits = getBits(value);
System.out.println(bits);
I suppose the output would be
0000...1010 (64 bits)
And no, this is not homework! :)
Use Long.toString with the radix:
String bits = Long.toString(someLong, 2);
2 specifies binary as opposed to any other base.
Edit: If you want to left-pad:
String bits = Long.toString(someLong, 2);
StringBuilder sb = new StringBuilder();
for (int toPrepend=10-str.length(); toPrepend>0; toPrepend--) {
sb.append('0');
}
sb.append(bits);
String output = sb.toString();
You can call the method for it:
Long.toBinaryString(long number)