Results 1 to 2 of 2
- 10-14-2009, 03:58 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 71
- Rep Power
- 0
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:
- 10-14-2009, 04:28 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Read from a certain character to a certain character
By blackstormattack in forum New To JavaReplies: 0Last Post: 03-16-2009, 11:36 AM -
Printing out ... depending on character count of previous words entered.
By TheRocket in forum New To JavaReplies: 5Last Post: 11-22-2008, 01:34 PM -
Getting row count
By Java Tip in forum Java TipReplies: 0Last Post: 02-11-2008, 08:49 AM -
reading text character by character
By bugger in forum New To JavaReplies: 2Last Post: 11-09-2007, 08:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks