Hi guys>.<

I'm trying to make a program that suppose to output like this:

Enter a String: 1231asd

No. of Digit: 4
End the Program? y

*the red part is the input from the user.

but the problem is, the no. of digits only display 0 and not the total digit
in the string. can anyone help me with this please :C. I've been searching through
the internet but can't find anything related to my problem.

This is a code that I came up with no reference other than
txt1.indexOf(txt2)>=0 and .equalsIgnoreCase()
so if anyone could point the things that shouldn't be there please do so.

:) thank you in advance to anyone that will take their time to help me

String text;
    int idx, ctrDigit=0;
    String ans;
    String digit = "0123456789";
do{    
    System.out.print("Enter a String: ");
      text = in.nextLine();
      text.equalsIgnoreCase(text);
     for(idx=0; idx < text.length(); idx++)
         if(digit.indexOf(text)>=0)
             ctrDigit++; 
    System.out.println();

    System.out.println("No. of Digits: " + ctrDigit);

    System.out.print("End the program? ");
      ans = in.nextLine();
}while(ans.indexOf("n")>=0 || ans.equalsIgnoreCase("NO"));
System.out.println();
    }}

you could also try:

int count = 0;
String test = "my1stStringwith2Digits";
// count should return 2 : 1 & 2
for ( int i = 0; i < test.length(); i++){
  if ( Character.isDigit(test.charAt(i))
    count += 1;
}
System.out.println("Count = " + count);

but .. just a question.
what exactly do you think this:

text.equalsIgnoreCase(text);

does?

for testing something that's not found by indexOf, check the equality against -1, it'll be a lot easier for others to read.
what exactly is that 'in' that you're reading from?

sorry :D my program actually not only need to count the digits but also the letter I just did not include it yet.

text.equalsIgnoreCase <-- All I know is that this is use if the comparison is case-sensitive. I'm still trying to figure out what will it do to my program :D

Anyway I tried using -1
and this is what happens

Enter a String: 1231231

No. of Digit: 10
Total Length: 7

:D I think I'm getting lost here. I don't know where did it get 10 from 1231231

ooooh.. I think it start having problem with the no. Of digit if the number of string inputed exceeded the amount of 10

just like this

run:
Enter a String: 1231123456

No. of Digit: 10
Total Length: 10

End the program?

Enter a String: 767866879797

No. of Digit: 32
Total Length: 12

End the program?

but it also has another problem.
it also counts the letters as digits>.<

Enter a String: 123asd

No. of Digit: 6
Total Length: 6

End the program?

Edited 4 Years Ago by Ashenvale: n/a

sorry :D my program actually not only need to count the digits but also the letter I just did not include it yet.

you can do that in a similar way, with Character.isLetter(myChar)

text.equalsIgnoreCase <-- All I know is that this is use if the comparison is case-sensitive. I'm still trying to figure out what will it do to my program :D

actually, I think you're a bit off there.
to compare String objects for equality, you use either the .equals(String o) or the .equalsIgnoreCase(String o) method.

what yours will do, it will always return true, but you are not using that value anyway so ... it will do nothing at all :)

to explain a bit what it does:

