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())
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a string array for an app I am making. It loads an EditText, so I will instead create a dummy one.
String[] s = ["1","2","3"];
How would I change String s into an integer array, such as Int i?
Int[] i = [1,2,3];
I have tried Integer.parseInt on the string array but it does not work. Does anyone know how to convert string arrays into integer arrays?
Something like the following should work:
string[] s = ["1","2","3"];
int[] i = new int[s.length()];
for (int j = 0; j < s.length(); j++)
i[j] = Integer.parseInt(s[j]);
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
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);
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 the array in JAVA:
public static char array[][] = new char[3[3];
during running the program this array fills some characters.
How can I check if this array[3][3] has the character' '(space) then return true?
Thanks.
Do you really try something ?
public boolean check(char[][] array){
for(int i=0; i<char.length; i++){
for(int j=0; j<char[i].length; j++){
if(char[i][j] == ' ') return true ;
}
}
return false;
}
I advice you to read some algorithm tutorial, it's the base !!
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) );