# comparing the count of digits to letters in an array

Page 1 of 1

## 10 Replies - 5751 Views - Last Post: 02 July 2011 - 10:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=237731&amp;s=1aed13fe3b40c34966e9bbcb8023f44f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 charchar

Reputation: 0
• Posts: 11
• Joined: 01-July 11

# comparing the count of digits to letters in an array

Posted 01 July 2011 - 02:43 AM

Hi! i just want to ask what is the code used to know what if the digits content in an array is greater than its letter's content. thanks!
Is This A Good Question/Topic? 0

## Replies To: comparing the count of digits to letters in an array

### #2 nick2price

• D.I.C Lover

Reputation: 563
• Posts: 2,826
• Joined: 23-November 07

## Re: comparing the count of digits to letters in an array

Posted 01 July 2011 - 02:56 AM

Show us what you have attempted.

This post has been edited by nick2price: 01 July 2011 - 02:57 AM

### #3 CasiOo

• D.I.C Lover

Reputation: 1503
• Posts: 3,336
• Joined: 05-April 11

## Re: comparing the count of digits to letters in an array

Posted 01 July 2011 - 04:42 AM

1. Have a variable digitsCount and lettersCount
2. Loop through the array
3. Use the Integer.toString( i ).length() method to count digits and add them to digitsCount
4. Use String.length() to find length of the word and add them to lettersCount

Still your question makes no sense. You have an array of digits and letters???

This post has been edited by CasiOo: 01 July 2011 - 04:44 AM

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11312
• Posts: 42,605
• Joined: 27-December 08

## Re: comparing the count of digits to letters in an array

Posted 01 July 2011 - 06:13 AM

I would just use the Character.isDigit() method to check for digits, and the Character.isLetter() method to check for letters. Keep in mind that there are other types of characters like symbols and whitespaces that wouldn't qualify as either digits or letters, so you cannot simply count one and assume the remainder are of the other type.

### #5 charchar

Reputation: 0
• Posts: 11
• Joined: 01-July 11

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 08:57 AM

```import java.io.*;

public class stringCheck{

public static void main(String[] args) throws IOException {

//calling the method
validstr();//error(don't know why.:(/>)

}

public int validstr(char[] st) throws IOException{

System.out.print("Enter a line:");

// String reverse = new String(st); convert array into string
int digitCounts;
int lettersCount;

//less than 9(array's content)
for( int i=0;i<10;i++){

System.out.println("0");
}
for( int i=0;i<st.length;i++){

if(Character.isDigit(st[0])){
System.out.println("0");
}

//digits and letters only
else if(!(Character.isLetterOrDigit(st.length))){
System.out.println("0");
}

//digits must be less than the letters
//else if(){}

else{
System.out.println("1");
}

}
return 0;

}
}

```

They are in the same array and it must determine which is greater in number to output. the output is 1 if it's letters and 0 if it's digit.

### #6 nick2price

• D.I.C Lover

Reputation: 563
• Posts: 2,826
• Joined: 23-November 07

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 09:42 AM

You have a lot of issues here. Firstly
``` validstr();//error(don't know why.:(/>)
```

Unless you make the method static
```public static int validstr(char[] st) throws IOException{
```

Now why you assigning the input to an int? So tell us exactly what you want here. You want to ask the user for input and assign this to an Array? Just one element of an Array? You want to break each char and digit from the input into its own element in an Array?

Need more infomation. By the way, you doing stuff on an Array in your code and you havnt even created one.

### #7 charchar

Reputation: 0
• Posts: 11
• Joined: 01-July 11

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 10:20 AM

actually we are instructed to used the method validstr(char[] st)and i just used int thinking that i need a return value which is 0 and 1.
i want to ask the user for an input then it will be assigned to an array. the input needs to satisfy these conditions to output a value of 1 or else 0:
--array size more than 9
--1st value of input is a letter
--more letters than digits
--letters and digits only

``` ArrayList arr = new ArrayList();

```

thanks! i need to add these but i can't add the input in the array.

### #8 nick2price

• D.I.C Lover

Reputation: 563
• Posts: 2,826
• Joined: 23-November 07

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 11:26 AM

An ArrayList is not an Array, so you need to decide which one you want. Start like this
```BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
```

Now you will have the input stored in a String variable called line. Now you can turn this String into a char Array
```char[] st = line.toCharArray();
```

Now each char is in its own element within an array called st.

All of this should be done in your main method, not the method validstr. Once you have created a char array as above, you can pass it to the validstr method in your main
```validstr(st);
```

You now have this Array in your method, and you can perform your checks on it. So make sure Array is greater than 9
```if(st.length<9){
System.out.println("Array to small");
}
```

You just have to do some of the other task now which shouldnt be a problem.

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11312
• Posts: 42,605
• Joined: 27-December 08

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 11:31 AM

Also, ArrayLists are generic. So if you want an ArrayList of chars, you need to store an ArrayList<Character>.

### #10 charchar

Reputation: 0
• Posts: 11
• Joined: 01-July 11

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 11:46 AM

macosxnerd101, on 02 July 2011 - 11:31 AM, said:

Also, ArrayLists are generic. So if you want an ArrayList of chars, you need to store an ArrayList<Character>.

THANK YOU SO MUCH!!!!

### #11 charchar

Reputation: 0
• Posts: 11
• Joined: 01-July 11

## Re: comparing the count of digits to letters in an array

Posted 02 July 2011 - 10:59 PM

nick2price, on 02 July 2011 - 11:26 AM, said:

An ArrayList is not an Array, so you need to decide which one you want. Start like this
```BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
```

Now you will have the input stored in a String variable called line. Now you can turn this String into a char Array
```char[] st = line.toCharArray();
```

Now each char is in its own element within an array called st.

All of this should be done in your main method, not the method validstr. Once you have created a char array as above, you can pass it to the validstr method in your main
```validstr(st);
```

THANK YOU SO MUCH!!!)

You now have this Array in your method, and you can perform your checks on it. So make sure Array is greater than 9
```if(st.length<9){
System.out.println("Array to small");
}
```

You just have to do some of the other task now which shouldnt be a problem.