String stA = new String("myString");
String stB = new String("myString");
String stC = new String("MyString");
if ( stA == stB )
System.out.println("stA == stB"); // will NOT be printed
if ( stA.equals(stB) ) 
System.out.println("stA.equals(stB))"; // will be printed since the values are identical
if ( stA.equals(stC))
System.out.println("stA.equals(stC))");// will NEVER be printed. java is case
// sensitive, and m is not equal to M
if ( stA.equalsIgnoreCase(stC))
System.out.println("stA.equalsIgnoreCase(stC))"); // this will be printed.
// even though the values in total aren't equal, the equalsIgnoreCase will disregard
// every difference caused by a difference of case.(upper/lower)

ooooh.. I think it start having problem with the no. Of digit if the number of string inputed exceeded the amount of 10

just like this

but it also has another problem.
it also counts the letters as digits>.<

can you show your code as it is now?
it might be easier if you show the code for the entire class (if it isn't too big) or at least the declaration of all the variables you use in that particular snippet.

you can do that in a similar way, with Character.isLetter(myChar)


actually, I think you're a bit off there.
to compare String objects for equality, you use either the .equals(String o) or the .equalsIgnoreCase(String o) method.

what yours will do, it will always return true, but you are not using that value anyway so ... it will do nothing at all :)

to explain a bit what it does:

String stA = new String("myString");
String stB = new String("myString");
String stC = new String("MyString");
if ( stA == stB )
System.out.println("stA == stB"); // will NOT be printed
if ( stA.equals(stB) ) 
System.out.println("stA.equals(stB))"; // will be printed since the values are identical
if ( stA.equals(stC))
System.out.println("stA.equals(stC))");// will NEVER be printed. java is case
// sensitive, and m is not equal to M
if ( stA.equalsIgnoreCase(stC))
System.out.println("stA.equalsIgnoreCase(stC))"); // this will be printed.
// even though the values in total aren't equal, the equalsIgnoreCase will disregard
// every difference caused by a difference of case.(upper/lower)

I see, so this is just for returning true or false. What if I want to change the inputted
string to small case what code should I use?

can you show your code as it is now?
it might be easier if you show the code for the entire class (if it isn't too big) or at least the declaration of all the variables you use in that particular snippet.

//variable declaration
    String text;
    int idx, ctrDigit;
    String ans;
    String digit = "1234567890";
do{    
    System.out.print("Enter a String: ");
      text = in.nextLine();

      ctrDigit=0;
     for(idx=0; idx < text.length(); idx++)
         if(digit.indexOf(text)>=-1)
             ctrDigit++;

    System.out.println();
    System.out.println("No. of Digit : " + ctrDigit);
    System.out.println("Total Length : " + text.length());
        System.out.println();
        
    System.out.print("End the program? ");
      ans = in.nextLine();
      System.out.println();

}while(ans.indexOf("n")>=0 || ans.equalsIgnoreCase("NO"));
    System.out.println();
    }}

SAMPLE OUTPUT:

run:
Enter a String: 123123

No. of Digit : 6
Total Length : 6

End the program? n

Enter a String: 123asd

No. of Digit : 6
Total Length : 6

End the program? n

Enter a String: asd

No. of Digit : 3
Total Length : 3

End the program? y

Edited 4 Years Ago by Ashenvale: n/a

from the original code (dont know about current code) i see that you could probably have fixed it or at least fix the problem you reported by changing these lines :

for(idx=0; idx < text.length(); idx++)
         if(digit.indexOf(text)>=0)   // <-- here you look for an occurence of the contents of "text" in "digit" while what you really want, is the occurence of the character at index "idx" of "text" in "digit"
             ctrDigit++;

basicly you were saying :

"for every single character in my string 'text', is there '1231asd' inside '0123456789' ?"

while you want to say :

"for every single character in my string 'text', is there '(char at index idx)' inside '0123456789' ?"

you could do this simply by adding :

for(idx=0; idx < text.length(); idx++)
         if(digit.indexOf(text.charAt(idx))>=0) // <-- text.charAt(idx)
             ctrDigit++;

but i still agree with stultuske that isDigit and isLetter would be better options.

I see, so this is just for returning true or false. What if I want to change the inputted
string to small case what code should I use?

String s = "Hi My NaMe iS pHIl";
s = s.toLower();
System.out.println(s);
//variable declaration
    String text;
    int idx, ctrDigit;
    String ans;
    String digit = "1234567890";
do{    
    System.out.print("Enter a String: ");
      text = in.nextLine();

      ctrDigit=0;
     for(idx=0; idx < text.length(); idx++)
         if(digit.indexOf(text)>=-1)
             ctrDigit++;

    System.out.println();
    System.out.println("No. of Digit : " + ctrDigit);
    System.out.println("Total Length : " + text.length());
        System.out.println();
        
    System.out.print("End the program? ");
      ans = in.nextLine();
      System.out.println();

}while(ans.indexOf("n")>=0 || ans.equalsIgnoreCase("NO"));
    System.out.println();
    }}

SAMPLE OUTPUT:

run:
Enter a String: 123123

No. of Digit : 6
Total Length : 6

End the program? n

Enter a String: 123asd

No. of Digit : 6
Total Length : 6

End the program? n

Enter a String: asd

No. of Digit : 3
Total Length : 3

End the program? y
if(digit.indexOf(text)>=-1)

you dont want it to be = to -1 , this method returns the index if its present, and -1 if it is not, you only want to test for 1 of these 2 conditions :

if(digit.indexOf(text) > -1){
    //present
}
//or
if(digit.indexOf(text) = -1){
    //not present
}

Phillipe a gazillion thanks :D I didn't know u can combine string methods like that.. It really fixed the problem all I have to do now is to put no. of Letter but I think I can manage that on my own..


Thank you everyone I learned a lot

Stultuske <<--- >.< It's the same exmple as Phillipe but I didn't get it the first time I'm so dumb haha. Thank you so much for your help..

if(digit.indexOf(text)>=-1)

you dont want it to be = to -1 , this method returns the index if its present, and -1 if it is not, you only want to test for 1 of these 2 conditions :

if(digit.indexOf(text) > -1){
    //present
}
//or
if(digit.indexOf(text) = -1){
    //not present
}

oh, someone told me to test -1, but I didn't get what should I be testing. Now I understand what he means.

if(digit.indexOf(text) > -1){
    //present
}
//or
if(digit.indexOf(text) = -1){
    //not present
}

do remember, you're comparing integers here, so ...

if ( digit.indexOf(text) = -1 ){
 // this is not what you want
 // firstly, looking at previous posts, you'll want to look for one single char, not 
 // the entire text-String
 // secondly = -1 just sets a value, it doesn't compare it
}

should be replaced by

if ( digit.indexOf(text.charAt(i)){
  // nope, not present
}

the ways I see it most is:

if ( digit.indexOf(text.charAt(i)) != -1 ){
 //run code
}

or

if ( digit.indexOf(text.charAt(i)) == -1 )
  throw new DigitNotFoundException("Char is not a digit.");
// run code
This question has already been answered. Start a new discussion instead.