• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in my String program

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic