to get the next character in the string [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
please help me find the code for getting the next character in the string
for example:
input string = "abcd"
output string = "bcde"
I can able to iterate only one character at the time.
thanks in advance

Get the ASCII of the last character in the String and increase the ASCII value by 1.
try this,
String sample = "abcd";
int value = (int) sample.charAt(sample.length() - 1); // here you get the ASCII value
System.out.println("" +((value < ((int)'z')) ? sample.substring(1) + (char) (value + 1) : sample));

Simply add one to the char values:
char[] chars = "abcd".toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] += 1;
}
String nextChars = new String(chars);

Related

Java : Reverse the String but remain the index of the comma [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Reversing the string while maintaining the index of COMMA.
the string that need to be reversed : "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g".
Expected output: "g,od ,yzal ,eht ,revo, spmuj ,xof nw,orb kc,iuq eh,T"
use StringBuffer and Stack first remove all commas and save the index of them on stack after reversing insert commas at location that you saved them on stack. use stringBufferObject.deleteCharAt(index); for deleting a character and use stringBufferObject.reverse(); for reversing string.
Here is the code with your input
public class Test {
public static void main(String [] args){
String original = "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g";
String reverse = "";
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}
Here this is code you can use it
String original = "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g";
String reverse = original.replaceAll(",","");
StringBuilder rev = new StringBuilder(reverse);
rev = rev.reverse();
String output="";
int j = 0;
for (int i = 0; i < original.length(); i++) {
if(original.charAt(i) == ','){
output += ",";
}else{
output+=rev.charAt(j++);
}
}
System.out.println(output);

Counting the nonwhitespace in a String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How would I count the nonwhitespace in String in Java?
Only thing I can declare is the int nonwhitespace = 0; That is all I can think of.
Code below should work.
int total = 0;
for(int i=0; i < str.length(); i++)
{
if(!Character.isWhitespace(str.charAt(i)))
total++;
}
System.out.println("total non-whitespace characters are " + total);
How about
int nonwhitespace = text.replaceAll("\\w","").length();
May be first replace all whitespaces and then count the length?
str.replaceAll("\\s+","") - to remove whitespaces..
str.length() -- to then count the number the nonwhite space chars

how to get last three words of a string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string containing this:
D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo.
I want to get just this part:
wt\lifecycle\StateRB
How can I do that?
You can simply spilt whole path to parts and then get the parts you want.
String path = "D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo";
String[] parts = path.split("\\");
parts = Arrays.copyOfRange(parts, parts.length-3, parts.length);
Or you can get throught string using loop (this seems to be better)
int index = 0, i = 0;
Stack<String> al = new Stack<String>();
while((index = path.lastIndexOf()))!=-1 && i < 3) {
al.push((path = path.substring(index)));
i++;
}
String[] parts = (String[])al.toArray(); //if you don't have array elements
// in correct order, you can use
// Collections.reverse with Arrays.asList
// applied on array
You can use string tokeniezer with \ delimiter and fetch only last three string tokens. i hope that above path going to be constant always.
http://www.tutorialspoint.com/java/java_string_substring.htm
Check the above link
example :
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );

Convert a String to a Vector in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a function that has an output as a 4 character String like "1000". I need to convert this into a vector of dimension (4,1) in order to be able to make computation with matrices.
Any idea or help? Thank you very much in advance.
String s = "1000";
Vector myVec = new Vector();
//Convert the string to a char array and then just add each char to the vector
char[] sChars = s.toCharArray();
for(int i = 0; i < s.length(); ++i) {
myVec.add(sChars[i]);
}
Try:
Vector<Character> v = new Vector<Character>(Arrays.asList(yourString.toCharArray()))
As stated by fge, a List would be at least as useful as a Vector:
List<Character> l = Arrays.asList(yourString.toCharArray())

How to get only integer part from String variable? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some string type data in my database table like as D-101 and D-102.
I want to get automatically next data in front end (a JSF web application) to send to database which is D-103.
For this I want to get only int data from string.
How can I do this?
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
String input = D101;
Matcher matcher = lastIntPattern.matcher(input);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
int lastNumberInt = Integer.parseInt(someNumberStr);
System.out.println("Test int output - " + lastNumberInt);
inputList.add(lastNumberInt);
}
then compare max value
int currentBranchCode = 0;
for (Integer CCS : companyArrayList) {
if (currentBranchCode <= CCS) {
currentBranchCode = CCS;
}
}
and then
currentBranchCode =currentBranchCode +1;
String codegenerate="D"+currentBranchCode;
you must try it.
In your managed bean you can use split() function.
String s = "D-101";
String[] arr = s.split("[^\\d]+");
System.out.println(arr[1]); //prints 101
OR
In the xhtml page you can write an EL like this. Note that myBean is the name of your bean and getColumnValue() method returns a value of a column(i.e. "D-101").
#{myBean.columnValue.split('[^\\d]+')[1]}
This can be done in just a couple of lines:
int i = Intger.parseInt(input.replaceAll(".*(?<!\\d)(\\d+)", "$1");
String next = input.replaceAll("(.*)(?<!\\d)\\d+", "$1"+ ++i);

Categories