#1
| |||
| |||
Java String test Hi I am trying to write a java program which reads a string from the keyboard and then reports how many time each letter occurs within that string. Non-letters which are included in the string should also be reported. Please check my code for further details and please help me by replying. I am waiting for your reply. Thanks in advance. Code: Import B102.*; class_prog1 { public static void UserInput() { public String dt = new String() Screen.out.println("Please enter some characters values:"); String.toLowerCase() dt=keybd.in.readchar(); return dt; } public static void Calculate() { private char calcArray[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; private int index; for(index=0; i<?; index++) } |
#2
| |||
| |||
Re: Java String test Hi Instead you can use the Character.isLetter() method which can make your program more perfect. Also do not initialize fy string by creating a new String. Strings are immutable, so doing that is waste of time and memory. See the following block of code below, it may help you, if any queries post back again. Code: String dt = "test string"; int letterCount = 0; for (int i = 0; i < dt.length(); i++) { if (Character.isLetter(dt.charAt(i)) { letterCount++; } } |
#3
| |||
| |||
Re: Java String test Hi There is a good way out for your first part of the question. You can "cheat" taking the advantage of underlying Unicode representation of characters. But for that you will have to convert the String to all lowercase or all uppercase first. Check this code out, try it and see if it works with you. Code: int[] lettersCount = new int[26]; char letter = 'a'; lettersCount[(int)letter-'a']++;
__________________ Grand Theft Auto 4 PC Video Game |
#4
| |||
| |||
Re: Java String test Thanks a lot guys. Still I have not written the full code, but I think I got the idea what you guys are talking about. And I think I can implement them now in my code. I will try all possible way for it so that my code is perfect and simple to read. By the way thanks again. |
![]() |
|
Tags: data type, java, programming language, string |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Definition of a String in Java? | cloud101 | Software Development | 5 | 28-07-2014 10:14 AM |
The String Class in Java | blueprats | Guides & Tutorials | 3 | 12-03-2010 02:44 PM |
String to uppercase in java | ISAIAH | Software Development | 5 | 24-02-2010 12:33 AM |
Separating a string in java | Aaliya Seth | Software Development | 5 | 10-02-2010 04:27 AM |
How to convert string into int in Java | Zool | Software Development | 3 | 09-11-2009 12:41 PM |