10 Replies - 4581 Views - Last Post: 02 July 2011 - 10:59 PM Rate Topic: -----

#1 charchar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 charchar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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{

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter a line:");

    int actualBuffered = input.read(st);
   // 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++){     
        
        //start with a letter
         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.
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 charchar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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(); 
    arr.add(input);



thanks! i need to add these but i can't add the input in the array.
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • 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:");
String line = reader.readLine(); 


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.
Was This Post Helpful? 2
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • 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>.
Was This Post Helpful? 0
  • +
  • -

#10 charchar  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 July 2011 - 11:46 AM

View Postmacosxnerd101, 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!!!!:)
Was This Post Helpful? 0
  • +
  • -

#11 charchar  Icon User is offline

  • New D.I.C Head

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

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

Posted 02 July 2011 - 10:59 PM

View Postnick2price, 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:");
String line = reader.readLine(); 


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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1