Counting the nonwhitespace in a String [closed] - java

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

Related

check regex for integers bigger than the negative -328 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}

CodeConversion from byte to int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
chr[k]=byte(chp[i]-chq[j]); //problem
The problem comes in type conversion. I have seen various answers to this and when implementing this it could not be solved.
How to remove Syntex error in the above code
Use ((byte)some expression) instead of byte(some expression).
Are you trying to compare the numerical difference (char by char) between two equal length strings? I'm sorry but your explanation of the problem left much to be desired.
If I were trying to solve the issue of comparing the numerical difference between two strings, I would likely come up with something like this:
char[] a = "cd".toCharArray();
char[] b = "aa".toCharArray();
int i = 0;
int sum = 0;
while (a.length == b.length && i < a.length)
{
if (a[i] > b[i])
sum += (a[i]-b[i]);
else
sum += (b[i])-a[i];
i++;
}
System.out.println("Total character difference: " + sum);
This should cover all cases... Assuming I correctly understood what you were asking.

to get the next character in the 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
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);

How to check if char is in a double char array? 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 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 !!

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())

Categories