This question already has answers here:
How to increment the number in a String by 1?
(7 answers)
Closed 5 years ago.
For example:
public static void main(String[] args)
{
String a="1";
int inc= Integer.parseInt(a+1);
System.out.println(inc);
}
I'm getting 11 but i want to get 2. How can i do it in a very efficient way?
Integer.parseInt(a+1); parses the String that results from concatenating the value of the String a ("1") to the int literal 1, which is "11".
Change it to
int inc = Integer.parseInt(a) + 1;
This way "a" would be parsed to the integer 1 and then 1 would be added to it to give you the value 2.
Since a is a String object this operation is not giving the desired input
Integer.parseInt(a+1);
because will be equivalent to do
Integer.parseInt("1"+"1");
or
Integer.parseInt("11");
you need to parse the string first and then increment
Integer.parseInt(a)+1
Related
This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 1 year ago.
I need to generate a sequence as follows:
PAY000000 - The first three characters(PAY) remain the same and the 000000 should be incremented by one:
I have tried the following method to generate a sequence number:
public String generateSequence(String currentPayment) {
String chunkNumeric = currentPayment.substring(3, 9);
return "PAY" + (Integer.parseInt(chunkNumeric) + 1);
}
Expected:
currentPayment: PAY000000 Expected value: PAY000001
currentPayment: PAY000001 Expected value: PAY000002
Actual Result:
currentPayment: PAY000001 Actual value: PAY2
The issue is when I pass PAY000001 as parameter the Integer.parseInt(chunkNumeric) remove all the leading zeros that is PAY2 generated instead of PAY000002.
Any idea how I can increment the string while keeping the leading zero?
You should instead maintain the sequence as a number, i.e. an integer or long, and then format that number with left padded zeroes:
public String generateSequence(int paymentSeq) {
return "PAY" + String.format("%06d", paymentSeq);
}
int seq = 1;
String nextSeq = generateSequence(seq);
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:
Splitting and converting String to int
(5 answers)
Closed 3 years ago.
String code = "U 12 24";
int s = Integer.parseInt(String.valueOf(code.charAt(2)));
System.out.println(s);
that would be print 1,
however, i want to try print 12 or i mean i want take 2 digits number, but i can't do it because the only way i know is just take one digit number.
how if i want take 12 and convert to int
int s = Integer.parseInt(String.valueOf(code.substring(2, 4)));
If you want to get all digits in a given string, you have to tokenize the string by space and parse every chunk into a number.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
Here's my code
public static int[] parseInt(char[] myChar)
{
int[] myInt = new int[myChar.length];
for(int x = 0; x < myChar.length; x++)
{
myInt[x] = (int)myChar[x];
}
return myInt;
}
When I System.out.println(object.parseInt(myChar)); with values {'1','2','3'} I would expect it to print myInt with the corresponding unicode values for {'1','2','3'}.
Instead I get [I#15db9742
Any ideas on what I'm doing wrong?
That because you are printing the object, hence java shows:
[I - the name of the type (in this case it is one dimensional [ array of int)
# - joins the string together
15db9742the hashcode of the object.
the memory address by default. You can use:
System.out.println(Arrays.asList(parseInt(array)));
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;
}