aspose file tools*
The moose likes Beginning Java and the fly likes problem in my String program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in my String program" Watch "problem in my String program" New topic
Author

problem in my String program

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
hi friends
can any one help me in following program
my requirements are
1]to check if any String which we enter has any white space or not
2] to check our String has any number in it or not
i have achieved first task by

private boolean whitespace_check(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt( (int) i) == ' ') {
return true;
}
}
return false;
}

but i am not able to achieve second task


code for second task is
private boolean number_check(String s) {
for (int i = 0; i < s.length(); i++)

{
for (int j = 0; j <= 9; j++) {
if (s.charAt(i) == (char) j) {
return true;
}
}

}
return false;
}

my whole code is

package New_programs;

public class Stringchecking {

private boolean whitespace_check(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt( (int) i) == ' ') {
return true;
}
}
return false;
}

//my problem area
private boolean number_check(String s) {
for (int i = 0; i < s.length(); i++)

{
for (int j = 0; j <= 9; j++) {
if (s.charAt(i) == (char) j) {
return true;
}
}

}
return false;
}

public static void main(String[] ar) {

String s1 = "ganeshpol007";
Stringchecking sc = new Stringchecking();

if (sc.whitespace_check(s1)) {
System.out.println("it contains white space");

}
else {
System.out.println("it does not contains white space");

}
if (sc.number_check(s1)) {

System.out.println("it contains numbers");
}
else {
System.out.println("it does not contain number");

}
}
}
ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
sorry friends
in last post i have not mentioned what is actual problem

my program gives follwing results

it contains white space <<----- expected results
it does not contain number<<<<------logical error

although my String is
String s1="gannesh pol009";
please help me friends
i think the problem is due to casting

THANKS IN ADVANCE
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you cast a number to a character, you don't get a character (string?) that represents that number; you get the character whose Unicode value is that number. (char) 0 is not '0', but u0000, the ASCII "NUL" character. The numeral '0' has the Unicode value 48 decimal. You could get your program to work by writing

if (s.charAt(i) == j + 48) { ...

Now, a word of advice: the Java API is full of useful stuff. For this sort of very basic program, the java.lang package is worth looking at. In particular, if you're working with Strings and characters, read the whole API for String and Character classes. Character has methods for determining whether a char is whitespace (there are many other whitespace characters than just ' ') and whether a character is a digit (and it's more efficient than looping over the ASCII digits. Note also, again, that Unicode includes other digits than just the Arabic ones.)


[Jess in Action][AskingGoodQuestions]
ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
Thanks Sir (java GURU)
for helping me out in this condn now my code is giving proper o/p
sir java has really big..... api AND some of the times when we get disturbed due to wrong results we are not able to get visualize powre of java API
sir most of the time when i have got problem in java i come to java Ranch and people like you are Helping people like me thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in my String program