Results 1 to 2 of 2
  1. #1
    ashin is offline Member
    Join Date
    Nov 2008
    Posts
    71
    Rep Power
    0

    Default how to count the given character?

    hey guys pls help me how will i code if im going to ask the user what letter does he wants to count? this is my code.

    /**
    * @(#)array.java
    *
    *
    * @Andrea
    * @version 1.00 2009/10/6
    */

    import java.util.*;
    import javax.swing.*;
    public class arrayWithCounting {

    static String num_name,choose,num_ctr;
    static int con_num_name,i,conchoose;
    static String[] name;
    static String[] strName;

    public static void main(String[] args) {

    JOptionPane.showMessageDialog(null,"Project in DESALGO"
    + "\n By: BSCS501 \nAndrea Aseneta \n Genecar Rosales \n Jervis Quiambao","WELCOME!",JOptionPane.PLAIN_MESSAGE);

    try{
    //num_name is variable for number of names to be input
    num_name = JOptionPane.showInputDialog("How many names do you want to enter?");
    con_num_name = Integer.parseInt(num_name); //con_num_name convert the num_name into integer

    name = new String[con_num_name];

    for(i = 0; i<=(con_num_name-1);i++){
    name[i] = JOptionPane.showInputDialog("Enter name ["+(i+1)+"]");
    }

    strName = name;

    choose= JOptionPane.showInputDialog("[1] Ascending \n [2] Descending:");
    conchoose = Integer.parseInt(choose);
    switch(conchoose){
    case 1:
    Arrays.sort(strName, String.CASE_INSENSITIVE_ORDER);
    break;
    case 2:
    //Collections.reverse(Arrays.asList(strArray));
    Arrays.sort(strName, Collections.reverseOrder());
    break;
    default:
    JOptionPane.showMessageDialog(null,"Exceed!");
    System.exit(0);
    }
    num_ctr = JOptionPane.showInputDialog("What letter do you want to count?");
    char[] chartostr= new char[strName.toCharArray()];

    for(int ctr=0; ctr<chartostr.length; ctr++){

    }
    JOptionPane.showMessageDialog(null, Arrays.toString(strName));
    //System.out.println(Arrays.toString(strArray));

    }
    catch(NullPointerException e){
    JOptionPane.showMessageDialog(null,"An Error Occured!", "ERROR",JOptionPane.ERROR_MESSAGE);

    }
    catch(NumberFormatException e){
    JOptionPane.showMessageDialog(null,"Error! Number format exception occured.", "ERROR",JOptionPane.ERROR_MESSAGE);

    }
    catch(ArrayIndexOutOfBoundsException e){
    JOptionPane.showMessageDialog(null,"Array out of bounds!", "ERROR",JOptionPane.ERROR_MESSAGE);

    }

    }
    }
    :cool:

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Please use code tags when posting code.
    Read the API specs for the showInputDialog method. It returns a String. You can break a string into an array of characters using the toCharArray method or
    You can get the first character of a string using string.charAt(0).
    All these methods are described in the API specs.

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Read from a certain character to a certain character
    By blackstormattack in forum New To Java
    Replies: 0
    Last Post: 03-16-2009, 11:36 AM
  3. Replies: 5
    Last Post: 11-22-2008, 01:34 PM
  4. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM
  5. reading text character by character
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •